OSDN Git Service

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