@@ -1,361 +1,361 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 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 AccountController < ApplicationController |
|
18 | class AccountController < ApplicationController | |
19 | helper :custom_fields |
|
19 | helper :custom_fields | |
20 | include CustomFieldsHelper |
|
20 | include CustomFieldsHelper | |
21 |
|
21 | |||
22 | # prevents login action to be filtered by check_if_login_required application scope filter |
|
22 | # prevents login action to be filtered by check_if_login_required application scope filter | |
23 |
skip_before_ |
|
23 | skip_before_action :check_if_login_required, :check_password_change | |
24 |
|
24 | |||
25 | # Overrides ApplicationController#verify_authenticity_token to disable |
|
25 | # Overrides ApplicationController#verify_authenticity_token to disable | |
26 | # token verification on openid callbacks |
|
26 | # token verification on openid callbacks | |
27 | def verify_authenticity_token |
|
27 | def verify_authenticity_token | |
28 | unless using_open_id? |
|
28 | unless using_open_id? | |
29 | super |
|
29 | super | |
30 | end |
|
30 | end | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | # Login request and validation |
|
33 | # Login request and validation | |
34 | def login |
|
34 | def login | |
35 | if request.get? |
|
35 | if request.get? | |
36 | if User.current.logged? |
|
36 | if User.current.logged? | |
37 | redirect_back_or_default home_url, :referer => true |
|
37 | redirect_back_or_default home_url, :referer => true | |
38 | end |
|
38 | end | |
39 | else |
|
39 | else | |
40 | authenticate_user |
|
40 | authenticate_user | |
41 | end |
|
41 | end | |
42 | rescue AuthSourceException => e |
|
42 | rescue AuthSourceException => e | |
43 | logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" |
|
43 | logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" | |
44 | render_error :message => e.message |
|
44 | render_error :message => e.message | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | # Log out current user and redirect to welcome page |
|
47 | # Log out current user and redirect to welcome page | |
48 | def logout |
|
48 | def logout | |
49 | if User.current.anonymous? |
|
49 | if User.current.anonymous? | |
50 | redirect_to home_url |
|
50 | redirect_to home_url | |
51 | elsif request.post? |
|
51 | elsif request.post? | |
52 | logout_user |
|
52 | logout_user | |
53 | redirect_to home_url |
|
53 | redirect_to home_url | |
54 | end |
|
54 | end | |
55 | # display the logout form |
|
55 | # display the logout form | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | # Lets user choose a new password |
|
58 | # Lets user choose a new password | |
59 | def lost_password |
|
59 | def lost_password | |
60 | (redirect_to(home_url); return) unless Setting.lost_password? |
|
60 | (redirect_to(home_url); return) unless Setting.lost_password? | |
61 | if params[:token] |
|
61 | if params[:token] | |
62 | @token = Token.find_token("recovery", params[:token].to_s) |
|
62 | @token = Token.find_token("recovery", params[:token].to_s) | |
63 | if @token.nil? || @token.expired? |
|
63 | if @token.nil? || @token.expired? | |
64 | redirect_to home_url |
|
64 | redirect_to home_url | |
65 | return |
|
65 | return | |
66 | end |
|
66 | end | |
67 | @user = @token.user |
|
67 | @user = @token.user | |
68 | unless @user && @user.active? |
|
68 | unless @user && @user.active? | |
69 | redirect_to home_url |
|
69 | redirect_to home_url | |
70 | return |
|
70 | return | |
71 | end |
|
71 | end | |
72 | if request.post? |
|
72 | if request.post? | |
73 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
73 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | |
74 | if @user.save |
|
74 | if @user.save | |
75 | @token.destroy |
|
75 | @token.destroy | |
76 | Mailer.password_updated(@user) |
|
76 | Mailer.password_updated(@user) | |
77 | flash[:notice] = l(:notice_account_password_updated) |
|
77 | flash[:notice] = l(:notice_account_password_updated) | |
78 | redirect_to signin_path |
|
78 | redirect_to signin_path | |
79 | return |
|
79 | return | |
80 | end |
|
80 | end | |
81 | end |
|
81 | end | |
82 | render :template => "account/password_recovery" |
|
82 | render :template => "account/password_recovery" | |
83 | return |
|
83 | return | |
84 | else |
|
84 | else | |
85 | if request.post? |
|
85 | if request.post? | |
86 | email = params[:mail].to_s |
|
86 | email = params[:mail].to_s | |
87 | user = User.find_by_mail(email) |
|
87 | user = User.find_by_mail(email) | |
88 | # user not found |
|
88 | # user not found | |
89 | unless user |
|
89 | unless user | |
90 | flash.now[:error] = l(:notice_account_unknown_email) |
|
90 | flash.now[:error] = l(:notice_account_unknown_email) | |
91 | return |
|
91 | return | |
92 | end |
|
92 | end | |
93 | unless user.active? |
|
93 | unless user.active? | |
94 | handle_inactive_user(user, lost_password_path) |
|
94 | handle_inactive_user(user, lost_password_path) | |
95 | return |
|
95 | return | |
96 | end |
|
96 | end | |
97 | # user cannot change its password |
|
97 | # user cannot change its password | |
98 | unless user.change_password_allowed? |
|
98 | unless user.change_password_allowed? | |
99 | flash.now[:error] = l(:notice_can_t_change_password) |
|
99 | flash.now[:error] = l(:notice_can_t_change_password) | |
100 | return |
|
100 | return | |
101 | end |
|
101 | end | |
102 | # create a new token for password recovery |
|
102 | # create a new token for password recovery | |
103 | token = Token.new(:user => user, :action => "recovery") |
|
103 | token = Token.new(:user => user, :action => "recovery") | |
104 | if token.save |
|
104 | if token.save | |
105 | # Don't use the param to send the email |
|
105 | # Don't use the param to send the email | |
106 | recipent = user.mails.detect {|e| email.casecmp(e) == 0} || user.mail |
|
106 | recipent = user.mails.detect {|e| email.casecmp(e) == 0} || user.mail | |
107 | Mailer.lost_password(token, recipent).deliver |
|
107 | Mailer.lost_password(token, recipent).deliver | |
108 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
108 | flash[:notice] = l(:notice_account_lost_email_sent) | |
109 | redirect_to signin_path |
|
109 | redirect_to signin_path | |
110 | return |
|
110 | return | |
111 | end |
|
111 | end | |
112 | end |
|
112 | end | |
113 | end |
|
113 | end | |
114 | end |
|
114 | end | |
115 |
|
115 | |||
116 | # User self-registration |
|
116 | # User self-registration | |
117 | def register |
|
117 | def register | |
118 | (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration] |
|
118 | (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration] | |
119 | if request.get? |
|
119 | if request.get? | |
120 | session[:auth_source_registration] = nil |
|
120 | session[:auth_source_registration] = nil | |
121 | @user = User.new(:language => current_language.to_s) |
|
121 | @user = User.new(:language => current_language.to_s) | |
122 | else |
|
122 | else | |
123 | user_params = params[:user] || {} |
|
123 | user_params = params[:user] || {} | |
124 | @user = User.new |
|
124 | @user = User.new | |
125 | @user.safe_attributes = user_params |
|
125 | @user.safe_attributes = user_params | |
126 | @user.pref.attributes = params[:pref] if params[:pref] |
|
126 | @user.pref.attributes = params[:pref] if params[:pref] | |
127 | @user.admin = false |
|
127 | @user.admin = false | |
128 | @user.register |
|
128 | @user.register | |
129 | if session[:auth_source_registration] |
|
129 | if session[:auth_source_registration] | |
130 | @user.activate |
|
130 | @user.activate | |
131 | @user.login = session[:auth_source_registration][:login] |
|
131 | @user.login = session[:auth_source_registration][:login] | |
132 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
|
132 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] | |
133 | if @user.save |
|
133 | if @user.save | |
134 | session[:auth_source_registration] = nil |
|
134 | session[:auth_source_registration] = nil | |
135 | self.logged_user = @user |
|
135 | self.logged_user = @user | |
136 | flash[:notice] = l(:notice_account_activated) |
|
136 | flash[:notice] = l(:notice_account_activated) | |
137 | redirect_to my_account_path |
|
137 | redirect_to my_account_path | |
138 | end |
|
138 | end | |
139 | else |
|
139 | else | |
140 | @user.login = params[:user][:login] |
|
140 | @user.login = params[:user][:login] | |
141 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? |
|
141 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? | |
142 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] |
|
142 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] | |
143 | end |
|
143 | end | |
144 |
|
144 | |||
145 | case Setting.self_registration |
|
145 | case Setting.self_registration | |
146 | when '1' |
|
146 | when '1' | |
147 | register_by_email_activation(@user) |
|
147 | register_by_email_activation(@user) | |
148 | when '3' |
|
148 | when '3' | |
149 | register_automatically(@user) |
|
149 | register_automatically(@user) | |
150 | else |
|
150 | else | |
151 | register_manually_by_administrator(@user) |
|
151 | register_manually_by_administrator(@user) | |
152 | end |
|
152 | end | |
153 | end |
|
153 | end | |
154 | end |
|
154 | end | |
155 | end |
|
155 | end | |
156 |
|
156 | |||
157 | # Token based account activation |
|
157 | # Token based account activation | |
158 | def activate |
|
158 | def activate | |
159 | (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present? |
|
159 | (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present? | |
160 | token = Token.find_token('register', params[:token].to_s) |
|
160 | token = Token.find_token('register', params[:token].to_s) | |
161 | (redirect_to(home_url); return) unless token and !token.expired? |
|
161 | (redirect_to(home_url); return) unless token and !token.expired? | |
162 | user = token.user |
|
162 | user = token.user | |
163 | (redirect_to(home_url); return) unless user.registered? |
|
163 | (redirect_to(home_url); return) unless user.registered? | |
164 | user.activate |
|
164 | user.activate | |
165 | if user.save |
|
165 | if user.save | |
166 | token.destroy |
|
166 | token.destroy | |
167 | flash[:notice] = l(:notice_account_activated) |
|
167 | flash[:notice] = l(:notice_account_activated) | |
168 | end |
|
168 | end | |
169 | redirect_to signin_path |
|
169 | redirect_to signin_path | |
170 | end |
|
170 | end | |
171 |
|
171 | |||
172 | # Sends a new account activation email |
|
172 | # Sends a new account activation email | |
173 | def activation_email |
|
173 | def activation_email | |
174 | if session[:registered_user_id] && Setting.self_registration == '1' |
|
174 | if session[:registered_user_id] && Setting.self_registration == '1' | |
175 | user_id = session.delete(:registered_user_id).to_i |
|
175 | user_id = session.delete(:registered_user_id).to_i | |
176 | user = User.find_by_id(user_id) |
|
176 | user = User.find_by_id(user_id) | |
177 | if user && user.registered? |
|
177 | if user && user.registered? | |
178 | register_by_email_activation(user) |
|
178 | register_by_email_activation(user) | |
179 | return |
|
179 | return | |
180 | end |
|
180 | end | |
181 | end |
|
181 | end | |
182 | redirect_to(home_url) |
|
182 | redirect_to(home_url) | |
183 | end |
|
183 | end | |
184 |
|
184 | |||
185 | private |
|
185 | private | |
186 |
|
186 | |||
187 | def authenticate_user |
|
187 | def authenticate_user | |
188 | if Setting.openid? && using_open_id? |
|
188 | if Setting.openid? && using_open_id? | |
189 | open_id_authenticate(params[:openid_url]) |
|
189 | open_id_authenticate(params[:openid_url]) | |
190 | else |
|
190 | else | |
191 | password_authentication |
|
191 | password_authentication | |
192 | end |
|
192 | end | |
193 | end |
|
193 | end | |
194 |
|
194 | |||
195 | def password_authentication |
|
195 | def password_authentication | |
196 | user = User.try_to_login(params[:username], params[:password], false) |
|
196 | user = User.try_to_login(params[:username], params[:password], false) | |
197 |
|
197 | |||
198 | if user.nil? |
|
198 | if user.nil? | |
199 | invalid_credentials |
|
199 | invalid_credentials | |
200 | elsif user.new_record? |
|
200 | elsif user.new_record? | |
201 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) |
|
201 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) | |
202 | else |
|
202 | else | |
203 | # Valid user |
|
203 | # Valid user | |
204 | if user.active? |
|
204 | if user.active? | |
205 | successful_authentication(user) |
|
205 | successful_authentication(user) | |
206 | update_sudo_timestamp! # activate Sudo Mode |
|
206 | update_sudo_timestamp! # activate Sudo Mode | |
207 | else |
|
207 | else | |
208 | handle_inactive_user(user) |
|
208 | handle_inactive_user(user) | |
209 | end |
|
209 | end | |
210 | end |
|
210 | end | |
211 | end |
|
211 | end | |
212 |
|
212 | |||
213 | def open_id_authenticate(openid_url) |
|
213 | def open_id_authenticate(openid_url) | |
214 | back_url = signin_url(:autologin => params[:autologin]) |
|
214 | back_url = signin_url(:autologin => params[:autologin]) | |
215 | authenticate_with_open_id( |
|
215 | authenticate_with_open_id( | |
216 | openid_url, :required => [:nickname, :fullname, :email], |
|
216 | openid_url, :required => [:nickname, :fullname, :email], | |
217 | :return_to => back_url, :method => :post |
|
217 | :return_to => back_url, :method => :post | |
218 | ) do |result, identity_url, registration| |
|
218 | ) do |result, identity_url, registration| | |
219 | if result.successful? |
|
219 | if result.successful? | |
220 | user = User.find_or_initialize_by_identity_url(identity_url) |
|
220 | user = User.find_or_initialize_by_identity_url(identity_url) | |
221 | if user.new_record? |
|
221 | if user.new_record? | |
222 | # Self-registration off |
|
222 | # Self-registration off | |
223 | (redirect_to(home_url); return) unless Setting.self_registration? |
|
223 | (redirect_to(home_url); return) unless Setting.self_registration? | |
224 | # Create on the fly |
|
224 | # Create on the fly | |
225 | user.login = registration['nickname'] unless registration['nickname'].nil? |
|
225 | user.login = registration['nickname'] unless registration['nickname'].nil? | |
226 | user.mail = registration['email'] unless registration['email'].nil? |
|
226 | user.mail = registration['email'] unless registration['email'].nil? | |
227 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
|
227 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? | |
228 | user.random_password |
|
228 | user.random_password | |
229 | user.register |
|
229 | user.register | |
230 | case Setting.self_registration |
|
230 | case Setting.self_registration | |
231 | when '1' |
|
231 | when '1' | |
232 | register_by_email_activation(user) do |
|
232 | register_by_email_activation(user) do | |
233 | onthefly_creation_failed(user) |
|
233 | onthefly_creation_failed(user) | |
234 | end |
|
234 | end | |
235 | when '3' |
|
235 | when '3' | |
236 | register_automatically(user) do |
|
236 | register_automatically(user) do | |
237 | onthefly_creation_failed(user) |
|
237 | onthefly_creation_failed(user) | |
238 | end |
|
238 | end | |
239 | else |
|
239 | else | |
240 | register_manually_by_administrator(user) do |
|
240 | register_manually_by_administrator(user) do | |
241 | onthefly_creation_failed(user) |
|
241 | onthefly_creation_failed(user) | |
242 | end |
|
242 | end | |
243 | end |
|
243 | end | |
244 | else |
|
244 | else | |
245 | # Existing record |
|
245 | # Existing record | |
246 | if user.active? |
|
246 | if user.active? | |
247 | successful_authentication(user) |
|
247 | successful_authentication(user) | |
248 | else |
|
248 | else | |
249 | handle_inactive_user(user) |
|
249 | handle_inactive_user(user) | |
250 | end |
|
250 | end | |
251 | end |
|
251 | end | |
252 | end |
|
252 | end | |
253 | end |
|
253 | end | |
254 | end |
|
254 | end | |
255 |
|
255 | |||
256 | def successful_authentication(user) |
|
256 | def successful_authentication(user) | |
257 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" |
|
257 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" | |
258 | # Valid user |
|
258 | # Valid user | |
259 | self.logged_user = user |
|
259 | self.logged_user = user | |
260 | # generate a key and set cookie if autologin |
|
260 | # generate a key and set cookie if autologin | |
261 | if params[:autologin] && Setting.autologin? |
|
261 | if params[:autologin] && Setting.autologin? | |
262 | set_autologin_cookie(user) |
|
262 | set_autologin_cookie(user) | |
263 | end |
|
263 | end | |
264 | call_hook(:controller_account_success_authentication_after, {:user => user }) |
|
264 | call_hook(:controller_account_success_authentication_after, {:user => user }) | |
265 | redirect_back_or_default my_page_path |
|
265 | redirect_back_or_default my_page_path | |
266 | end |
|
266 | end | |
267 |
|
267 | |||
268 | def set_autologin_cookie(user) |
|
268 | def set_autologin_cookie(user) | |
269 | token = Token.create(:user => user, :action => 'autologin') |
|
269 | token = Token.create(:user => user, :action => 'autologin') | |
270 | secure = Redmine::Configuration['autologin_cookie_secure'] |
|
270 | secure = Redmine::Configuration['autologin_cookie_secure'] | |
271 | if secure.nil? |
|
271 | if secure.nil? | |
272 | secure = request.ssl? |
|
272 | secure = request.ssl? | |
273 | end |
|
273 | end | |
274 | cookie_options = { |
|
274 | cookie_options = { | |
275 | :value => token.value, |
|
275 | :value => token.value, | |
276 | :expires => 1.year.from_now, |
|
276 | :expires => 1.year.from_now, | |
277 | :path => (Redmine::Configuration['autologin_cookie_path'] || RedmineApp::Application.config.relative_url_root || '/'), |
|
277 | :path => (Redmine::Configuration['autologin_cookie_path'] || RedmineApp::Application.config.relative_url_root || '/'), | |
278 | :secure => secure, |
|
278 | :secure => secure, | |
279 | :httponly => true |
|
279 | :httponly => true | |
280 | } |
|
280 | } | |
281 | cookies[autologin_cookie_name] = cookie_options |
|
281 | cookies[autologin_cookie_name] = cookie_options | |
282 | end |
|
282 | end | |
283 |
|
283 | |||
284 | # Onthefly creation failed, display the registration form to fill/fix attributes |
|
284 | # Onthefly creation failed, display the registration form to fill/fix attributes | |
285 | def onthefly_creation_failed(user, auth_source_options = { }) |
|
285 | def onthefly_creation_failed(user, auth_source_options = { }) | |
286 | @user = user |
|
286 | @user = user | |
287 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? |
|
287 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? | |
288 | render :action => 'register' |
|
288 | render :action => 'register' | |
289 | end |
|
289 | end | |
290 |
|
290 | |||
291 | def invalid_credentials |
|
291 | def invalid_credentials | |
292 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" |
|
292 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" | |
293 | flash.now[:error] = l(:notice_account_invalid_credentials) |
|
293 | flash.now[:error] = l(:notice_account_invalid_credentials) | |
294 | end |
|
294 | end | |
295 |
|
295 | |||
296 | # Register a user for email activation. |
|
296 | # Register a user for email activation. | |
297 | # |
|
297 | # | |
298 | # Pass a block for behavior when a user fails to save |
|
298 | # Pass a block for behavior when a user fails to save | |
299 | def register_by_email_activation(user, &block) |
|
299 | def register_by_email_activation(user, &block) | |
300 | token = Token.new(:user => user, :action => "register") |
|
300 | token = Token.new(:user => user, :action => "register") | |
301 | if user.save and token.save |
|
301 | if user.save and token.save | |
302 | Mailer.register(token).deliver |
|
302 | Mailer.register(token).deliver | |
303 | flash[:notice] = l(:notice_account_register_done, :email => ERB::Util.h(user.mail)) |
|
303 | flash[:notice] = l(:notice_account_register_done, :email => ERB::Util.h(user.mail)) | |
304 | redirect_to signin_path |
|
304 | redirect_to signin_path | |
305 | else |
|
305 | else | |
306 | yield if block_given? |
|
306 | yield if block_given? | |
307 | end |
|
307 | end | |
308 | end |
|
308 | end | |
309 |
|
309 | |||
310 | # Automatically register a user |
|
310 | # Automatically register a user | |
311 | # |
|
311 | # | |
312 | # Pass a block for behavior when a user fails to save |
|
312 | # Pass a block for behavior when a user fails to save | |
313 | def register_automatically(user, &block) |
|
313 | def register_automatically(user, &block) | |
314 | # Automatic activation |
|
314 | # Automatic activation | |
315 | user.activate |
|
315 | user.activate | |
316 | user.last_login_on = Time.now |
|
316 | user.last_login_on = Time.now | |
317 | if user.save |
|
317 | if user.save | |
318 | self.logged_user = user |
|
318 | self.logged_user = user | |
319 | flash[:notice] = l(:notice_account_activated) |
|
319 | flash[:notice] = l(:notice_account_activated) | |
320 | redirect_to my_account_path |
|
320 | redirect_to my_account_path | |
321 | else |
|
321 | else | |
322 | yield if block_given? |
|
322 | yield if block_given? | |
323 | end |
|
323 | end | |
324 | end |
|
324 | end | |
325 |
|
325 | |||
326 | # Manual activation by the administrator |
|
326 | # Manual activation by the administrator | |
327 | # |
|
327 | # | |
328 | # Pass a block for behavior when a user fails to save |
|
328 | # Pass a block for behavior when a user fails to save | |
329 | def register_manually_by_administrator(user, &block) |
|
329 | def register_manually_by_administrator(user, &block) | |
330 | if user.save |
|
330 | if user.save | |
331 | # Sends an email to the administrators |
|
331 | # Sends an email to the administrators | |
332 | Mailer.account_activation_request(user).deliver |
|
332 | Mailer.account_activation_request(user).deliver | |
333 | account_pending(user) |
|
333 | account_pending(user) | |
334 | else |
|
334 | else | |
335 | yield if block_given? |
|
335 | yield if block_given? | |
336 | end |
|
336 | end | |
337 | end |
|
337 | end | |
338 |
|
338 | |||
339 | def handle_inactive_user(user, redirect_path=signin_path) |
|
339 | def handle_inactive_user(user, redirect_path=signin_path) | |
340 | if user.registered? |
|
340 | if user.registered? | |
341 | account_pending(user, redirect_path) |
|
341 | account_pending(user, redirect_path) | |
342 | else |
|
342 | else | |
343 | account_locked(user, redirect_path) |
|
343 | account_locked(user, redirect_path) | |
344 | end |
|
344 | end | |
345 | end |
|
345 | end | |
346 |
|
346 | |||
347 | def account_pending(user, redirect_path=signin_path) |
|
347 | def account_pending(user, redirect_path=signin_path) | |
348 | if Setting.self_registration == '1' |
|
348 | if Setting.self_registration == '1' | |
349 | flash[:error] = l(:notice_account_not_activated_yet, :url => activation_email_path) |
|
349 | flash[:error] = l(:notice_account_not_activated_yet, :url => activation_email_path) | |
350 | session[:registered_user_id] = user.id |
|
350 | session[:registered_user_id] = user.id | |
351 | else |
|
351 | else | |
352 | flash[:error] = l(:notice_account_pending) |
|
352 | flash[:error] = l(:notice_account_pending) | |
353 | end |
|
353 | end | |
354 | redirect_to redirect_path |
|
354 | redirect_to redirect_path | |
355 | end |
|
355 | end | |
356 |
|
356 | |||
357 | def account_locked(user, redirect_path=signin_path) |
|
357 | def account_locked(user, redirect_path=signin_path) | |
358 | flash[:error] = l(:notice_account_locked) |
|
358 | flash[:error] = l(:notice_account_locked) | |
359 | redirect_to redirect_path |
|
359 | redirect_to redirect_path | |
360 | end |
|
360 | end | |
361 | end |
|
361 | end |
@@ -1,211 +1,211 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 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 MyController < ApplicationController |
|
18 | class MyController < ApplicationController | |
19 | before_action :require_login |
|
19 | before_action :require_login | |
20 | # let user change user's password when user has to |
|
20 | # let user change user's password when user has to | |
21 |
skip_before_ |
|
21 | skip_before_action :check_password_change, :only => :password | |
22 |
|
22 | |||
23 | require_sudo_mode :account, only: :post |
|
23 | require_sudo_mode :account, only: :post | |
24 | require_sudo_mode :reset_rss_key, :reset_api_key, :show_api_key, :destroy |
|
24 | require_sudo_mode :reset_rss_key, :reset_api_key, :show_api_key, :destroy | |
25 |
|
25 | |||
26 | helper :issues |
|
26 | helper :issues | |
27 | helper :users |
|
27 | helper :users | |
28 | helper :custom_fields |
|
28 | helper :custom_fields | |
29 |
|
29 | |||
30 | BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues, |
|
30 | BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues, | |
31 | 'issuesreportedbyme' => :label_reported_issues, |
|
31 | 'issuesreportedbyme' => :label_reported_issues, | |
32 | 'issueswatched' => :label_watched_issues, |
|
32 | 'issueswatched' => :label_watched_issues, | |
33 | 'news' => :label_news_latest, |
|
33 | 'news' => :label_news_latest, | |
34 | 'calendar' => :label_calendar, |
|
34 | 'calendar' => :label_calendar, | |
35 | 'documents' => :label_document_plural, |
|
35 | 'documents' => :label_document_plural, | |
36 | 'timelog' => :label_spent_time |
|
36 | 'timelog' => :label_spent_time | |
37 | }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze |
|
37 | }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze | |
38 |
|
38 | |||
39 | DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'], |
|
39 | DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'], | |
40 | 'right' => ['issuesreportedbyme'] |
|
40 | 'right' => ['issuesreportedbyme'] | |
41 | }.freeze |
|
41 | }.freeze | |
42 |
|
42 | |||
43 | def index |
|
43 | def index | |
44 | page |
|
44 | page | |
45 | render :action => 'page' |
|
45 | render :action => 'page' | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | # Show user's page |
|
48 | # Show user's page | |
49 | def page |
|
49 | def page | |
50 | @user = User.current |
|
50 | @user = User.current | |
51 | @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT |
|
51 | @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
54 | # Edit user's account |
|
54 | # Edit user's account | |
55 | def account |
|
55 | def account | |
56 | @user = User.current |
|
56 | @user = User.current | |
57 | @pref = @user.pref |
|
57 | @pref = @user.pref | |
58 | if request.post? |
|
58 | if request.post? | |
59 | @user.safe_attributes = params[:user] if params[:user] |
|
59 | @user.safe_attributes = params[:user] if params[:user] | |
60 | @user.pref.attributes = params[:pref] if params[:pref] |
|
60 | @user.pref.attributes = params[:pref] if params[:pref] | |
61 | if @user.save |
|
61 | if @user.save | |
62 | @user.pref.save |
|
62 | @user.pref.save | |
63 | set_language_if_valid @user.language |
|
63 | set_language_if_valid @user.language | |
64 | flash[:notice] = l(:notice_account_updated) |
|
64 | flash[:notice] = l(:notice_account_updated) | |
65 | redirect_to my_account_path |
|
65 | redirect_to my_account_path | |
66 | return |
|
66 | return | |
67 | end |
|
67 | end | |
68 | end |
|
68 | end | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | # Destroys user's account |
|
71 | # Destroys user's account | |
72 | def destroy |
|
72 | def destroy | |
73 | @user = User.current |
|
73 | @user = User.current | |
74 | unless @user.own_account_deletable? |
|
74 | unless @user.own_account_deletable? | |
75 | redirect_to my_account_path |
|
75 | redirect_to my_account_path | |
76 | return |
|
76 | return | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | if request.post? && params[:confirm] |
|
79 | if request.post? && params[:confirm] | |
80 | @user.destroy |
|
80 | @user.destroy | |
81 | if @user.destroyed? |
|
81 | if @user.destroyed? | |
82 | logout_user |
|
82 | logout_user | |
83 | flash[:notice] = l(:notice_account_deleted) |
|
83 | flash[:notice] = l(:notice_account_deleted) | |
84 | end |
|
84 | end | |
85 | redirect_to home_path |
|
85 | redirect_to home_path | |
86 | end |
|
86 | end | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | # Manage user's password |
|
89 | # Manage user's password | |
90 | def password |
|
90 | def password | |
91 | @user = User.current |
|
91 | @user = User.current | |
92 | unless @user.change_password_allowed? |
|
92 | unless @user.change_password_allowed? | |
93 | flash[:error] = l(:notice_can_t_change_password) |
|
93 | flash[:error] = l(:notice_can_t_change_password) | |
94 | redirect_to my_account_path |
|
94 | redirect_to my_account_path | |
95 | return |
|
95 | return | |
96 | end |
|
96 | end | |
97 | if request.post? |
|
97 | if request.post? | |
98 | if !@user.check_password?(params[:password]) |
|
98 | if !@user.check_password?(params[:password]) | |
99 | flash.now[:error] = l(:notice_account_wrong_password) |
|
99 | flash.now[:error] = l(:notice_account_wrong_password) | |
100 | elsif params[:password] == params[:new_password] |
|
100 | elsif params[:password] == params[:new_password] | |
101 | flash.now[:error] = l(:notice_new_password_must_be_different) |
|
101 | flash.now[:error] = l(:notice_new_password_must_be_different) | |
102 | else |
|
102 | else | |
103 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
103 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | |
104 | @user.must_change_passwd = false |
|
104 | @user.must_change_passwd = false | |
105 | if @user.save |
|
105 | if @user.save | |
106 | # The session token was destroyed by the password change, generate a new one |
|
106 | # The session token was destroyed by the password change, generate a new one | |
107 | session[:tk] = @user.generate_session_token |
|
107 | session[:tk] = @user.generate_session_token | |
108 | Mailer.password_updated(@user) |
|
108 | Mailer.password_updated(@user) | |
109 | flash[:notice] = l(:notice_account_password_updated) |
|
109 | flash[:notice] = l(:notice_account_password_updated) | |
110 | redirect_to my_account_path |
|
110 | redirect_to my_account_path | |
111 | end |
|
111 | end | |
112 | end |
|
112 | end | |
113 | end |
|
113 | end | |
114 | end |
|
114 | end | |
115 |
|
115 | |||
116 | # Create a new feeds key |
|
116 | # Create a new feeds key | |
117 | def reset_rss_key |
|
117 | def reset_rss_key | |
118 | if request.post? |
|
118 | if request.post? | |
119 | if User.current.rss_token |
|
119 | if User.current.rss_token | |
120 | User.current.rss_token.destroy |
|
120 | User.current.rss_token.destroy | |
121 | User.current.reload |
|
121 | User.current.reload | |
122 | end |
|
122 | end | |
123 | User.current.rss_key |
|
123 | User.current.rss_key | |
124 | flash[:notice] = l(:notice_feeds_access_key_reseted) |
|
124 | flash[:notice] = l(:notice_feeds_access_key_reseted) | |
125 | end |
|
125 | end | |
126 | redirect_to my_account_path |
|
126 | redirect_to my_account_path | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | def show_api_key |
|
129 | def show_api_key | |
130 | @user = User.current |
|
130 | @user = User.current | |
131 | end |
|
131 | end | |
132 |
|
132 | |||
133 | # Create a new API key |
|
133 | # Create a new API key | |
134 | def reset_api_key |
|
134 | def reset_api_key | |
135 | if request.post? |
|
135 | if request.post? | |
136 | if User.current.api_token |
|
136 | if User.current.api_token | |
137 | User.current.api_token.destroy |
|
137 | User.current.api_token.destroy | |
138 | User.current.reload |
|
138 | User.current.reload | |
139 | end |
|
139 | end | |
140 | User.current.api_key |
|
140 | User.current.api_key | |
141 | flash[:notice] = l(:notice_api_access_key_reseted) |
|
141 | flash[:notice] = l(:notice_api_access_key_reseted) | |
142 | end |
|
142 | end | |
143 | redirect_to my_account_path |
|
143 | redirect_to my_account_path | |
144 | end |
|
144 | end | |
145 |
|
145 | |||
146 | # User's page layout configuration |
|
146 | # User's page layout configuration | |
147 | def page_layout |
|
147 | def page_layout | |
148 | @user = User.current |
|
148 | @user = User.current | |
149 | @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup |
|
149 | @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup | |
150 | @block_options = [] |
|
150 | @block_options = [] | |
151 | BLOCKS.each do |k, v| |
|
151 | BLOCKS.each do |k, v| | |
152 | unless @blocks.values.flatten.include?(k) |
|
152 | unless @blocks.values.flatten.include?(k) | |
153 | @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize] |
|
153 | @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize] | |
154 | end |
|
154 | end | |
155 | end |
|
155 | end | |
156 | end |
|
156 | end | |
157 |
|
157 | |||
158 | # Add a block to user's page |
|
158 | # Add a block to user's page | |
159 | # The block is added on top of the page |
|
159 | # The block is added on top of the page | |
160 | # params[:block] : id of the block to add |
|
160 | # params[:block] : id of the block to add | |
161 | def add_block |
|
161 | def add_block | |
162 | block = params[:block].to_s.underscore |
|
162 | block = params[:block].to_s.underscore | |
163 | if block.present? && BLOCKS.key?(block) |
|
163 | if block.present? && BLOCKS.key?(block) | |
164 | @user = User.current |
|
164 | @user = User.current | |
165 | layout = @user.pref[:my_page_layout] || {} |
|
165 | layout = @user.pref[:my_page_layout] || {} | |
166 | # remove if already present in a group |
|
166 | # remove if already present in a group | |
167 | %w(top left right).each {|f| (layout[f] ||= []).delete block } |
|
167 | %w(top left right).each {|f| (layout[f] ||= []).delete block } | |
168 | # add it on top |
|
168 | # add it on top | |
169 | layout['top'].unshift block |
|
169 | layout['top'].unshift block | |
170 | @user.pref[:my_page_layout] = layout |
|
170 | @user.pref[:my_page_layout] = layout | |
171 | @user.pref.save |
|
171 | @user.pref.save | |
172 | end |
|
172 | end | |
173 | redirect_to my_page_layout_path |
|
173 | redirect_to my_page_layout_path | |
174 | end |
|
174 | end | |
175 |
|
175 | |||
176 | # Remove a block to user's page |
|
176 | # Remove a block to user's page | |
177 | # params[:block] : id of the block to remove |
|
177 | # params[:block] : id of the block to remove | |
178 | def remove_block |
|
178 | def remove_block | |
179 | block = params[:block].to_s.underscore |
|
179 | block = params[:block].to_s.underscore | |
180 | @user = User.current |
|
180 | @user = User.current | |
181 | # remove block in all groups |
|
181 | # remove block in all groups | |
182 | layout = @user.pref[:my_page_layout] || {} |
|
182 | layout = @user.pref[:my_page_layout] || {} | |
183 | %w(top left right).each {|f| (layout[f] ||= []).delete block } |
|
183 | %w(top left right).each {|f| (layout[f] ||= []).delete block } | |
184 | @user.pref[:my_page_layout] = layout |
|
184 | @user.pref[:my_page_layout] = layout | |
185 | @user.pref.save |
|
185 | @user.pref.save | |
186 | redirect_to my_page_layout_path |
|
186 | redirect_to my_page_layout_path | |
187 | end |
|
187 | end | |
188 |
|
188 | |||
189 | # Change blocks order on user's page |
|
189 | # Change blocks order on user's page | |
190 | # params[:group] : group to order (top, left or right) |
|
190 | # params[:group] : group to order (top, left or right) | |
191 | # params[:list-(top|left|right)] : array of block ids of the group |
|
191 | # params[:list-(top|left|right)] : array of block ids of the group | |
192 | def order_blocks |
|
192 | def order_blocks | |
193 | group = params[:group] |
|
193 | group = params[:group] | |
194 | @user = User.current |
|
194 | @user = User.current | |
195 | if group.is_a?(String) |
|
195 | if group.is_a?(String) | |
196 | group_items = (params["blocks"] || []).collect(&:underscore) |
|
196 | group_items = (params["blocks"] || []).collect(&:underscore) | |
197 | group_items.each {|s| s.sub!(/^block_/, '')} |
|
197 | group_items.each {|s| s.sub!(/^block_/, '')} | |
198 | if group_items and group_items.is_a? Array |
|
198 | if group_items and group_items.is_a? Array | |
199 | layout = @user.pref[:my_page_layout] || {} |
|
199 | layout = @user.pref[:my_page_layout] || {} | |
200 | # remove group blocks if they are presents in other groups |
|
200 | # remove group blocks if they are presents in other groups | |
201 | %w(top left right).each {|f| |
|
201 | %w(top left right).each {|f| | |
202 | layout[f] = (layout[f] || []) - group_items |
|
202 | layout[f] = (layout[f] || []) - group_items | |
203 | } |
|
203 | } | |
204 | layout[group] = group_items |
|
204 | layout[group] = group_items | |
205 | @user.pref[:my_page_layout] = layout |
|
205 | @user.pref[:my_page_layout] = layout | |
206 | @user.pref.save |
|
206 | @user.pref.save | |
207 | end |
|
207 | end | |
208 | end |
|
208 | end | |
209 | render :nothing => true |
|
209 | render :nothing => true | |
210 | end |
|
210 | end | |
211 | end |
|
211 | end |
General Comments 0
You need to be logged in to leave comments.
Login now