1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
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.
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.
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.
18 class UsersController < ApplicationController
20 before_filter :require_admin
25 include CustomFieldsHelper
29 render :action => 'list' unless request.xhr?
33 sort_init 'login', 'asc'
36 @status = params[:status] ? params[:status].to_i : 1
38 conditions = ["status=?", @status] unless @status == 0
40 @user_count = User.count(:conditions => conditions)
41 @user_pages = Paginator.new self, @user_count,
44 @users = User.find :all,:order => sort_clause,
45 :conditions => conditions,
46 :limit => @user_pages.items_per_page,
47 :offset => @user_pages.current.offset
49 render :action => "list", :layout => false if request.xhr?
54 @user = User.new(:language => Setting.default_language)
55 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) }
57 @user = User.new(params[:user])
58 @user.admin = params[:user][:admin] || false
59 @user.login = params[:user][:login]
60 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
61 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => params["custom_fields"][x.id.to_s]) }
62 @user.custom_values = @custom_values
64 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
65 flash[:notice] = l(:notice_successful_create)
66 redirect_to :action => 'list'
69 @auth_sources = AuthSource.find(:all)
73 @user = User.find(params[:id])
75 @custom_values = UserCustomField.find(:all).collect { |x| @user.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
77 @user.admin = params[:user][:admin] if params[:user][:admin]
78 @user.login = params[:user][:login] if params[:user][:login]
79 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id
80 if params[:custom_fields]
81 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => params["custom_fields"][x.id.to_s]) }
82 @user.custom_values = @custom_values
84 if @user.update_attributes(params[:user])
85 flash[:notice] = l(:notice_successful_update)
86 redirect_to :action => 'list'
89 @auth_sources = AuthSource.find(:all)
90 @roles = Role.find(:all, :order => 'position')
91 @projects = Project.find(:all, :order => 'name', :conditions => "status=#{Project::STATUS_ACTIVE}") - @user.projects
92 @membership ||= Member.new
96 @user = User.find(params[:id])
97 @membership = params[:membership_id] ? Member.find(params[:membership_id]) : Member.new(:user => @user)
98 @membership.attributes = params[:membership]
99 if request.post? and @membership.save
100 flash[:notice] = l(:notice_successful_update)
102 redirect_to :action => 'edit', :id => @user and return
105 def destroy_membership
106 @user = User.find(params[:id])
107 if request.post? and Member.find(params[:membership_id]).destroy
108 flash[:notice] = l(:notice_successful_update)
110 redirect_to :action => 'edit', :id => @user and return
114 User.find(params[:id]).destroy
115 redirect_to :action => 'list'
117 flash[:notice] = "Unable to delete user"
118 redirect_to :action => 'list'