OSDN Git Service

Added the ability to archive projects:
[redminele/redmine.git] / app / controllers / users_controller.rb
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
18 class UsersController < ApplicationController
19   layout 'base' 
20   before_filter :require_admin
21
22   helper :sort
23   include SortHelper
24   helper :custom_fields
25   include CustomFieldsHelper   
26
27   def index
28     list
29     render :action => 'list' unless request.xhr?
30   end
31
32   def list
33     sort_init 'login', 'asc'
34     sort_update
35     
36     @status = params[:status] ? params[:status].to_i : 1    
37     conditions = nil
38     conditions = ["status=?", @status] unless @status == 0
39     
40     @user_count = User.count(:conditions => conditions)
41     @user_pages = Paginator.new self, @user_count,
42                                                                 15,
43                                                                 params['page']                                                          
44     @users =  User.find :all,:order => sort_clause,
45                         :conditions => conditions,
46                                                 :limit  =>  @user_pages.items_per_page,
47                                                 :offset =>  @user_pages.current.offset
48
49     render :action => "list", :layout => false if request.xhr?  
50   end
51
52   def add
53     if request.get?
54       @user = User.new(:language => Setting.default_language)
55       @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) }
56     else
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                      
63       if @user.save
64         Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
65         flash[:notice] = l(:notice_successful_create)
66         redirect_to :action => 'list'
67       end
68     end
69     @auth_sources = AuthSource.find(:all)
70   end
71
72   def edit
73     @user = User.find(params[:id])
74     if request.get?
75       @custom_values = UserCustomField.find(:all).collect { |x| @user.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
76     else
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
83       end
84       if @user.update_attributes(params[:user])
85         flash[:notice] = l(:notice_successful_update)
86         redirect_to :action => 'list'
87       end
88     end
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
93   end
94   
95   def edit_membership
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)
101     end
102     redirect_to :action => 'edit', :id => @user and return
103   end
104   
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)
109     end
110     redirect_to :action => 'edit', :id => @user and return
111   end
112
113   def destroy
114     User.find(params[:id]).destroy
115     redirect_to :action => 'list'
116   rescue
117     flash[:notice] = "Unable to delete user"
118     redirect_to :action => 'list'
119   end  
120 end