@@ -1,227 +1,227 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] |
|
22 | before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership] | |
23 | accept_api_auth :index, :show, :create, :update, :destroy |
|
23 | accept_api_auth :index, :show, :create, :update, :destroy | |
24 |
|
24 | |||
25 | helper :sort |
|
25 | helper :sort | |
26 | include SortHelper |
|
26 | include SortHelper | |
27 | helper :custom_fields |
|
27 | helper :custom_fields | |
28 | include CustomFieldsHelper |
|
28 | include CustomFieldsHelper | |
29 |
|
29 | |||
30 | def index |
|
30 | def index | |
31 | sort_init 'login', 'asc' |
|
31 | sort_init 'login', 'asc' | |
32 | sort_update %w(login firstname lastname mail admin created_on last_login_on) |
|
32 | sort_update %w(login firstname lastname mail admin created_on last_login_on) | |
33 |
|
33 | |||
34 | case params[:format] |
|
34 | case params[:format] | |
35 | when 'xml', 'json' |
|
35 | when 'xml', 'json' | |
36 | @offset, @limit = api_offset_and_limit |
|
36 | @offset, @limit = api_offset_and_limit | |
37 | else |
|
37 | else | |
38 | @limit = per_page_option |
|
38 | @limit = per_page_option | |
39 | end |
|
39 | end | |
40 |
|
40 | |||
41 | @status = params[:status] || 1 |
|
41 | @status = params[:status] || 1 | |
42 |
|
42 | |||
43 | scope = User.logged.status(@status) |
|
43 | scope = User.logged.status(@status) | |
44 | scope = scope.like(params[:name]) if params[:name].present? |
|
44 | scope = scope.like(params[:name]) if params[:name].present? | |
45 | scope = scope.in_group(params[:group_id]) if params[:group_id].present? |
|
45 | scope = scope.in_group(params[:group_id]) if params[:group_id].present? | |
46 |
|
46 | |||
47 | @user_count = scope.count |
|
47 | @user_count = scope.count | |
48 | @user_pages = Paginator.new self, @user_count, @limit, params['page'] |
|
48 | @user_pages = Paginator.new self, @user_count, @limit, params['page'] | |
49 | @offset ||= @user_pages.current.offset |
|
49 | @offset ||= @user_pages.current.offset | |
50 | @users = scope.find :all, |
|
50 | @users = scope.find :all, | |
51 | :order => sort_clause, |
|
51 | :order => sort_clause, | |
52 | :limit => @limit, |
|
52 | :limit => @limit, | |
53 | :offset => @offset |
|
53 | :offset => @offset | |
54 |
|
54 | |||
55 | respond_to do |format| |
|
55 | respond_to do |format| | |
56 | format.html { |
|
56 | format.html { | |
57 | @groups = Group.all.sort |
|
57 | @groups = Group.all.sort | |
58 | render :layout => !request.xhr? |
|
58 | render :layout => !request.xhr? | |
59 | } |
|
59 | } | |
60 | format.api |
|
60 | format.api | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def show |
|
64 | def show | |
65 | # show projects based on current user visibility |
|
65 | # show projects based on current user visibility | |
66 | @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) |
|
66 | @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) | |
67 |
|
67 | |||
68 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
68 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) | |
69 | @events_by_day = events.group_by(&:event_date) |
|
69 | @events_by_day = events.group_by(&:event_date) | |
70 |
|
70 | |||
71 | unless User.current.admin? |
|
71 | unless User.current.admin? | |
72 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
|
72 | if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) | |
73 | render_404 |
|
73 | render_404 | |
74 | return |
|
74 | return | |
75 | end |
|
75 | end | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | respond_to do |format| |
|
78 | respond_to do |format| | |
79 | format.html { render :layout => 'base' } |
|
79 | format.html { render :layout => 'base' } | |
80 | format.api |
|
80 | format.api | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def new |
|
84 | def new | |
85 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
85 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
86 | @auth_sources = AuthSource.find(:all) |
|
86 | @auth_sources = AuthSource.find(:all) | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | def create |
|
89 | def create | |
90 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) |
|
90 | @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) | |
91 | @user.safe_attributes = params[:user] |
|
91 | @user.safe_attributes = params[:user] | |
92 | @user.admin = params[:user][:admin] || false |
|
92 | @user.admin = params[:user][:admin] || false | |
93 | @user.login = params[:user][:login] |
|
93 | @user.login = params[:user][:login] | |
94 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id |
|
94 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id | |
95 |
|
95 | |||
96 | if @user.save |
|
96 | if @user.save | |
97 | @user.pref.attributes = params[:pref] |
|
97 | @user.pref.attributes = params[:pref] | |
98 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
98 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
99 | @user.pref.save |
|
99 | @user.pref.save | |
100 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
100 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) | |
101 |
|
101 | |||
102 | Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information] |
|
102 | Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information] | |
103 |
|
103 | |||
104 | respond_to do |format| |
|
104 | respond_to do |format| | |
105 | format.html { |
|
105 | format.html { | |
106 | flash[:notice] = l(:notice_successful_create) |
|
106 | flash[:notice] = l(:notice_successful_create) | |
107 | redirect_to(params[:continue] ? |
|
107 | redirect_to(params[:continue] ? | |
108 | {:controller => 'users', :action => 'new'} : |
|
108 | {:controller => 'users', :action => 'new'} : | |
109 | {:controller => 'users', :action => 'edit', :id => @user} |
|
109 | {:controller => 'users', :action => 'edit', :id => @user} | |
110 | ) |
|
110 | ) | |
111 | } |
|
111 | } | |
112 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } |
|
112 | format.api { render :action => 'show', :status => :created, :location => user_url(@user) } | |
113 | end |
|
113 | end | |
114 | else |
|
114 | else | |
115 | @auth_sources = AuthSource.find(:all) |
|
115 | @auth_sources = AuthSource.find(:all) | |
116 | # Clear password input |
|
116 | # Clear password input | |
117 | @user.password = @user.password_confirmation = nil |
|
117 | @user.password = @user.password_confirmation = nil | |
118 |
|
118 | |||
119 | respond_to do |format| |
|
119 | respond_to do |format| | |
120 | format.html { render :action => 'new' } |
|
120 | format.html { render :action => 'new' } | |
121 | format.api { render_validation_errors(@user) } |
|
121 | format.api { render_validation_errors(@user) } | |
122 | end |
|
122 | end | |
123 | end |
|
123 | end | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | def edit |
|
126 | def edit | |
127 | @auth_sources = AuthSource.find(:all) |
|
127 | @auth_sources = AuthSource.find(:all) | |
128 | @membership ||= Member.new |
|
128 | @membership ||= Member.new | |
129 | end |
|
129 | end | |
130 |
|
130 | |||
131 | def update |
|
131 | def update | |
132 | @user.admin = params[:user][:admin] if params[:user][:admin] |
|
132 | @user.admin = params[:user][:admin] if params[:user][:admin] | |
133 | @user.login = params[:user][:login] if params[:user][:login] |
|
133 | @user.login = params[:user][:login] if params[:user][:login] | |
134 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
|
134 | if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) | |
135 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] |
|
135 | @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] | |
136 | end |
|
136 | end | |
137 | @user.safe_attributes = params[:user] |
|
137 | @user.safe_attributes = params[:user] | |
138 | # Was the account actived ? (do it before User#save clears the change) |
|
138 | # Was the account actived ? (do it before User#save clears the change) | |
139 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
|
139 | was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) | |
140 | # TODO: Similar to My#account |
|
140 | # TODO: Similar to My#account | |
141 | @user.pref.attributes = params[:pref] |
|
141 | @user.pref.attributes = params[:pref] | |
142 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
|
142 | @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') | |
143 |
|
143 | |||
144 | if @user.save |
|
144 | if @user.save | |
145 | @user.pref.save |
|
145 | @user.pref.save | |
146 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) |
|
146 | @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) | |
147 |
|
147 | |||
148 | if was_activated |
|
148 | if was_activated | |
149 | Mailer.account_activated(@user).deliver |
|
149 | Mailer.account_activated(@user).deliver | |
150 | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? |
|
150 | elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? | |
151 | Mailer.account_information(@user, params[:user][:password]).deliver |
|
151 | Mailer.account_information(@user, params[:user][:password]).deliver | |
152 | end |
|
152 | end | |
153 |
|
153 | |||
154 | respond_to do |format| |
|
154 | respond_to do |format| | |
155 | format.html { |
|
155 | format.html { | |
156 | flash[:notice] = l(:notice_successful_update) |
|
156 | flash[:notice] = l(:notice_successful_update) | |
157 | redirect_to_referer_or edit_user_path(@user) |
|
157 | redirect_to_referer_or edit_user_path(@user) | |
158 | } |
|
158 | } | |
159 | format.api { head :ok } |
|
159 | format.api { head :ok } | |
160 | end |
|
160 | end | |
161 | else |
|
161 | else | |
162 | @auth_sources = AuthSource.find(:all) |
|
162 | @auth_sources = AuthSource.find(:all) | |
163 | @membership ||= Member.new |
|
163 | @membership ||= Member.new | |
164 | # Clear password input |
|
164 | # Clear password input | |
165 | @user.password = @user.password_confirmation = nil |
|
165 | @user.password = @user.password_confirmation = nil | |
166 |
|
166 | |||
167 | respond_to do |format| |
|
167 | respond_to do |format| | |
168 | format.html { render :action => :edit } |
|
168 | format.html { render :action => :edit } | |
169 | format.api { render_validation_errors(@user) } |
|
169 | format.api { render_validation_errors(@user) } | |
170 | end |
|
170 | end | |
171 | end |
|
171 | end | |
172 | end |
|
172 | end | |
173 |
|
173 | |||
174 | def destroy |
|
174 | def destroy | |
175 | @user.destroy |
|
175 | @user.destroy | |
176 | respond_to do |format| |
|
176 | respond_to do |format| | |
177 | format.html { redirect_to(users_url) } |
|
177 | format.html { redirect_to_referer_or(users_url) } | |
178 | format.api { head :ok } |
|
178 | format.api { head :ok } | |
179 | end |
|
179 | end | |
180 | end |
|
180 | end | |
181 |
|
181 | |||
182 | def edit_membership |
|
182 | def edit_membership | |
183 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
|
183 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) | |
184 | @membership.save |
|
184 | @membership.save | |
185 | respond_to do |format| |
|
185 | respond_to do |format| | |
186 | if @membership.valid? |
|
186 | if @membership.valid? | |
187 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
187 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
188 | format.js { |
|
188 | format.js { | |
189 | render(:update) {|page| |
|
189 | render(:update) {|page| | |
190 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' |
|
190 | page.replace_html "tab-content-memberships", :partial => 'users/memberships' | |
191 | page.visual_effect(:highlight, "member-#{@membership.id}") |
|
191 | page.visual_effect(:highlight, "member-#{@membership.id}") | |
192 | } |
|
192 | } | |
193 | } |
|
193 | } | |
194 | else |
|
194 | else | |
195 | format.js { |
|
195 | format.js { | |
196 | render(:update) {|page| |
|
196 | render(:update) {|page| | |
197 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) |
|
197 | page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) | |
198 | } |
|
198 | } | |
199 | } |
|
199 | } | |
200 | end |
|
200 | end | |
201 | end |
|
201 | end | |
202 | end |
|
202 | end | |
203 |
|
203 | |||
204 | def destroy_membership |
|
204 | def destroy_membership | |
205 | @membership = Member.find(params[:membership_id]) |
|
205 | @membership = Member.find(params[:membership_id]) | |
206 | if @membership.deletable? |
|
206 | if @membership.deletable? | |
207 | @membership.destroy |
|
207 | @membership.destroy | |
208 | end |
|
208 | end | |
209 | respond_to do |format| |
|
209 | respond_to do |format| | |
210 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } |
|
210 | format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } | |
211 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } |
|
211 | format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } | |
212 | end |
|
212 | end | |
213 | end |
|
213 | end | |
214 |
|
214 | |||
215 | private |
|
215 | private | |
216 |
|
216 | |||
217 | def find_user |
|
217 | def find_user | |
218 | if params[:id] == 'current' |
|
218 | if params[:id] == 'current' | |
219 | require_login || return |
|
219 | require_login || return | |
220 | @user = User.current |
|
220 | @user = User.current | |
221 | else |
|
221 | else | |
222 | @user = User.find(params[:id]) |
|
222 | @user = User.find(params[:id]) | |
223 | end |
|
223 | end | |
224 | rescue ActiveRecord::RecordNotFound |
|
224 | rescue ActiveRecord::RecordNotFound | |
225 | render_404 |
|
225 | render_404 | |
226 | end |
|
226 | end | |
227 | end |
|
227 | end |
General Comments 0
You need to be logged in to leave comments.
Login now