You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_controller.rb 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class UsersController < ApplicationController
  18. layout 'base'
  19. before_filter :require_admin
  20. helper :sort
  21. include SortHelper
  22. helper :custom_fields
  23. include CustomFieldsHelper
  24. def index
  25. list
  26. render :action => 'list' unless request.xhr?
  27. end
  28. def list
  29. sort_init 'login', 'asc'
  30. sort_update %w(login firstname lastname mail admin created_on last_login_on)
  31. @status = params[:status] ? params[:status].to_i : 1
  32. conditions = "status <> 0"
  33. conditions = ["status=?", @status] unless @status == 0
  34. @user_count = User.count(:conditions => conditions)
  35. @user_pages = Paginator.new self, @user_count,
  36. per_page_option,
  37. params['page']
  38. @users = User.find :all,:order => sort_clause,
  39. :conditions => conditions,
  40. :limit => @user_pages.items_per_page,
  41. :offset => @user_pages.current.offset
  42. render :action => "list", :layout => false if request.xhr?
  43. end
  44. def add
  45. if request.get?
  46. @user = User.new(:language => Setting.default_language)
  47. @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @user) }
  48. else
  49. @user = User.new(params[:user])
  50. @user.admin = params[:user][:admin] || false
  51. @user.login = params[:user][:login]
  52. @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
  53. @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) }
  54. @user.custom_values = @custom_values
  55. if @user.save
  56. Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
  57. flash[:notice] = l(:notice_successful_create)
  58. redirect_to :action => 'list'
  59. end
  60. end
  61. @auth_sources = AuthSource.find(:all)
  62. end
  63. def edit
  64. @user = User.find(params[:id])
  65. if request.get?
  66. @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| @user.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
  67. else
  68. @user.admin = params[:user][:admin] if params[:user][:admin]
  69. @user.login = params[:user][:login] if params[:user][:login]
  70. @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id
  71. if params[:custom_fields]
  72. @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => params["custom_fields"][x.id.to_s]) }
  73. @user.custom_values = @custom_values
  74. end
  75. if @user.update_attributes(params[:user])
  76. flash[:notice] = l(:notice_successful_update)
  77. # Give a string to redirect_to otherwise it would use status param as the response code
  78. redirect_to(url_for(:action => 'list', :status => params[:status], :page => params[:page]))
  79. end
  80. end
  81. @auth_sources = AuthSource.find(:all)
  82. @roles = Role.find_all_givable
  83. @projects = Project.find(:all, :order => 'name', :conditions => "status=#{Project::STATUS_ACTIVE}") - @user.projects
  84. @membership ||= Member.new
  85. end
  86. def edit_membership
  87. @user = User.find(params[:id])
  88. @membership = params[:membership_id] ? Member.find(params[:membership_id]) : Member.new(:user => @user)
  89. @membership.attributes = params[:membership]
  90. if request.post? and @membership.save
  91. flash[:notice] = l(:notice_successful_update)
  92. end
  93. redirect_to :action => 'edit', :id => @user and return
  94. end
  95. def destroy_membership
  96. @user = User.find(params[:id])
  97. if request.post? and Member.find(params[:membership_id]).destroy
  98. flash[:notice] = l(:notice_successful_update)
  99. end
  100. redirect_to :action => 'edit', :id => @user and return
  101. end
  102. end