@@ -1,197 +1,216 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class UsersController < ApplicationController |
|
18 | class UsersController < ApplicationController | |
19 | layout 'admin' |
|
19 | layout 'admin' | |
20 |
|
20 | |||
21 | before_filter :require_admin, :except => :show |
|
21 | before_filter :require_admin, :except => :show | |
22 |
|
22 | |||
23 | helper :sort |
|
23 | helper :sort | |
24 | include SortHelper |
|
24 | include SortHelper | |
25 | helper :custom_fields |
|
25 | helper :custom_fields | |
26 | include CustomFieldsHelper |
|
26 | include CustomFieldsHelper | |
27 |
|
27 | |||
28 | def index |
|
28 | def index | |
29 | sort_init 'login', 'asc' |
|
29 | sort_init 'login', 'asc' | |
30 | sort_update %w(login firstname lastname mail admin created_on last_login_on) |
|
30 | sort_update %w(login firstname lastname mail admin created_on last_login_on) | |
31 |
|
31 | |||
32 | @status = params[:status] ? params[:status].to_i : 1 |
|
32 | @status = params[:status] ? params[:status].to_i : 1 | |
33 | c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) |
|
33 | c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) | |
34 |
|
34 | |||
35 | unless params[:name].blank? |
|
35 | unless params[:name].blank? | |
36 | name = "%#{params[:name].strip.downcase}%" |
|
36 | name = "%#{params[:name].strip.downcase}%" | |
37 | c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] |
|
37 | c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] | |
38 | end |
|
38 | end | |
39 |
|
39 | |||
40 | @user_count = User.count(:conditions => c.conditions) |
|
40 | @user_count = User.count(:conditions => c.conditions) | |
41 | @user_pages = Paginator.new self, @user_count, |
|
41 | @user_pages = Paginator.new self, @user_count, | |
42 | per_page_option, |
|
42 | per_page_option, | |
43 | params['page'] |
|
43 | params['page'] | |
44 | @users = User.find :all,:order => sort_clause, |
|
44 | @users = User.find :all,:order => sort_clause, | |
45 | :conditions => c.conditions, |
|
45 | :conditions => c.conditions, | |
46 | :limit => @user_pages.items_per_page, |
|
46 | :limit => @user_pages.items_per_page, | |
47 | :offset => @user_pages.current.offset |
|
47 | :offset => @user_pages.current.offset | |
48 |
|
48 | |||
49 | render :layout => !request.xhr? |
|
49 | respond_to do |format| | |
|
50 | format.html { render :layout => !request.xhr? } | |||
|
51 | end | |||
50 | end |
|
52 | end | |
51 |
|
53 | |||
52 | def show |
|
54 | def show | |
53 | @user = User.find(params[:id]) |
|
55 | @user = User.find(params[:id]) | |
54 |
|
56 | |||
55 | # show projects based on current user visibility |
|
57 | # show projects based on current user visibility | |
56 | @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current)) |
|
58 | @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current)) | |
57 |
|
59 | |||
58 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
60 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) | |
59 | @events_by_day = events.group_by(&:event_date) |
|
61 | @events_by_day = events.group_by(&:event_date) | |
60 |
|
62 | |||
61 | unless User.current.admin? |
|
63 | unless User.current.admin? | |
62 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
|
64 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) | |
63 | render_404 |
|
65 | render_404 | |
64 | return |
|
66 | return | |
65 | end |
|
67 | end | |
66 | end |
|
68 | end | |
67 | render :layout => 'base' |
|
69 | ||
68 |
|
70 | respond_to do |format| | ||
|
71 | format.html { render :layout => 'base' } | |||
|
72 | end | |||
69 | rescue ActiveRecord::RecordNotFound |
|
73 | rescue ActiveRecord::RecordNotFound | |
70 | render_404 |
|
74 | render_404 | |
71 | end |
|
75 | end | |
72 |
|
76 | |||
73 | def new |
|
77 | def new | |
74 | @notification_options = User::MAIL_NOTIFICATION_OPTIONS |
|
78 | @notification_options = User::MAIL_NOTIFICATION_OPTIONS | |
75 | @notification_option = Setting.default_notification_option |
|
79 | @notification_option = Setting.default_notification_option | |
76 |
|
80 | |||
77 | @user = User.new(:language => Setting.default_language) |
|
81 | @user = User.new(:language => Setting.default_language) | |
78 | @auth_sources = AuthSource.find(:all) |
|
82 | @auth_sources = AuthSource.find(:all) | |
79 | end |
|
83 | end | |
80 |
|
84 | |||
81 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
|
85 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } | |
82 | def create |
|
86 | def create | |
83 | @notification_options = User::MAIL_NOTIFICATION_OPTIONS |
|
87 | @notification_options = User::MAIL_NOTIFICATION_OPTIONS | |
84 | @notification_option = Setting.default_notification_option |
|
88 | @notification_option = Setting.default_notification_option | |
85 |
|
89 | |||
86 | @user = User.new(params[:user]) |
|
90 | @user = User.new(params[:user]) | |
87 | @user.admin = params[:user][:admin] || false |
|
91 | @user.admin = params[:user][:admin] || false | |
88 | @user.login = params[:user][:login] |
|
92 | @user.login = params[:user][:login] | |
89 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id |
|
93 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id | |
90 |
|
94 | |||
91 | # TODO: Similar to My#account |
|
95 | # TODO: Similar to My#account | |
92 | @user.mail_notification = params[:notification_option] || 'only_my_events' |
|
96 | @user.mail_notification = params[:notification_option] || 'only_my_events' | |
93 | @user.pref.attributes = params[:pref] |
|
97 | @user.pref.attributes = params[:pref] | |
94 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
98 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
95 |
|
99 | |||
96 | if @user.save |
|
100 | if @user.save | |
97 | @user.pref.save |
|
101 | @user.pref.save | |
98 | @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) |
|
102 | @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) | |
99 |
|
103 | |||
100 | Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] |
|
104 | Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] | |
101 | flash[:notice] = l(:notice_successful_create) |
|
105 | ||
102 | redirect_to(params[:continue] ? {:controller => 'users', :action => 'new'} : |
|
106 | respond_to do |format| | |
103 | {:controller => 'users', :action => 'edit', :id => @user}) |
|
107 | format.html { | |
104 | return |
|
108 | flash[:notice] = l(:notice_successful_create) | |
|
109 | redirect_to(params[:continue] ? | |||
|
110 | {:controller => 'users', :action => 'new'} : | |||
|
111 | {:controller => 'users', :action => 'edit', :id => @user} | |||
|
112 | ) | |||
|
113 | } | |||
|
114 | end | |||
105 | else |
|
115 | else | |
106 | @auth_sources = AuthSource.find(:all) |
|
116 | @auth_sources = AuthSource.find(:all) | |
107 | @notification_option = @user.mail_notification |
|
117 | @notification_option = @user.mail_notification | |
108 |
|
118 | |||
109 | render :action => 'new' |
|
119 | respond_to do |format| | |
|
120 | format.html { render :action => 'new' } | |||
|
121 | end | |||
110 | end |
|
122 | end | |
111 | end |
|
123 | end | |
112 |
|
124 | |||
113 | def edit |
|
125 | def edit | |
114 | @user = User.find(params[:id]) |
|
126 | @user = User.find(params[:id]) | |
115 | @notification_options = @user.valid_notification_options |
|
127 | @notification_options = @user.valid_notification_options | |
116 | @notification_option = @user.mail_notification |
|
128 | @notification_option = @user.mail_notification | |
117 |
|
129 | |||
118 | @auth_sources = AuthSource.find(:all) |
|
130 | @auth_sources = AuthSource.find(:all) | |
119 | @membership ||= Member.new |
|
131 | @membership ||= Member.new | |
120 | end |
|
132 | end | |
121 |
|
133 | |||
122 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
134 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } | |
123 | def update |
|
135 | def update | |
124 | @user = User.find(params[:id]) |
|
136 | @user = User.find(params[:id]) | |
125 | @notification_options = @user.valid_notification_options |
|
137 | @notification_options = @user.valid_notification_options | |
126 | @notification_option = @user.mail_notification |
|
138 | @notification_option = @user.mail_notification | |
127 |
|
139 | |||
128 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
140 | @user.admin = params[:user][:admin] if params[:user][:admin] | |
129 | @user.login = params[:user][:login] if params[:user][:login] |
|
141 | @user.login = params[:user][:login] if params[:user][:login] | |
130 | if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
142 | if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) | |
131 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
|
143 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] | |
132 | end |
|
144 | end | |
133 | @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids] |
|
145 | @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids] | |
134 | @user.attributes = params[:user] |
|
146 | @user.attributes = params[:user] | |
135 | # Was the account actived ? (do it before User#save clears the change) |
|
147 | # Was the account actived ? (do it before User#save clears the change) | |
136 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
148 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) | |
137 | # TODO: Similar to My#account |
|
149 | # TODO: Similar to My#account | |
138 | @user.mail_notification = params[:notification_option] || 'only_my_events' |
|
150 | @user.mail_notification = params[:notification_option] || 'only_my_events' | |
139 | @user.pref.attributes = params[:pref] |
|
151 | @user.pref.attributes = params[:pref] | |
140 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
152 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
141 |
|
153 | |||
142 | if @user.save |
|
154 | if @user.save | |
143 | @user.pref.save |
|
155 | @user.pref.save | |
144 | @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) |
|
156 | @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) | |
145 |
|
157 | |||
146 | if was_activated |
|
158 | if was_activated | |
147 | Mailer.deliver_account_activated(@user) |
|
159 | Mailer.deliver_account_activated(@user) | |
148 | elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil? |
|
160 | elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil? | |
149 | Mailer.deliver_account_information(@user, params[:password]) |
|
161 | Mailer.deliver_account_information(@user, params[:password]) | |
150 | end |
|
162 | end | |
151 | flash[:notice] = l(:notice_successful_update) |
|
163 | ||
152 | redirect_to :back |
|
164 | respond_to do |format| | |
|
165 | format.html { | |||
|
166 | flash[:notice] = l(:notice_successful_update) | |||
|
167 | redirect_to :back | |||
|
168 | } | |||
|
169 | end | |||
153 | else |
|
170 | else | |
154 | @auth_sources = AuthSource.find(:all) |
|
171 | @auth_sources = AuthSource.find(:all) | |
155 | @membership ||= Member.new |
|
172 | @membership ||= Member.new | |
156 |
|
173 | |||
157 | render :action => :edit |
|
174 | respond_to do |format| | |
|
175 | format.html { render :action => :edit } | |||
|
176 | end | |||
158 | end |
|
177 | end | |
159 | rescue ::ActionController::RedirectBackError |
|
178 | rescue ::ActionController::RedirectBackError | |
160 | redirect_to :controller => 'users', :action => 'edit', :id => @user |
|
179 | redirect_to :controller => 'users', :action => 'edit', :id => @user | |
161 | end |
|
180 | end | |
162 |
|
181 | |||
163 | def edit_membership |
|
182 | def edit_membership | |
164 | @user = User.find(params[:id]) |
|
183 | @user = User.find(params[:id]) | |
165 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
|
184 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) | |
166 | @membership.save if request.post? |
|
185 | @membership.save if request.post? | |
167 | respond_to do |format| |
|
186 | respond_to do |format| | |
168 | if @membership.valid? |
|
187 | if @membership.valid? | |
169 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
188 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
170 | format.js { |
|
189 | format.js { | |
171 | render(:update) {|page| |
|
190 | render(:update) {|page| | |
172 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' |
|
191 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' | |
173 | page.visual_effect(:highlight, "member-#{@membership.id}") |
|
192 | page.visual_effect(:highlight, "member-#{@membership.id}") | |
174 | } |
|
193 | } | |
175 | } |
|
194 | } | |
176 | else |
|
195 | else | |
177 | format.js { |
|
196 | format.js { | |
178 | render(:update) {|page| |
|
197 | render(:update) {|page| | |
179 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) |
|
198 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) | |
180 | } |
|
199 | } | |
181 | } |
|
200 | } | |
182 | end |
|
201 | end | |
183 | end |
|
202 | end | |
184 | end |
|
203 | end | |
185 |
|
204 | |||
186 | def destroy_membership |
|
205 | def destroy_membership | |
187 | @user = User.find(params[:id]) |
|
206 | @user = User.find(params[:id]) | |
188 | @membership = Member.find(params[:membership_id]) |
|
207 | @membership = Member.find(params[:membership_id]) | |
189 | if request.post? && @membership.deletable? |
|
208 | if request.post? && @membership.deletable? | |
190 | @membership.destroy |
|
209 | @membership.destroy | |
191 | end |
|
210 | end | |
192 | respond_to do |format| |
|
211 | respond_to do |format| | |
193 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
212 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
194 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } |
|
213 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } | |
195 | end |
|
214 | end | |
196 | end |
|
215 | end | |
197 | end |
|
216 | end |
General Comments 0
You need to be logged in to leave comments.
Login now