@@ -1,351 +1,351 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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_filter :check_if_login_required, :check_password_change |
|
23 | skip_before_filter :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_t |
|
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 | flash[:notice] = l(:notice_account_password_updated) |
|
76 | flash[:notice] = l(:notice_account_password_updated) | |
77 | redirect_to signin_path |
|
77 | redirect_to signin_path | |
78 | return |
|
78 | return | |
79 | end |
|
79 | end | |
80 | end |
|
80 | end | |
81 | render :template => "account/password_recovery" |
|
81 | render :template => "account/password_recovery" | |
82 | return |
|
82 | return | |
83 | else |
|
83 | else | |
84 | if request.post? |
|
84 | if request.post? | |
85 | user = User.find_by_mail(params[:mail].to_s) |
|
85 | user = User.find_by_mail(params[:mail].to_s) | |
86 | # user not found |
|
86 | # user not found | |
87 | unless user |
|
87 | unless user | |
88 | flash.now[:error] = l(:notice_account_unknown_email) |
|
88 | flash.now[:error] = l(:notice_account_unknown_email) | |
89 | return |
|
89 | return | |
90 | end |
|
90 | end | |
91 | unless user.active? |
|
91 | unless user.active? | |
92 | handle_inactive_user(user, lost_password_path) |
|
92 | handle_inactive_user(user, lost_password_path) | |
93 | return |
|
93 | return | |
94 | end |
|
94 | end | |
95 | # user cannot change its password |
|
95 | # user cannot change its password | |
96 | unless user.change_password_allowed? |
|
96 | unless user.change_password_allowed? | |
97 | flash.now[:error] = l(:notice_can_t_change_password) |
|
97 | flash.now[:error] = l(:notice_can_t_change_password) | |
98 | return |
|
98 | return | |
99 | end |
|
99 | end | |
100 | # create a new token for password recovery |
|
100 | # create a new token for password recovery | |
101 | token = Token.new(:user => user, :action => "recovery") |
|
101 | token = Token.new(:user => user, :action => "recovery") | |
102 | if token.save |
|
102 | if token.save | |
103 | Mailer.lost_password(token).deliver |
|
103 | Mailer.lost_password(token).deliver | |
104 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
104 | flash[:notice] = l(:notice_account_lost_email_sent) | |
105 | redirect_to signin_path |
|
105 | redirect_to signin_path | |
106 | return |
|
106 | return | |
107 | end |
|
107 | end | |
108 | end |
|
108 | end | |
109 | end |
|
109 | end | |
110 | end |
|
110 | end | |
111 |
|
111 | |||
112 | # User self-registration |
|
112 | # User self-registration | |
113 | def register |
|
113 | def register | |
114 | (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration] |
|
114 | (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration] | |
115 | if request.get? |
|
115 | if request.get? | |
116 | session[:auth_source_registration] = nil |
|
116 | session[:auth_source_registration] = nil | |
117 | @user = User.new(:language => current_language.to_s) |
|
117 | @user = User.new(:language => current_language.to_s) | |
118 | else |
|
118 | else | |
119 | user_params = params[:user] || {} |
|
119 | user_params = params[:user] || {} | |
120 | @user = User.new |
|
120 | @user = User.new | |
121 | @user.safe_attributes = user_params |
|
121 | @user.safe_attributes = user_params | |
122 | @user.admin = false |
|
122 | @user.admin = false | |
123 | @user.register |
|
123 | @user.register | |
124 | if session[:auth_source_registration] |
|
124 | if session[:auth_source_registration] | |
125 | @user.activate |
|
125 | @user.activate | |
126 | @user.login = session[:auth_source_registration][:login] |
|
126 | @user.login = session[:auth_source_registration][:login] | |
127 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
|
127 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] | |
128 | if @user.save |
|
128 | if @user.save | |
129 | session[:auth_source_registration] = nil |
|
129 | session[:auth_source_registration] = nil | |
130 | self.logged_user = @user |
|
130 | self.logged_user = @user | |
131 | flash[:notice] = l(:notice_account_activated) |
|
131 | flash[:notice] = l(:notice_account_activated) | |
132 | redirect_to my_account_path |
|
132 | redirect_to my_account_path | |
133 | end |
|
133 | end | |
134 | else |
|
134 | else | |
135 | @user.login = params[:user][:login] |
|
135 | @user.login = params[:user][:login] | |
136 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? |
|
136 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? | |
137 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] |
|
137 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] | |
138 | end |
|
138 | end | |
139 |
|
139 | |||
140 | case Setting.self_registration |
|
140 | case Setting.self_registration | |
141 | when '1' |
|
141 | when '1' | |
142 | register_by_email_activation(@user) |
|
142 | register_by_email_activation(@user) | |
143 | when '3' |
|
143 | when '3' | |
144 | register_automatically(@user) |
|
144 | register_automatically(@user) | |
145 | else |
|
145 | else | |
146 | register_manually_by_administrator(@user) |
|
146 | register_manually_by_administrator(@user) | |
147 | end |
|
147 | end | |
148 | end |
|
148 | end | |
149 | end |
|
149 | end | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | # Token based account activation |
|
152 | # Token based account activation | |
153 | def activate |
|
153 | def activate | |
154 | (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present? |
|
154 | (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present? | |
155 | token = Token.find_token('register', params[:token].to_s) |
|
155 | token = Token.find_token('register', params[:token].to_s) | |
156 | (redirect_to(home_url); return) unless token and !token.expired? |
|
156 | (redirect_to(home_url); return) unless token and !token.expired? | |
157 | user = token.user |
|
157 | user = token.user | |
158 | (redirect_to(home_url); return) unless user.registered? |
|
158 | (redirect_to(home_url); return) unless user.registered? | |
159 | user.activate |
|
159 | user.activate | |
160 | if user.save |
|
160 | if user.save | |
161 | token.destroy |
|
161 | token.destroy | |
162 | flash[:notice] = l(:notice_account_activated) |
|
162 | flash[:notice] = l(:notice_account_activated) | |
163 | end |
|
163 | end | |
164 | redirect_to signin_path |
|
164 | redirect_to signin_path | |
165 | end |
|
165 | end | |
166 |
|
166 | |||
167 | # Sends a new account activation email |
|
167 | # Sends a new account activation email | |
168 | def activation_email |
|
168 | def activation_email | |
169 | if session[:registered_user_id] && Setting.self_registration == '1' |
|
169 | if session[:registered_user_id] && Setting.self_registration == '1' | |
170 | user_id = session.delete(:registered_user_id).to_i |
|
170 | user_id = session.delete(:registered_user_id).to_i | |
171 | user = User.find_by_id(user_id) |
|
171 | user = User.find_by_id(user_id) | |
172 | if user && user.registered? |
|
172 | if user && user.registered? | |
173 | register_by_email_activation(user) |
|
173 | register_by_email_activation(user) | |
174 | return |
|
174 | return | |
175 | end |
|
175 | end | |
176 | end |
|
176 | end | |
177 | redirect_to(home_url) |
|
177 | redirect_to(home_url) | |
178 | end |
|
178 | end | |
179 |
|
179 | |||
180 | private |
|
180 | private | |
181 |
|
181 | |||
182 | def authenticate_user |
|
182 | def authenticate_user | |
183 | if Setting.openid? && using_open_id? |
|
183 | if Setting.openid? && using_open_id? | |
184 | open_id_authenticate(params[:openid_url]) |
|
184 | open_id_authenticate(params[:openid_url]) | |
185 | else |
|
185 | else | |
186 | password_authentication |
|
186 | password_authentication | |
187 | end |
|
187 | end | |
188 | end |
|
188 | end | |
189 |
|
189 | |||
190 | def password_authentication |
|
190 | def password_authentication | |
191 | user = User.try_to_login(params[:username], params[:password], false) |
|
191 | user = User.try_to_login(params[:username], params[:password], false) | |
192 |
|
192 | |||
193 | if user.nil? |
|
193 | if user.nil? | |
194 | invalid_credentials |
|
194 | invalid_credentials | |
195 | elsif user.new_record? |
|
195 | elsif user.new_record? | |
196 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) |
|
196 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) | |
197 | else |
|
197 | else | |
198 | # Valid user |
|
198 | # Valid user | |
199 | if user.active? |
|
199 | if user.active? | |
200 | successful_authentication(user) |
|
200 | successful_authentication(user) | |
201 | else |
|
201 | else | |
202 | handle_inactive_user(user) |
|
202 | handle_inactive_user(user) | |
203 | end |
|
203 | end | |
204 | end |
|
204 | end | |
205 | end |
|
205 | end | |
206 |
|
206 | |||
207 | def open_id_authenticate(openid_url) |
|
207 | def open_id_authenticate(openid_url) | |
208 | back_url = signin_url(:autologin => params[:autologin]) |
|
208 | back_url = signin_url(:autologin => params[:autologin]) | |
209 | authenticate_with_open_id( |
|
209 | authenticate_with_open_id( | |
210 | openid_url, :required => [:nickname, :fullname, :email], |
|
210 | openid_url, :required => [:nickname, :fullname, :email], | |
211 | :return_to => back_url, :method => :post |
|
211 | :return_to => back_url, :method => :post | |
212 | ) do |result, identity_url, registration| |
|
212 | ) do |result, identity_url, registration| | |
213 | if result.successful? |
|
213 | if result.successful? | |
214 | user = User.find_or_initialize_by_identity_url(identity_url) |
|
214 | user = User.find_or_initialize_by_identity_url(identity_url) | |
215 | if user.new_record? |
|
215 | if user.new_record? | |
216 | # Self-registration off |
|
216 | # Self-registration off | |
217 | (redirect_to(home_url); return) unless Setting.self_registration? |
|
217 | (redirect_to(home_url); return) unless Setting.self_registration? | |
218 | # Create on the fly |
|
218 | # Create on the fly | |
219 | user.login = registration['nickname'] unless registration['nickname'].nil? |
|
219 | user.login = registration['nickname'] unless registration['nickname'].nil? | |
220 | user.mail = registration['email'] unless registration['email'].nil? |
|
220 | user.mail = registration['email'] unless registration['email'].nil? | |
221 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
|
221 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? | |
222 | user.random_password |
|
222 | user.random_password | |
223 | user.register |
|
223 | user.register | |
224 | case Setting.self_registration |
|
224 | case Setting.self_registration | |
225 | when '1' |
|
225 | when '1' | |
226 | register_by_email_activation(user) do |
|
226 | register_by_email_activation(user) do | |
227 | onthefly_creation_failed(user) |
|
227 | onthefly_creation_failed(user) | |
228 | end |
|
228 | end | |
229 | when '3' |
|
229 | when '3' | |
230 | register_automatically(user) do |
|
230 | register_automatically(user) do | |
231 | onthefly_creation_failed(user) |
|
231 | onthefly_creation_failed(user) | |
232 | end |
|
232 | end | |
233 | else |
|
233 | else | |
234 | register_manually_by_administrator(user) do |
|
234 | register_manually_by_administrator(user) do | |
235 | onthefly_creation_failed(user) |
|
235 | onthefly_creation_failed(user) | |
236 | end |
|
236 | end | |
237 | end |
|
237 | end | |
238 | else |
|
238 | else | |
239 | # Existing record |
|
239 | # Existing record | |
240 | if user.active? |
|
240 | if user.active? | |
241 | successful_authentication(user) |
|
241 | successful_authentication(user) | |
242 | else |
|
242 | else | |
243 | handle_inactive_user(user) |
|
243 | handle_inactive_user(user) | |
244 | end |
|
244 | end | |
245 | end |
|
245 | end | |
246 | end |
|
246 | end | |
247 | end |
|
247 | end | |
248 | end |
|
248 | end | |
249 |
|
249 | |||
250 | def successful_authentication(user) |
|
250 | def successful_authentication(user) | |
251 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" |
|
251 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" | |
252 | # Valid user |
|
252 | # Valid user | |
253 | self.logged_user = user |
|
253 | self.logged_user = user | |
254 | # generate a key and set cookie if autologin |
|
254 | # generate a key and set cookie if autologin | |
255 | if params[:autologin] && Setting.autologin? |
|
255 | if params[:autologin] && Setting.autologin? | |
256 | set_autologin_cookie(user) |
|
256 | set_autologin_cookie(user) | |
257 | end |
|
257 | end | |
258 | call_hook(:controller_account_success_authentication_after, {:user => user }) |
|
258 | call_hook(:controller_account_success_authentication_after, {:user => user }) | |
259 | redirect_back_or_default my_page_path |
|
259 | redirect_back_or_default my_page_path | |
260 | end |
|
260 | end | |
261 |
|
261 | |||
262 | def set_autologin_cookie(user) |
|
262 | def set_autologin_cookie(user) | |
263 | token = Token.create(:user => user, :action => 'autologin') |
|
263 | token = Token.create(:user => user, :action => 'autologin') | |
264 | cookie_options = { |
|
264 | cookie_options = { | |
265 | :value => token.value, |
|
265 | :value => token.value, | |
266 | :expires => 1.year.from_now, |
|
266 | :expires => 1.year.from_now, | |
267 | :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), |
|
267 | :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), | |
268 | :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), |
|
268 | :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), | |
269 | :httponly => true |
|
269 | :httponly => true | |
270 | } |
|
270 | } | |
271 | cookies[autologin_cookie_name] = cookie_options |
|
271 | cookies[autologin_cookie_name] = cookie_options | |
272 | end |
|
272 | end | |
273 |
|
273 | |||
274 | # Onthefly creation failed, display the registration form to fill/fix attributes |
|
274 | # Onthefly creation failed, display the registration form to fill/fix attributes | |
275 | def onthefly_creation_failed(user, auth_source_options = { }) |
|
275 | def onthefly_creation_failed(user, auth_source_options = { }) | |
276 | @user = user |
|
276 | @user = user | |
277 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? |
|
277 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? | |
278 | render :action => 'register' |
|
278 | render :action => 'register' | |
279 | end |
|
279 | end | |
280 |
|
280 | |||
281 | def invalid_credentials |
|
281 | def invalid_credentials | |
282 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" |
|
282 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" | |
283 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
283 | flash.now[:error] = l(:notice_account_invalid_creditentials) | |
284 | end |
|
284 | end | |
285 |
|
285 | |||
286 | # Register a user for email activation. |
|
286 | # Register a user for email activation. | |
287 | # |
|
287 | # | |
288 | # Pass a block for behavior when a user fails to save |
|
288 | # Pass a block for behavior when a user fails to save | |
289 | def register_by_email_activation(user, &block) |
|
289 | def register_by_email_activation(user, &block) | |
290 | token = Token.new(:user => user, :action => "register") |
|
290 | token = Token.new(:user => user, :action => "register") | |
291 | if user.save and token.save |
|
291 | if user.save and token.save | |
292 | Mailer.register(token).deliver |
|
292 | Mailer.register(token).deliver | |
293 | flash[:notice] = l(:notice_account_register_done, :email => user.mail) |
|
293 | flash[:notice] = l(:notice_account_register_done, :email => user.mail) | |
294 | redirect_to signin_path |
|
294 | redirect_to signin_path | |
295 | else |
|
295 | else | |
296 | yield if block_given? |
|
296 | yield if block_given? | |
297 | end |
|
297 | end | |
298 | end |
|
298 | end | |
299 |
|
299 | |||
300 | # Automatically register a user |
|
300 | # Automatically register a user | |
301 | # |
|
301 | # | |
302 | # Pass a block for behavior when a user fails to save |
|
302 | # Pass a block for behavior when a user fails to save | |
303 | def register_automatically(user, &block) |
|
303 | def register_automatically(user, &block) | |
304 | # Automatic activation |
|
304 | # Automatic activation | |
305 | user.activate |
|
305 | user.activate | |
306 | user.last_login_on = Time.now |
|
306 | user.last_login_on = Time.now | |
307 | if user.save |
|
307 | if user.save | |
308 | self.logged_user = user |
|
308 | self.logged_user = user | |
309 | flash[:notice] = l(:notice_account_activated) |
|
309 | flash[:notice] = l(:notice_account_activated) | |
310 | redirect_to my_account_path |
|
310 | redirect_to my_account_path | |
311 | else |
|
311 | else | |
312 | yield if block_given? |
|
312 | yield if block_given? | |
313 | end |
|
313 | end | |
314 | end |
|
314 | end | |
315 |
|
315 | |||
316 | # Manual activation by the administrator |
|
316 | # Manual activation by the administrator | |
317 | # |
|
317 | # | |
318 | # Pass a block for behavior when a user fails to save |
|
318 | # Pass a block for behavior when a user fails to save | |
319 | def register_manually_by_administrator(user, &block) |
|
319 | def register_manually_by_administrator(user, &block) | |
320 | if user.save |
|
320 | if user.save | |
321 | # Sends an email to the administrators |
|
321 | # Sends an email to the administrators | |
322 | Mailer.account_activation_request(user).deliver |
|
322 | Mailer.account_activation_request(user).deliver | |
323 | account_pending(user) |
|
323 | account_pending(user) | |
324 | else |
|
324 | else | |
325 | yield if block_given? |
|
325 | yield if block_given? | |
326 | end |
|
326 | end | |
327 | end |
|
327 | end | |
328 |
|
328 | |||
329 | def handle_inactive_user(user, redirect_path=signin_path) |
|
329 | def handle_inactive_user(user, redirect_path=signin_path) | |
330 | if user.registered? |
|
330 | if user.registered? | |
331 | account_pending(user, redirect_path) |
|
331 | account_pending(user, redirect_path) | |
332 | else |
|
332 | else | |
333 | account_locked(user, redirect_path) |
|
333 | account_locked(user, redirect_path) | |
334 | end |
|
334 | end | |
335 | end |
|
335 | end | |
336 |
|
336 | |||
337 | def account_pending(user, redirect_path=signin_path) |
|
337 | def account_pending(user, redirect_path=signin_path) | |
338 | if Setting.self_registration == '1' |
|
338 | if Setting.self_registration == '1' | |
339 | flash[:error] = l(:notice_account_not_activated_yet, :url => activation_email_path) |
|
339 | flash[:error] = l(:notice_account_not_activated_yet, :url => activation_email_path) | |
340 | session[:registered_user_id] = user.id |
|
340 | session[:registered_user_id] = user.id | |
341 | else |
|
341 | else | |
342 | flash[:error] = l(:notice_account_pending) |
|
342 | flash[:error] = l(:notice_account_pending) | |
343 | end |
|
343 | end | |
344 | redirect_to redirect_path |
|
344 | redirect_to redirect_path | |
345 | end |
|
345 | end | |
346 |
|
346 | |||
347 | def account_locked(user, redirect_path=signin_path) |
|
347 | def account_locked(user, redirect_path=signin_path) | |
348 | flash[:error] = l(:notice_account_locked) |
|
348 | flash[:error] = l(:notice_account_locked) | |
349 | redirect_to redirect_path |
|
349 | redirect_to redirect_path | |
350 | end |
|
350 | end | |
351 | end |
|
351 | end |
@@ -1,617 +1,620 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 | require 'uri' |
|
18 | require 'uri' | |
19 | require 'cgi' |
|
19 | require 'cgi' | |
20 |
|
20 | |||
21 | class Unauthorized < Exception; end |
|
21 | class Unauthorized < Exception; end | |
22 |
|
22 | |||
23 | class ApplicationController < ActionController::Base |
|
23 | class ApplicationController < ActionController::Base | |
24 | include Redmine::I18n |
|
24 | include Redmine::I18n | |
25 | include Redmine::Pagination |
|
25 | include Redmine::Pagination | |
26 | include RoutesHelper |
|
26 | include RoutesHelper | |
27 | helper :routes |
|
27 | helper :routes | |
28 |
|
28 | |||
29 | class_attribute :accept_api_auth_actions |
|
29 | class_attribute :accept_api_auth_actions | |
30 | class_attribute :accept_rss_auth_actions |
|
30 | class_attribute :accept_rss_auth_actions | |
31 | class_attribute :model_object |
|
31 | class_attribute :model_object | |
32 |
|
32 | |||
33 | layout 'base' |
|
33 | layout 'base' | |
34 |
|
34 | |||
35 | protect_from_forgery |
|
35 | protect_from_forgery | |
36 |
|
36 | |||
37 | def verify_authenticity_token |
|
37 | def verify_authenticity_token | |
38 | unless api_request? |
|
38 | unless api_request? | |
39 | super |
|
39 | super | |
40 | end |
|
40 | end | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | def handle_unverified_request |
|
43 | def handle_unverified_request | |
44 | unless api_request? |
|
44 | unless api_request? | |
45 | super |
|
45 | super | |
46 | cookies.delete(autologin_cookie_name) |
|
46 | cookies.delete(autologin_cookie_name) | |
47 | render_error :status => 422, :message => "Invalid form authenticity token." |
|
47 | render_error :status => 422, :message => "Invalid form authenticity token." | |
48 | end |
|
48 | end | |
49 | end |
|
49 | end | |
50 |
|
50 | |||
51 | before_filter :session_expiration, :user_setup, :check_if_login_required, :check_password_change, :set_localization |
|
51 | before_filter :session_expiration, :user_setup, :check_if_login_required, :check_password_change, :set_localization | |
52 |
|
52 | |||
53 | rescue_from ::Unauthorized, :with => :deny_access |
|
53 | rescue_from ::Unauthorized, :with => :deny_access | |
54 | rescue_from ::ActionView::MissingTemplate, :with => :missing_template |
|
54 | rescue_from ::ActionView::MissingTemplate, :with => :missing_template | |
55 |
|
55 | |||
56 | include Redmine::Search::Controller |
|
56 | include Redmine::Search::Controller | |
57 | include Redmine::MenuManager::MenuController |
|
57 | include Redmine::MenuManager::MenuController | |
58 | helper Redmine::MenuManager::MenuHelper |
|
58 | helper Redmine::MenuManager::MenuHelper | |
59 |
|
59 | |||
60 | def session_expiration |
|
60 | def session_expiration | |
61 | if session[:user_id] |
|
61 | if session[:user_id] | |
62 | if session_expired? && !try_to_autologin |
|
62 | if session_expired? && !try_to_autologin | |
63 | reset_session |
|
63 | reset_session | |
64 | flash[:error] = l(:error_session_expired) |
|
64 | flash[:error] = l(:error_session_expired) | |
65 | redirect_to signin_url |
|
65 | redirect_to signin_url | |
66 | else |
|
66 | else | |
67 | session[:atime] = Time.now.utc.to_i |
|
67 | session[:atime] = Time.now.utc.to_i | |
68 | end |
|
68 | end | |
69 | end |
|
69 | end | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def session_expired? |
|
72 | def session_expired? | |
73 | if Setting.session_lifetime? |
|
73 | if Setting.session_lifetime? | |
74 | unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60) |
|
74 | unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60) | |
75 | return true |
|
75 | return true | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end | |
78 | if Setting.session_timeout? |
|
78 | if Setting.session_timeout? | |
79 | unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60) |
|
79 | unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60) | |
80 | return true |
|
80 | return true | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 | false |
|
83 | false | |
84 | end |
|
84 | end | |
85 |
|
85 | |||
86 | def start_user_session(user) |
|
86 | def start_user_session(user) | |
87 | session[:user_id] = user.id |
|
87 | session[:user_id] = user.id | |
88 | session[:ctime] = Time.now.utc.to_i |
|
88 | session[:ctime] = Time.now.utc.to_i | |
89 | session[:atime] = Time.now.utc.to_i |
|
89 | session[:atime] = Time.now.utc.to_i | |
90 | if user.must_change_password? |
|
90 | if user.must_change_password? | |
91 | session[:pwd] = '1' |
|
91 | session[:pwd] = '1' | |
92 | end |
|
92 | end | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 | def user_setup |
|
95 | def user_setup | |
96 | # Check the settings cache for each request |
|
96 | # Check the settings cache for each request | |
97 | Setting.check_cache |
|
97 | Setting.check_cache | |
98 | # Find the current user |
|
98 | # Find the current user | |
99 | User.current = find_current_user |
|
99 | User.current = find_current_user | |
100 | logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger |
|
100 | logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | # Returns the current user or nil if no user is logged in |
|
103 | # Returns the current user or nil if no user is logged in | |
104 | # and starts a session if needed |
|
104 | # and starts a session if needed | |
105 | def find_current_user |
|
105 | def find_current_user | |
106 | user = nil |
|
106 | user = nil | |
107 | unless api_request? |
|
107 | unless api_request? | |
108 | if session[:user_id] |
|
108 | if session[:user_id] | |
109 | # existing session |
|
109 | # existing session | |
110 | user = (User.active.find(session[:user_id]) rescue nil) |
|
110 | user = (User.active.find(session[:user_id]) rescue nil) | |
111 | elsif autologin_user = try_to_autologin |
|
111 | elsif autologin_user = try_to_autologin | |
112 | user = autologin_user |
|
112 | user = autologin_user | |
113 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? |
|
113 | elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? | |
114 | # RSS key authentication does not start a session |
|
114 | # RSS key authentication does not start a session | |
115 | user = User.find_by_rss_key(params[:key]) |
|
115 | user = User.find_by_rss_key(params[:key]) | |
116 | end |
|
116 | end | |
117 | end |
|
117 | end | |
118 | if user.nil? && Setting.rest_api_enabled? && accept_api_auth? |
|
118 | if user.nil? && Setting.rest_api_enabled? && accept_api_auth? | |
119 | if (key = api_key_from_request) |
|
119 | if (key = api_key_from_request) | |
120 | # Use API key |
|
120 | # Use API key | |
121 | user = User.find_by_api_key(key) |
|
121 | user = User.find_by_api_key(key) | |
122 | else |
|
122 | else | |
123 | # HTTP Basic, either username/password or API key/random |
|
123 | # HTTP Basic, either username/password or API key/random | |
124 | authenticate_with_http_basic do |username, password| |
|
124 | authenticate_with_http_basic do |username, password| | |
125 | user = User.try_to_login(username, password) || User.find_by_api_key(username) |
|
125 | user = User.try_to_login(username, password) || User.find_by_api_key(username) | |
126 | end |
|
126 | end | |
127 | if user && user.must_change_password? |
|
127 | if user && user.must_change_password? | |
128 | render_error :message => 'You must change your password', :status => 403 |
|
128 | render_error :message => 'You must change your password', :status => 403 | |
129 | return |
|
129 | return | |
130 | end |
|
130 | end | |
131 | end |
|
131 | end | |
132 | # Switch user if requested by an admin user |
|
132 | # Switch user if requested by an admin user | |
133 | if user && user.admin? && (username = api_switch_user_from_request) |
|
133 | if user && user.admin? && (username = api_switch_user_from_request) | |
134 | su = User.find_by_login(username) |
|
134 | su = User.find_by_login(username) | |
135 | if su && su.active? |
|
135 | if su && su.active? | |
136 | logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger |
|
136 | logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger | |
137 | user = su |
|
137 | user = su | |
138 | else |
|
138 | else | |
139 | render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412 |
|
139 | render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412 | |
140 | end |
|
140 | end | |
141 | end |
|
141 | end | |
142 | end |
|
142 | end | |
143 | user |
|
143 | user | |
144 | end |
|
144 | end | |
145 |
|
145 | |||
146 | def autologin_cookie_name |
|
146 | def autologin_cookie_name | |
147 | Redmine::Configuration['autologin_cookie_name'].presence || 'autologin' |
|
147 | Redmine::Configuration['autologin_cookie_name'].presence || 'autologin' | |
148 | end |
|
148 | end | |
149 |
|
149 | |||
150 | def try_to_autologin |
|
150 | def try_to_autologin | |
151 | if cookies[autologin_cookie_name] && Setting.autologin? |
|
151 | if cookies[autologin_cookie_name] && Setting.autologin? | |
152 | # auto-login feature starts a new session |
|
152 | # auto-login feature starts a new session | |
153 | user = User.try_to_autologin(cookies[autologin_cookie_name]) |
|
153 | user = User.try_to_autologin(cookies[autologin_cookie_name]) | |
154 | if user |
|
154 | if user | |
155 | reset_session |
|
155 | reset_session | |
156 | start_user_session(user) |
|
156 | start_user_session(user) | |
157 | end |
|
157 | end | |
158 | user |
|
158 | user | |
159 | end |
|
159 | end | |
160 | end |
|
160 | end | |
161 |
|
161 | |||
162 | # Sets the logged in user |
|
162 | # Sets the logged in user | |
163 | def logged_user=(user) |
|
163 | def logged_user=(user) | |
164 | reset_session |
|
164 | reset_session | |
165 | if user && user.is_a?(User) |
|
165 | if user && user.is_a?(User) | |
166 | User.current = user |
|
166 | User.current = user | |
167 | start_user_session(user) |
|
167 | start_user_session(user) | |
168 | else |
|
168 | else | |
169 | User.current = User.anonymous |
|
169 | User.current = User.anonymous | |
170 | end |
|
170 | end | |
171 | end |
|
171 | end | |
172 |
|
172 | |||
173 | # Logs out current user |
|
173 | # Logs out current user | |
174 | def logout_user |
|
174 | def logout_user | |
175 | if User.current.logged? |
|
175 | if User.current.logged? | |
176 | cookies.delete(autologin_cookie_name) |
|
176 | cookies.delete(autologin_cookie_name) | |
177 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) |
|
177 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) | |
178 | self.logged_user = nil |
|
178 | self.logged_user = nil | |
179 | end |
|
179 | end | |
180 | end |
|
180 | end | |
181 |
|
181 | |||
182 | # check if login is globally required to access the application |
|
182 | # check if login is globally required to access the application | |
183 | def check_if_login_required |
|
183 | def check_if_login_required | |
184 | # no check needed if user is already logged in |
|
184 | # no check needed if user is already logged in | |
185 | return true if User.current.logged? |
|
185 | return true if User.current.logged? | |
186 | require_login if Setting.login_required? |
|
186 | require_login if Setting.login_required? | |
187 | end |
|
187 | end | |
188 |
|
188 | |||
189 | def check_password_change |
|
189 | def check_password_change | |
190 | if session[:pwd] |
|
190 | if session[:pwd] | |
191 | if User.current.must_change_password? |
|
191 | if User.current.must_change_password? | |
192 | redirect_to my_password_path |
|
192 | redirect_to my_password_path | |
193 | else |
|
193 | else | |
194 | session.delete(:pwd) |
|
194 | session.delete(:pwd) | |
195 | end |
|
195 | end | |
196 | end |
|
196 | end | |
197 | end |
|
197 | end | |
198 |
|
198 | |||
199 | def set_localization |
|
199 | def set_localization | |
200 | lang = nil |
|
200 | lang = nil | |
201 | if User.current.logged? |
|
201 | if User.current.logged? | |
202 | lang = find_language(User.current.language) |
|
202 | lang = find_language(User.current.language) | |
203 | end |
|
203 | end | |
204 | if lang.nil? && !Setting.force_default_language_for_anonymous? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
204 | if lang.nil? && !Setting.force_default_language_for_anonymous? && request.env['HTTP_ACCEPT_LANGUAGE'] | |
205 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first |
|
205 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first | |
206 | if !accept_lang.blank? |
|
206 | if !accept_lang.blank? | |
207 | accept_lang = accept_lang.downcase |
|
207 | accept_lang = accept_lang.downcase | |
208 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
208 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) | |
209 | end |
|
209 | end | |
210 | end |
|
210 | end | |
211 | lang ||= Setting.default_language |
|
211 | lang ||= Setting.default_language | |
212 | set_language_if_valid(lang) |
|
212 | set_language_if_valid(lang) | |
213 | end |
|
213 | end | |
214 |
|
214 | |||
215 | def require_login |
|
215 | def require_login | |
216 | if !User.current.logged? |
|
216 | if !User.current.logged? | |
217 | # Extract only the basic url parameters on non-GET requests |
|
217 | # Extract only the basic url parameters on non-GET requests | |
218 | if request.get? |
|
218 | if request.get? | |
219 | url = url_for(params) |
|
219 | url = url_for(params) | |
220 | else |
|
220 | else | |
221 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
|
221 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) | |
222 | end |
|
222 | end | |
223 | respond_to do |format| |
|
223 | respond_to do |format| | |
224 | format.html { |
|
224 | format.html { | |
225 | if request.xhr? |
|
225 | if request.xhr? | |
226 | head :unauthorized |
|
226 | head :unauthorized | |
227 | else |
|
227 | else | |
228 | redirect_to :controller => "account", :action => "login", :back_url => url |
|
228 | redirect_to :controller => "account", :action => "login", :back_url => url | |
229 | end |
|
229 | end | |
230 | } |
|
230 | } | |
231 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
231 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } | |
232 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
232 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
233 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
233 | format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
234 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
234 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
235 | end |
|
235 | end | |
236 | return false |
|
236 | return false | |
237 | end |
|
237 | end | |
238 | true |
|
238 | true | |
239 | end |
|
239 | end | |
240 |
|
240 | |||
241 | def require_admin |
|
241 | def require_admin | |
242 | return unless require_login |
|
242 | return unless require_login | |
243 | if !User.current.admin? |
|
243 | if !User.current.admin? | |
244 | render_403 |
|
244 | render_403 | |
245 | return false |
|
245 | return false | |
246 | end |
|
246 | end | |
247 | true |
|
247 | true | |
248 | end |
|
248 | end | |
249 |
|
249 | |||
250 | def deny_access |
|
250 | def deny_access | |
251 | User.current.logged? ? render_403 : require_login |
|
251 | User.current.logged? ? render_403 : require_login | |
252 | end |
|
252 | end | |
253 |
|
253 | |||
254 | # Authorize the user for the requested action |
|
254 | # Authorize the user for the requested action | |
255 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
255 | def authorize(ctrl = params[:controller], action = params[:action], global = false) | |
256 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) |
|
256 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) | |
257 | if allowed |
|
257 | if allowed | |
258 | true |
|
258 | true | |
259 | else |
|
259 | else | |
260 | if @project && @project.archived? |
|
260 | if @project && @project.archived? | |
261 | render_403 :message => :notice_not_authorized_archived_project |
|
261 | render_403 :message => :notice_not_authorized_archived_project | |
262 | else |
|
262 | else | |
263 | deny_access |
|
263 | deny_access | |
264 | end |
|
264 | end | |
265 | end |
|
265 | end | |
266 | end |
|
266 | end | |
267 |
|
267 | |||
268 | # Authorize the user for the requested action outside a project |
|
268 | # Authorize the user for the requested action outside a project | |
269 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
269 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) | |
270 | authorize(ctrl, action, global) |
|
270 | authorize(ctrl, action, global) | |
271 | end |
|
271 | end | |
272 |
|
272 | |||
273 | # Find project of id params[:id] |
|
273 | # Find project of id params[:id] | |
274 | def find_project |
|
274 | def find_project | |
275 | @project = Project.find(params[:id]) |
|
275 | @project = Project.find(params[:id]) | |
276 | rescue ActiveRecord::RecordNotFound |
|
276 | rescue ActiveRecord::RecordNotFound | |
277 | render_404 |
|
277 | render_404 | |
278 | end |
|
278 | end | |
279 |
|
279 | |||
280 | # Find project of id params[:project_id] |
|
280 | # Find project of id params[:project_id] | |
281 | def find_project_by_project_id |
|
281 | def find_project_by_project_id | |
282 | @project = Project.find(params[:project_id]) |
|
282 | @project = Project.find(params[:project_id]) | |
283 | rescue ActiveRecord::RecordNotFound |
|
283 | rescue ActiveRecord::RecordNotFound | |
284 | render_404 |
|
284 | render_404 | |
285 | end |
|
285 | end | |
286 |
|
286 | |||
287 | # Find a project based on params[:project_id] |
|
287 | # Find a project based on params[:project_id] | |
288 | # TODO: some subclasses override this, see about merging their logic |
|
288 | # TODO: some subclasses override this, see about merging their logic | |
289 | def find_optional_project |
|
289 | def find_optional_project | |
290 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
290 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? | |
291 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
291 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) | |
292 | allowed ? true : deny_access |
|
292 | allowed ? true : deny_access | |
293 | rescue ActiveRecord::RecordNotFound |
|
293 | rescue ActiveRecord::RecordNotFound | |
294 | render_404 |
|
294 | render_404 | |
295 | end |
|
295 | end | |
296 |
|
296 | |||
297 | # Finds and sets @project based on @object.project |
|
297 | # Finds and sets @project based on @object.project | |
298 | def find_project_from_association |
|
298 | def find_project_from_association | |
299 | render_404 unless @object.present? |
|
299 | render_404 unless @object.present? | |
300 |
|
300 | |||
301 | @project = @object.project |
|
301 | @project = @object.project | |
302 | end |
|
302 | end | |
303 |
|
303 | |||
304 | def find_model_object |
|
304 | def find_model_object | |
305 | model = self.class.model_object |
|
305 | model = self.class.model_object | |
306 | if model |
|
306 | if model | |
307 | @object = model.find(params[:id]) |
|
307 | @object = model.find(params[:id]) | |
308 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
308 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object | |
309 | end |
|
309 | end | |
310 | rescue ActiveRecord::RecordNotFound |
|
310 | rescue ActiveRecord::RecordNotFound | |
311 | render_404 |
|
311 | render_404 | |
312 | end |
|
312 | end | |
313 |
|
313 | |||
314 | def self.model_object(model) |
|
314 | def self.model_object(model) | |
315 | self.model_object = model |
|
315 | self.model_object = model | |
316 | end |
|
316 | end | |
317 |
|
317 | |||
318 | # Find the issue whose id is the :id parameter |
|
318 | # Find the issue whose id is the :id parameter | |
319 | # Raises a Unauthorized exception if the issue is not visible |
|
319 | # Raises a Unauthorized exception if the issue is not visible | |
320 | def find_issue |
|
320 | def find_issue | |
321 | # Issue.visible.find(...) can not be used to redirect user to the login form |
|
321 | # Issue.visible.find(...) can not be used to redirect user to the login form | |
322 | # if the issue actually exists but requires authentication |
|
322 | # if the issue actually exists but requires authentication | |
323 | @issue = Issue.find(params[:id]) |
|
323 | @issue = Issue.find(params[:id]) | |
324 | raise Unauthorized unless @issue.visible? |
|
324 | raise Unauthorized unless @issue.visible? | |
325 | @project = @issue.project |
|
325 | @project = @issue.project | |
326 | rescue ActiveRecord::RecordNotFound |
|
326 | rescue ActiveRecord::RecordNotFound | |
327 | render_404 |
|
327 | render_404 | |
328 | end |
|
328 | end | |
329 |
|
329 | |||
330 | # Find issues with a single :id param or :ids array param |
|
330 | # Find issues with a single :id param or :ids array param | |
331 | # Raises a Unauthorized exception if one of the issues is not visible |
|
331 | # Raises a Unauthorized exception if one of the issues is not visible | |
332 | def find_issues |
|
332 | def find_issues | |
333 | @issues = Issue.where(:id => (params[:id] || params[:ids])).preload(:project, :status, :tracker, :priority, :author, :assigned_to, :relations_to).to_a |
|
333 | @issues = Issue.where(:id => (params[:id] || params[:ids])).preload(:project, :status, :tracker, :priority, :author, :assigned_to, :relations_to).to_a | |
334 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
334 | raise ActiveRecord::RecordNotFound if @issues.empty? | |
335 | raise Unauthorized unless @issues.all?(&:visible?) |
|
335 | raise Unauthorized unless @issues.all?(&:visible?) | |
336 | @projects = @issues.collect(&:project).compact.uniq |
|
336 | @projects = @issues.collect(&:project).compact.uniq | |
337 | @project = @projects.first if @projects.size == 1 |
|
337 | @project = @projects.first if @projects.size == 1 | |
338 | rescue ActiveRecord::RecordNotFound |
|
338 | rescue ActiveRecord::RecordNotFound | |
339 | render_404 |
|
339 | render_404 | |
340 | end |
|
340 | end | |
341 |
|
341 | |||
342 | def find_attachments |
|
342 | def find_attachments | |
343 | if (attachments = params[:attachments]).present? |
|
343 | if (attachments = params[:attachments]).present? | |
344 | att = attachments.values.collect do |attachment| |
|
344 | att = attachments.values.collect do |attachment| | |
345 | Attachment.find_by_token( attachment[:token] ) if attachment[:token].present? |
|
345 | Attachment.find_by_token( attachment[:token] ) if attachment[:token].present? | |
346 | end |
|
346 | end | |
347 | att.compact! |
|
347 | att.compact! | |
348 | end |
|
348 | end | |
349 | @attachments = att || [] |
|
349 | @attachments = att || [] | |
350 | end |
|
350 | end | |
351 |
|
351 | |||
352 | # make sure that the user is a member of the project (or admin) if project is private |
|
352 | # make sure that the user is a member of the project (or admin) if project is private | |
353 | # used as a before_filter for actions that do not require any particular permission on the project |
|
353 | # used as a before_filter for actions that do not require any particular permission on the project | |
354 | def check_project_privacy |
|
354 | def check_project_privacy | |
355 | if @project && !@project.archived? |
|
355 | if @project && !@project.archived? | |
356 | if @project.visible? |
|
356 | if @project.visible? | |
357 | true |
|
357 | true | |
358 | else |
|
358 | else | |
359 | deny_access |
|
359 | deny_access | |
360 | end |
|
360 | end | |
361 | else |
|
361 | else | |
362 | @project = nil |
|
362 | @project = nil | |
363 | render_404 |
|
363 | render_404 | |
364 | false |
|
364 | false | |
365 | end |
|
365 | end | |
366 | end |
|
366 | end | |
367 |
|
367 | |||
368 | def back_url |
|
368 | def back_url | |
369 | url = params[:back_url] |
|
369 | url = params[:back_url] | |
370 | if url.nil? && referer = request.env['HTTP_REFERER'] |
|
370 | if url.nil? && referer = request.env['HTTP_REFERER'] | |
371 | url = CGI.unescape(referer.to_s) |
|
371 | url = CGI.unescape(referer.to_s) | |
372 | end |
|
372 | end | |
373 | url |
|
373 | url | |
374 | end |
|
374 | end | |
375 |
|
375 | |||
376 | def redirect_back_or_default(default) |
|
376 | def redirect_back_or_default(default, options={}) | |
377 | back_url = params[:back_url].to_s |
|
377 | back_url = params[:back_url].to_s | |
378 | if back_url.present? |
|
378 | if back_url.present? | |
379 | begin |
|
379 | begin | |
380 | uri = URI.parse(back_url) |
|
380 | uri = URI.parse(back_url) | |
381 | # do not redirect user to another host or to the login or register page |
|
381 | # do not redirect user to another host or to the login or register page | |
382 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
382 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) | |
383 | redirect_to(back_url) |
|
383 | redirect_to(back_url) | |
384 | return |
|
384 | return | |
385 | end |
|
385 | end | |
386 | rescue URI::InvalidURIError |
|
386 | rescue URI::InvalidURIError | |
387 | logger.warn("Could not redirect to invalid URL #{back_url}") |
|
387 | logger.warn("Could not redirect to invalid URL #{back_url}") | |
388 | # redirect to default |
|
388 | # redirect to default | |
389 | end |
|
389 | end | |
|
390 | elsif options[:referer] | |||
|
391 | redirect_to_referer_or default | |||
|
392 | return | |||
390 | end |
|
393 | end | |
391 | redirect_to default |
|
394 | redirect_to default | |
392 | false |
|
395 | false | |
393 | end |
|
396 | end | |
394 |
|
397 | |||
395 | # Redirects to the request referer if present, redirects to args or call block otherwise. |
|
398 | # Redirects to the request referer if present, redirects to args or call block otherwise. | |
396 | def redirect_to_referer_or(*args, &block) |
|
399 | def redirect_to_referer_or(*args, &block) | |
397 | redirect_to :back |
|
400 | redirect_to :back | |
398 | rescue ::ActionController::RedirectBackError |
|
401 | rescue ::ActionController::RedirectBackError | |
399 | if args.any? |
|
402 | if args.any? | |
400 | redirect_to *args |
|
403 | redirect_to *args | |
401 | elsif block_given? |
|
404 | elsif block_given? | |
402 | block.call |
|
405 | block.call | |
403 | else |
|
406 | else | |
404 | raise "#redirect_to_referer_or takes arguments or a block" |
|
407 | raise "#redirect_to_referer_or takes arguments or a block" | |
405 | end |
|
408 | end | |
406 | end |
|
409 | end | |
407 |
|
410 | |||
408 | def render_403(options={}) |
|
411 | def render_403(options={}) | |
409 | @project = nil |
|
412 | @project = nil | |
410 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) |
|
413 | render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) | |
411 | return false |
|
414 | return false | |
412 | end |
|
415 | end | |
413 |
|
416 | |||
414 | def render_404(options={}) |
|
417 | def render_404(options={}) | |
415 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) |
|
418 | render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) | |
416 | return false |
|
419 | return false | |
417 | end |
|
420 | end | |
418 |
|
421 | |||
419 | # Renders an error response |
|
422 | # Renders an error response | |
420 | def render_error(arg) |
|
423 | def render_error(arg) | |
421 | arg = {:message => arg} unless arg.is_a?(Hash) |
|
424 | arg = {:message => arg} unless arg.is_a?(Hash) | |
422 |
|
425 | |||
423 | @message = arg[:message] |
|
426 | @message = arg[:message] | |
424 | @message = l(@message) if @message.is_a?(Symbol) |
|
427 | @message = l(@message) if @message.is_a?(Symbol) | |
425 | @status = arg[:status] || 500 |
|
428 | @status = arg[:status] || 500 | |
426 |
|
429 | |||
427 | respond_to do |format| |
|
430 | respond_to do |format| | |
428 | format.html { |
|
431 | format.html { | |
429 | render :template => 'common/error', :layout => use_layout, :status => @status |
|
432 | render :template => 'common/error', :layout => use_layout, :status => @status | |
430 | } |
|
433 | } | |
431 | format.any { head @status } |
|
434 | format.any { head @status } | |
432 | end |
|
435 | end | |
433 | end |
|
436 | end | |
434 |
|
437 | |||
435 | # Handler for ActionView::MissingTemplate exception |
|
438 | # Handler for ActionView::MissingTemplate exception | |
436 | def missing_template |
|
439 | def missing_template | |
437 | logger.warn "Missing template, responding with 404" |
|
440 | logger.warn "Missing template, responding with 404" | |
438 | @project = nil |
|
441 | @project = nil | |
439 | render_404 |
|
442 | render_404 | |
440 | end |
|
443 | end | |
441 |
|
444 | |||
442 | # Filter for actions that provide an API response |
|
445 | # Filter for actions that provide an API response | |
443 | # but have no HTML representation for non admin users |
|
446 | # but have no HTML representation for non admin users | |
444 | def require_admin_or_api_request |
|
447 | def require_admin_or_api_request | |
445 | return true if api_request? |
|
448 | return true if api_request? | |
446 | if User.current.admin? |
|
449 | if User.current.admin? | |
447 | true |
|
450 | true | |
448 | elsif User.current.logged? |
|
451 | elsif User.current.logged? | |
449 | render_error(:status => 406) |
|
452 | render_error(:status => 406) | |
450 | else |
|
453 | else | |
451 | deny_access |
|
454 | deny_access | |
452 | end |
|
455 | end | |
453 | end |
|
456 | end | |
454 |
|
457 | |||
455 | # Picks which layout to use based on the request |
|
458 | # Picks which layout to use based on the request | |
456 | # |
|
459 | # | |
457 | # @return [boolean, string] name of the layout to use or false for no layout |
|
460 | # @return [boolean, string] name of the layout to use or false for no layout | |
458 | def use_layout |
|
461 | def use_layout | |
459 | request.xhr? ? false : 'base' |
|
462 | request.xhr? ? false : 'base' | |
460 | end |
|
463 | end | |
461 |
|
464 | |||
462 | def render_feed(items, options={}) |
|
465 | def render_feed(items, options={}) | |
463 | @items = items || [] |
|
466 | @items = items || [] | |
464 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
467 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } | |
465 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
468 | @items = @items.slice(0, Setting.feeds_limit.to_i) | |
466 | @title = options[:title] || Setting.app_title |
|
469 | @title = options[:title] || Setting.app_title | |
467 | render :template => "common/feed", :formats => [:atom], :layout => false, |
|
470 | render :template => "common/feed", :formats => [:atom], :layout => false, | |
468 | :content_type => 'application/atom+xml' |
|
471 | :content_type => 'application/atom+xml' | |
469 | end |
|
472 | end | |
470 |
|
473 | |||
471 | def self.accept_rss_auth(*actions) |
|
474 | def self.accept_rss_auth(*actions) | |
472 | if actions.any? |
|
475 | if actions.any? | |
473 | self.accept_rss_auth_actions = actions |
|
476 | self.accept_rss_auth_actions = actions | |
474 | else |
|
477 | else | |
475 | self.accept_rss_auth_actions || [] |
|
478 | self.accept_rss_auth_actions || [] | |
476 | end |
|
479 | end | |
477 | end |
|
480 | end | |
478 |
|
481 | |||
479 | def accept_rss_auth?(action=action_name) |
|
482 | def accept_rss_auth?(action=action_name) | |
480 | self.class.accept_rss_auth.include?(action.to_sym) |
|
483 | self.class.accept_rss_auth.include?(action.to_sym) | |
481 | end |
|
484 | end | |
482 |
|
485 | |||
483 | def self.accept_api_auth(*actions) |
|
486 | def self.accept_api_auth(*actions) | |
484 | if actions.any? |
|
487 | if actions.any? | |
485 | self.accept_api_auth_actions = actions |
|
488 | self.accept_api_auth_actions = actions | |
486 | else |
|
489 | else | |
487 | self.accept_api_auth_actions || [] |
|
490 | self.accept_api_auth_actions || [] | |
488 | end |
|
491 | end | |
489 | end |
|
492 | end | |
490 |
|
493 | |||
491 | def accept_api_auth?(action=action_name) |
|
494 | def accept_api_auth?(action=action_name) | |
492 | self.class.accept_api_auth.include?(action.to_sym) |
|
495 | self.class.accept_api_auth.include?(action.to_sym) | |
493 | end |
|
496 | end | |
494 |
|
497 | |||
495 | # Returns the number of objects that should be displayed |
|
498 | # Returns the number of objects that should be displayed | |
496 | # on the paginated list |
|
499 | # on the paginated list | |
497 | def per_page_option |
|
500 | def per_page_option | |
498 | per_page = nil |
|
501 | per_page = nil | |
499 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
502 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) | |
500 | per_page = params[:per_page].to_s.to_i |
|
503 | per_page = params[:per_page].to_s.to_i | |
501 | session[:per_page] = per_page |
|
504 | session[:per_page] = per_page | |
502 | elsif session[:per_page] |
|
505 | elsif session[:per_page] | |
503 | per_page = session[:per_page] |
|
506 | per_page = session[:per_page] | |
504 | else |
|
507 | else | |
505 | per_page = Setting.per_page_options_array.first || 25 |
|
508 | per_page = Setting.per_page_options_array.first || 25 | |
506 | end |
|
509 | end | |
507 | per_page |
|
510 | per_page | |
508 | end |
|
511 | end | |
509 |
|
512 | |||
510 | # Returns offset and limit used to retrieve objects |
|
513 | # Returns offset and limit used to retrieve objects | |
511 | # for an API response based on offset, limit and page parameters |
|
514 | # for an API response based on offset, limit and page parameters | |
512 | def api_offset_and_limit(options=params) |
|
515 | def api_offset_and_limit(options=params) | |
513 | if options[:offset].present? |
|
516 | if options[:offset].present? | |
514 | offset = options[:offset].to_i |
|
517 | offset = options[:offset].to_i | |
515 | if offset < 0 |
|
518 | if offset < 0 | |
516 | offset = 0 |
|
519 | offset = 0 | |
517 | end |
|
520 | end | |
518 | end |
|
521 | end | |
519 | limit = options[:limit].to_i |
|
522 | limit = options[:limit].to_i | |
520 | if limit < 1 |
|
523 | if limit < 1 | |
521 | limit = 25 |
|
524 | limit = 25 | |
522 | elsif limit > 100 |
|
525 | elsif limit > 100 | |
523 | limit = 100 |
|
526 | limit = 100 | |
524 | end |
|
527 | end | |
525 | if offset.nil? && options[:page].present? |
|
528 | if offset.nil? && options[:page].present? | |
526 | offset = (options[:page].to_i - 1) * limit |
|
529 | offset = (options[:page].to_i - 1) * limit | |
527 | offset = 0 if offset < 0 |
|
530 | offset = 0 if offset < 0 | |
528 | end |
|
531 | end | |
529 | offset ||= 0 |
|
532 | offset ||= 0 | |
530 |
|
533 | |||
531 | [offset, limit] |
|
534 | [offset, limit] | |
532 | end |
|
535 | end | |
533 |
|
536 | |||
534 | # qvalues http header parser |
|
537 | # qvalues http header parser | |
535 | # code taken from webrick |
|
538 | # code taken from webrick | |
536 | def parse_qvalues(value) |
|
539 | def parse_qvalues(value) | |
537 | tmp = [] |
|
540 | tmp = [] | |
538 | if value |
|
541 | if value | |
539 | parts = value.split(/,\s*/) |
|
542 | parts = value.split(/,\s*/) | |
540 | parts.each {|part| |
|
543 | parts.each {|part| | |
541 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
544 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) | |
542 | val = m[1] |
|
545 | val = m[1] | |
543 | q = (m[2] or 1).to_f |
|
546 | q = (m[2] or 1).to_f | |
544 | tmp.push([val, q]) |
|
547 | tmp.push([val, q]) | |
545 | end |
|
548 | end | |
546 | } |
|
549 | } | |
547 | tmp = tmp.sort_by{|val, q| -q} |
|
550 | tmp = tmp.sort_by{|val, q| -q} | |
548 | tmp.collect!{|val, q| val} |
|
551 | tmp.collect!{|val, q| val} | |
549 | end |
|
552 | end | |
550 | return tmp |
|
553 | return tmp | |
551 | rescue |
|
554 | rescue | |
552 | nil |
|
555 | nil | |
553 | end |
|
556 | end | |
554 |
|
557 | |||
555 | # Returns a string that can be used as filename value in Content-Disposition header |
|
558 | # Returns a string that can be used as filename value in Content-Disposition header | |
556 | def filename_for_content_disposition(name) |
|
559 | def filename_for_content_disposition(name) | |
557 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
560 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name | |
558 | end |
|
561 | end | |
559 |
|
562 | |||
560 | def api_request? |
|
563 | def api_request? | |
561 | %w(xml json).include? params[:format] |
|
564 | %w(xml json).include? params[:format] | |
562 | end |
|
565 | end | |
563 |
|
566 | |||
564 | # Returns the API key present in the request |
|
567 | # Returns the API key present in the request | |
565 | def api_key_from_request |
|
568 | def api_key_from_request | |
566 | if params[:key].present? |
|
569 | if params[:key].present? | |
567 | params[:key].to_s |
|
570 | params[:key].to_s | |
568 | elsif request.headers["X-Redmine-API-Key"].present? |
|
571 | elsif request.headers["X-Redmine-API-Key"].present? | |
569 | request.headers["X-Redmine-API-Key"].to_s |
|
572 | request.headers["X-Redmine-API-Key"].to_s | |
570 | end |
|
573 | end | |
571 | end |
|
574 | end | |
572 |
|
575 | |||
573 | # Returns the API 'switch user' value if present |
|
576 | # Returns the API 'switch user' value if present | |
574 | def api_switch_user_from_request |
|
577 | def api_switch_user_from_request | |
575 | request.headers["X-Redmine-Switch-User"].to_s.presence |
|
578 | request.headers["X-Redmine-Switch-User"].to_s.presence | |
576 | end |
|
579 | end | |
577 |
|
580 | |||
578 | # Renders a warning flash if obj has unsaved attachments |
|
581 | # Renders a warning flash if obj has unsaved attachments | |
579 | def render_attachment_warning_if_needed(obj) |
|
582 | def render_attachment_warning_if_needed(obj) | |
580 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
583 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? | |
581 | end |
|
584 | end | |
582 |
|
585 | |||
583 | # Rescues an invalid query statement. Just in case... |
|
586 | # Rescues an invalid query statement. Just in case... | |
584 | def query_statement_invalid(exception) |
|
587 | def query_statement_invalid(exception) | |
585 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
588 | logger.error "Query::StatementInvalid: #{exception.message}" if logger | |
586 | session.delete(:query) |
|
589 | session.delete(:query) | |
587 | sort_clear if respond_to?(:sort_clear) |
|
590 | sort_clear if respond_to?(:sort_clear) | |
588 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
591 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." | |
589 | end |
|
592 | end | |
590 |
|
593 | |||
591 | # Renders a 200 response for successfull updates or deletions via the API |
|
594 | # Renders a 200 response for successfull updates or deletions via the API | |
592 | def render_api_ok |
|
595 | def render_api_ok | |
593 | render_api_head :ok |
|
596 | render_api_head :ok | |
594 | end |
|
597 | end | |
595 |
|
598 | |||
596 | # Renders a head API response |
|
599 | # Renders a head API response | |
597 | def render_api_head(status) |
|
600 | def render_api_head(status) | |
598 | # #head would return a response body with one space |
|
601 | # #head would return a response body with one space | |
599 | render :text => '', :status => status, :layout => nil |
|
602 | render :text => '', :status => status, :layout => nil | |
600 | end |
|
603 | end | |
601 |
|
604 | |||
602 | # Renders API response on validation failure |
|
605 | # Renders API response on validation failure | |
603 | def render_validation_errors(objects) |
|
606 | def render_validation_errors(objects) | |
604 | if objects.is_a?(Array) |
|
607 | if objects.is_a?(Array) | |
605 | @error_messages = objects.map {|object| object.errors.full_messages}.flatten |
|
608 | @error_messages = objects.map {|object| object.errors.full_messages}.flatten | |
606 | else |
|
609 | else | |
607 | @error_messages = objects.errors.full_messages |
|
610 | @error_messages = objects.errors.full_messages | |
608 | end |
|
611 | end | |
609 | render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil |
|
612 | render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil | |
610 | end |
|
613 | end | |
611 |
|
614 | |||
612 | # Overrides #_include_layout? so that #render with no arguments |
|
615 | # Overrides #_include_layout? so that #render with no arguments | |
613 | # doesn't use the layout for api requests |
|
616 | # doesn't use the layout for api requests | |
614 | def _include_layout?(*args) |
|
617 | def _include_layout?(*args) | |
615 | api_request? ? false : super |
|
618 | api_request? ? false : super | |
616 | end |
|
619 | end | |
617 | end |
|
620 | end |
@@ -1,344 +1,362 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class AccountControllerTest < ActionController::TestCase |
|
20 | class AccountControllerTest < ActionController::TestCase | |
21 | fixtures :users, :roles |
|
21 | fixtures :users, :roles | |
22 |
|
22 | |||
23 | def setup |
|
23 | def setup | |
24 | User.current = nil |
|
24 | User.current = nil | |
25 | end |
|
25 | end | |
26 |
|
26 | |||
27 | def test_get_login |
|
27 | def test_get_login | |
28 | get :login |
|
28 | get :login | |
29 | assert_response :success |
|
29 | assert_response :success | |
30 | assert_template 'login' |
|
30 | assert_template 'login' | |
31 |
|
31 | |||
32 | assert_select 'input[name=username]' |
|
32 | assert_select 'input[name=username]' | |
33 | assert_select 'input[name=password]' |
|
33 | assert_select 'input[name=password]' | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 |
def test_get_login_while_logged_in_should_redirect_to_ |
|
36 | def test_get_login_while_logged_in_should_redirect_to_back_url_if_present | |
|
37 | @request.session[:user_id] = 2 | |||
|
38 | @request.env["HTTP_REFERER"] = 'http://test.host/issues/show/1' | |||
|
39 | ||||
|
40 | get :login, :back_url => 'http://test.host/issues/show/1' | |||
|
41 | assert_redirected_to '/issues/show/1' | |||
|
42 | assert_equal 2, @request.session[:user_id] | |||
|
43 | end | |||
|
44 | ||||
|
45 | def test_get_login_while_logged_in_should_redirect_to_referer_without_back_url | |||
|
46 | @request.session[:user_id] = 2 | |||
|
47 | @request.env["HTTP_REFERER"] = 'http://test.host/issues/show/1' | |||
|
48 | ||||
|
49 | get :login | |||
|
50 | assert_redirected_to '/issues/show/1' | |||
|
51 | assert_equal 2, @request.session[:user_id] | |||
|
52 | end | |||
|
53 | ||||
|
54 | def test_get_login_while_logged_in_should_redirect_to_home_by_default | |||
37 | @request.session[:user_id] = 2 |
|
55 | @request.session[:user_id] = 2 | |
38 |
|
56 | |||
39 | get :login |
|
57 | get :login | |
40 | assert_redirected_to '/' |
|
58 | assert_redirected_to '/' | |
41 | assert_equal 2, @request.session[:user_id] |
|
59 | assert_equal 2, @request.session[:user_id] | |
42 | end |
|
60 | end | |
43 |
|
61 | |||
44 | def test_login_should_redirect_to_back_url_param |
|
62 | def test_login_should_redirect_to_back_url_param | |
45 | # request.uri is "test.host" in test environment |
|
63 | # request.uri is "test.host" in test environment | |
46 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1' |
|
64 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1' | |
47 | assert_redirected_to '/issues/show/1' |
|
65 | assert_redirected_to '/issues/show/1' | |
48 | end |
|
66 | end | |
49 |
|
67 | |||
50 | def test_login_should_not_redirect_to_another_host |
|
68 | def test_login_should_not_redirect_to_another_host | |
51 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake' |
|
69 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake' | |
52 | assert_redirected_to '/my/page' |
|
70 | assert_redirected_to '/my/page' | |
53 | end |
|
71 | end | |
54 |
|
72 | |||
55 | def test_login_with_wrong_password |
|
73 | def test_login_with_wrong_password | |
56 | post :login, :username => 'admin', :password => 'bad' |
|
74 | post :login, :username => 'admin', :password => 'bad' | |
57 | assert_response :success |
|
75 | assert_response :success | |
58 | assert_template 'login' |
|
76 | assert_template 'login' | |
59 |
|
77 | |||
60 | assert_select 'div.flash.error', :text => /Invalid user or password/ |
|
78 | assert_select 'div.flash.error', :text => /Invalid user or password/ | |
61 | assert_select 'input[name=username][value=admin]' |
|
79 | assert_select 'input[name=username][value=admin]' | |
62 | assert_select 'input[name=password]' |
|
80 | assert_select 'input[name=password]' | |
63 | assert_select 'input[name=password][value]', 0 |
|
81 | assert_select 'input[name=password][value]', 0 | |
64 | end |
|
82 | end | |
65 |
|
83 | |||
66 | def test_login_with_locked_account_should_fail |
|
84 | def test_login_with_locked_account_should_fail | |
67 | User.find(2).update_attribute :status, User::STATUS_LOCKED |
|
85 | User.find(2).update_attribute :status, User::STATUS_LOCKED | |
68 |
|
86 | |||
69 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
87 | post :login, :username => 'jsmith', :password => 'jsmith' | |
70 | assert_redirected_to '/login' |
|
88 | assert_redirected_to '/login' | |
71 | assert_include 'locked', flash[:error] |
|
89 | assert_include 'locked', flash[:error] | |
72 | assert_nil @request.session[:user_id] |
|
90 | assert_nil @request.session[:user_id] | |
73 | end |
|
91 | end | |
74 |
|
92 | |||
75 | def test_login_as_registered_user_with_manual_activation_should_inform_user |
|
93 | def test_login_as_registered_user_with_manual_activation_should_inform_user | |
76 | User.find(2).update_attribute :status, User::STATUS_REGISTERED |
|
94 | User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
77 |
|
95 | |||
78 | with_settings :self_registration => '2', :default_language => 'en' do |
|
96 | with_settings :self_registration => '2', :default_language => 'en' do | |
79 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
97 | post :login, :username => 'jsmith', :password => 'jsmith' | |
80 | assert_redirected_to '/login' |
|
98 | assert_redirected_to '/login' | |
81 | assert_include 'pending administrator approval', flash[:error] |
|
99 | assert_include 'pending administrator approval', flash[:error] | |
82 | end |
|
100 | end | |
83 | end |
|
101 | end | |
84 |
|
102 | |||
85 | def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email |
|
103 | def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email | |
86 | User.find(2).update_attribute :status, User::STATUS_REGISTERED |
|
104 | User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
87 |
|
105 | |||
88 | with_settings :self_registration => '1', :default_language => 'en' do |
|
106 | with_settings :self_registration => '1', :default_language => 'en' do | |
89 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
107 | post :login, :username => 'jsmith', :password => 'jsmith' | |
90 | assert_redirected_to '/login' |
|
108 | assert_redirected_to '/login' | |
91 | assert_equal 2, @request.session[:registered_user_id] |
|
109 | assert_equal 2, @request.session[:registered_user_id] | |
92 | assert_include 'new activation email', flash[:error] |
|
110 | assert_include 'new activation email', flash[:error] | |
93 | end |
|
111 | end | |
94 | end |
|
112 | end | |
95 |
|
113 | |||
96 | def test_login_should_rescue_auth_source_exception |
|
114 | def test_login_should_rescue_auth_source_exception | |
97 | source = AuthSource.create!(:name => 'Test') |
|
115 | source = AuthSource.create!(:name => 'Test') | |
98 | User.find(2).update_attribute :auth_source_id, source.id |
|
116 | User.find(2).update_attribute :auth_source_id, source.id | |
99 | AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) |
|
117 | AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) | |
100 |
|
118 | |||
101 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
119 | post :login, :username => 'jsmith', :password => 'jsmith' | |
102 | assert_response 500 |
|
120 | assert_response 500 | |
103 | assert_error_tag :content => /Something wrong/ |
|
121 | assert_error_tag :content => /Something wrong/ | |
104 | end |
|
122 | end | |
105 |
|
123 | |||
106 | def test_login_should_reset_session |
|
124 | def test_login_should_reset_session | |
107 | @controller.expects(:reset_session).once |
|
125 | @controller.expects(:reset_session).once | |
108 |
|
126 | |||
109 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
127 | post :login, :username => 'jsmith', :password => 'jsmith' | |
110 | assert_response 302 |
|
128 | assert_response 302 | |
111 | end |
|
129 | end | |
112 |
|
130 | |||
113 | def test_get_logout_should_not_logout |
|
131 | def test_get_logout_should_not_logout | |
114 | @request.session[:user_id] = 2 |
|
132 | @request.session[:user_id] = 2 | |
115 | get :logout |
|
133 | get :logout | |
116 | assert_response :success |
|
134 | assert_response :success | |
117 | assert_template 'logout' |
|
135 | assert_template 'logout' | |
118 |
|
136 | |||
119 | assert_equal 2, @request.session[:user_id] |
|
137 | assert_equal 2, @request.session[:user_id] | |
120 | end |
|
138 | end | |
121 |
|
139 | |||
122 | def test_get_logout_with_anonymous_should_redirect |
|
140 | def test_get_logout_with_anonymous_should_redirect | |
123 | get :logout |
|
141 | get :logout | |
124 | assert_redirected_to '/' |
|
142 | assert_redirected_to '/' | |
125 | end |
|
143 | end | |
126 |
|
144 | |||
127 | def test_logout |
|
145 | def test_logout | |
128 | @request.session[:user_id] = 2 |
|
146 | @request.session[:user_id] = 2 | |
129 | post :logout |
|
147 | post :logout | |
130 | assert_redirected_to '/' |
|
148 | assert_redirected_to '/' | |
131 | assert_nil @request.session[:user_id] |
|
149 | assert_nil @request.session[:user_id] | |
132 | end |
|
150 | end | |
133 |
|
151 | |||
134 | def test_logout_should_reset_session |
|
152 | def test_logout_should_reset_session | |
135 | @controller.expects(:reset_session).once |
|
153 | @controller.expects(:reset_session).once | |
136 |
|
154 | |||
137 | @request.session[:user_id] = 2 |
|
155 | @request.session[:user_id] = 2 | |
138 | post :logout |
|
156 | post :logout | |
139 | assert_response 302 |
|
157 | assert_response 302 | |
140 | end |
|
158 | end | |
141 |
|
159 | |||
142 | def test_get_register_with_registration_on |
|
160 | def test_get_register_with_registration_on | |
143 | with_settings :self_registration => '3' do |
|
161 | with_settings :self_registration => '3' do | |
144 | get :register |
|
162 | get :register | |
145 | assert_response :success |
|
163 | assert_response :success | |
146 | assert_template 'register' |
|
164 | assert_template 'register' | |
147 | assert_not_nil assigns(:user) |
|
165 | assert_not_nil assigns(:user) | |
148 |
|
166 | |||
149 | assert_select 'input[name=?]', 'user[password]' |
|
167 | assert_select 'input[name=?]', 'user[password]' | |
150 | assert_select 'input[name=?]', 'user[password_confirmation]' |
|
168 | assert_select 'input[name=?]', 'user[password_confirmation]' | |
151 | end |
|
169 | end | |
152 | end |
|
170 | end | |
153 |
|
171 | |||
154 | def test_get_register_should_detect_user_language |
|
172 | def test_get_register_should_detect_user_language | |
155 | with_settings :self_registration => '3' do |
|
173 | with_settings :self_registration => '3' do | |
156 | @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' |
|
174 | @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' | |
157 | get :register |
|
175 | get :register | |
158 | assert_response :success |
|
176 | assert_response :success | |
159 | assert_not_nil assigns(:user) |
|
177 | assert_not_nil assigns(:user) | |
160 | assert_equal 'fr', assigns(:user).language |
|
178 | assert_equal 'fr', assigns(:user).language | |
161 | assert_select 'select[name=?]', 'user[language]' do |
|
179 | assert_select 'select[name=?]', 'user[language]' do | |
162 | assert_select 'option[value=fr][selected=selected]' |
|
180 | assert_select 'option[value=fr][selected=selected]' | |
163 | end |
|
181 | end | |
164 | end |
|
182 | end | |
165 | end |
|
183 | end | |
166 |
|
184 | |||
167 | def test_get_register_with_registration_off_should_redirect |
|
185 | def test_get_register_with_registration_off_should_redirect | |
168 | with_settings :self_registration => '0' do |
|
186 | with_settings :self_registration => '0' do | |
169 | get :register |
|
187 | get :register | |
170 | assert_redirected_to '/' |
|
188 | assert_redirected_to '/' | |
171 | end |
|
189 | end | |
172 | end |
|
190 | end | |
173 |
|
191 | |||
174 | # See integration/account_test.rb for the full test |
|
192 | # See integration/account_test.rb for the full test | |
175 | def test_post_register_with_registration_on |
|
193 | def test_post_register_with_registration_on | |
176 | with_settings :self_registration => '3' do |
|
194 | with_settings :self_registration => '3' do | |
177 | assert_difference 'User.count' do |
|
195 | assert_difference 'User.count' do | |
178 | post :register, :user => { |
|
196 | post :register, :user => { | |
179 | :login => 'register', |
|
197 | :login => 'register', | |
180 | :password => 'secret123', |
|
198 | :password => 'secret123', | |
181 | :password_confirmation => 'secret123', |
|
199 | :password_confirmation => 'secret123', | |
182 | :firstname => 'John', |
|
200 | :firstname => 'John', | |
183 | :lastname => 'Doe', |
|
201 | :lastname => 'Doe', | |
184 | :mail => 'register@example.com' |
|
202 | :mail => 'register@example.com' | |
185 | } |
|
203 | } | |
186 | assert_redirected_to '/my/account' |
|
204 | assert_redirected_to '/my/account' | |
187 | end |
|
205 | end | |
188 | user = User.order('id DESC').first |
|
206 | user = User.order('id DESC').first | |
189 | assert_equal 'register', user.login |
|
207 | assert_equal 'register', user.login | |
190 | assert_equal 'John', user.firstname |
|
208 | assert_equal 'John', user.firstname | |
191 | assert_equal 'Doe', user.lastname |
|
209 | assert_equal 'Doe', user.lastname | |
192 | assert_equal 'register@example.com', user.mail |
|
210 | assert_equal 'register@example.com', user.mail | |
193 | assert user.check_password?('secret123') |
|
211 | assert user.check_password?('secret123') | |
194 | assert user.active? |
|
212 | assert user.active? | |
195 | end |
|
213 | end | |
196 | end |
|
214 | end | |
197 |
|
215 | |||
198 | def test_post_register_with_registration_off_should_redirect |
|
216 | def test_post_register_with_registration_off_should_redirect | |
199 | with_settings :self_registration => '0' do |
|
217 | with_settings :self_registration => '0' do | |
200 | assert_no_difference 'User.count' do |
|
218 | assert_no_difference 'User.count' do | |
201 | post :register, :user => { |
|
219 | post :register, :user => { | |
202 | :login => 'register', |
|
220 | :login => 'register', | |
203 | :password => 'test', |
|
221 | :password => 'test', | |
204 | :password_confirmation => 'test', |
|
222 | :password_confirmation => 'test', | |
205 | :firstname => 'John', |
|
223 | :firstname => 'John', | |
206 | :lastname => 'Doe', |
|
224 | :lastname => 'Doe', | |
207 | :mail => 'register@example.com' |
|
225 | :mail => 'register@example.com' | |
208 | } |
|
226 | } | |
209 | assert_redirected_to '/' |
|
227 | assert_redirected_to '/' | |
210 | end |
|
228 | end | |
211 | end |
|
229 | end | |
212 | end |
|
230 | end | |
213 |
|
231 | |||
214 | def test_get_lost_password_should_display_lost_password_form |
|
232 | def test_get_lost_password_should_display_lost_password_form | |
215 | get :lost_password |
|
233 | get :lost_password | |
216 | assert_response :success |
|
234 | assert_response :success | |
217 | assert_select 'input[name=mail]' |
|
235 | assert_select 'input[name=mail]' | |
218 | end |
|
236 | end | |
219 |
|
237 | |||
220 | def test_lost_password_for_active_user_should_create_a_token |
|
238 | def test_lost_password_for_active_user_should_create_a_token | |
221 | Token.delete_all |
|
239 | Token.delete_all | |
222 | ActionMailer::Base.deliveries.clear |
|
240 | ActionMailer::Base.deliveries.clear | |
223 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
241 | assert_difference 'ActionMailer::Base.deliveries.size' do | |
224 | assert_difference 'Token.count' do |
|
242 | assert_difference 'Token.count' do | |
225 | with_settings :host_name => 'mydomain.foo', :protocol => 'http' do |
|
243 | with_settings :host_name => 'mydomain.foo', :protocol => 'http' do | |
226 | post :lost_password, :mail => 'JSmith@somenet.foo' |
|
244 | post :lost_password, :mail => 'JSmith@somenet.foo' | |
227 | assert_redirected_to '/login' |
|
245 | assert_redirected_to '/login' | |
228 | end |
|
246 | end | |
229 | end |
|
247 | end | |
230 | end |
|
248 | end | |
231 |
|
249 | |||
232 | token = Token.order('id DESC').first |
|
250 | token = Token.order('id DESC').first | |
233 | assert_equal User.find(2), token.user |
|
251 | assert_equal User.find(2), token.user | |
234 | assert_equal 'recovery', token.action |
|
252 | assert_equal 'recovery', token.action | |
235 |
|
253 | |||
236 | assert_select_email do |
|
254 | assert_select_email do | |
237 | assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}" |
|
255 | assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}" | |
238 | end |
|
256 | end | |
239 | end |
|
257 | end | |
240 |
|
258 | |||
241 | def test_lost_password_for_unknown_user_should_fail |
|
259 | def test_lost_password_for_unknown_user_should_fail | |
242 | Token.delete_all |
|
260 | Token.delete_all | |
243 | assert_no_difference 'Token.count' do |
|
261 | assert_no_difference 'Token.count' do | |
244 | post :lost_password, :mail => 'invalid@somenet.foo' |
|
262 | post :lost_password, :mail => 'invalid@somenet.foo' | |
245 | assert_response :success |
|
263 | assert_response :success | |
246 | end |
|
264 | end | |
247 | end |
|
265 | end | |
248 |
|
266 | |||
249 | def test_lost_password_for_non_active_user_should_fail |
|
267 | def test_lost_password_for_non_active_user_should_fail | |
250 | Token.delete_all |
|
268 | Token.delete_all | |
251 | assert User.find(2).lock! |
|
269 | assert User.find(2).lock! | |
252 |
|
270 | |||
253 | assert_no_difference 'Token.count' do |
|
271 | assert_no_difference 'Token.count' do | |
254 | post :lost_password, :mail => 'JSmith@somenet.foo' |
|
272 | post :lost_password, :mail => 'JSmith@somenet.foo' | |
255 | assert_redirected_to '/account/lost_password' |
|
273 | assert_redirected_to '/account/lost_password' | |
256 | end |
|
274 | end | |
257 | end |
|
275 | end | |
258 |
|
276 | |||
259 | def test_lost_password_for_user_who_cannot_change_password_should_fail |
|
277 | def test_lost_password_for_user_who_cannot_change_password_should_fail | |
260 | User.any_instance.stubs(:change_password_allowed?).returns(false) |
|
278 | User.any_instance.stubs(:change_password_allowed?).returns(false) | |
261 |
|
279 | |||
262 | assert_no_difference 'Token.count' do |
|
280 | assert_no_difference 'Token.count' do | |
263 | post :lost_password, :mail => 'JSmith@somenet.foo' |
|
281 | post :lost_password, :mail => 'JSmith@somenet.foo' | |
264 | assert_response :success |
|
282 | assert_response :success | |
265 | end |
|
283 | end | |
266 | end |
|
284 | end | |
267 |
|
285 | |||
268 | def test_get_lost_password_with_token_should_display_the_password_recovery_form |
|
286 | def test_get_lost_password_with_token_should_display_the_password_recovery_form | |
269 | user = User.find(2) |
|
287 | user = User.find(2) | |
270 | token = Token.create!(:action => 'recovery', :user => user) |
|
288 | token = Token.create!(:action => 'recovery', :user => user) | |
271 |
|
289 | |||
272 | get :lost_password, :token => token.value |
|
290 | get :lost_password, :token => token.value | |
273 | assert_response :success |
|
291 | assert_response :success | |
274 | assert_template 'password_recovery' |
|
292 | assert_template 'password_recovery' | |
275 |
|
293 | |||
276 | assert_select 'input[type=hidden][name=token][value=?]', token.value |
|
294 | assert_select 'input[type=hidden][name=token][value=?]', token.value | |
277 | end |
|
295 | end | |
278 |
|
296 | |||
279 | def test_get_lost_password_with_invalid_token_should_redirect |
|
297 | def test_get_lost_password_with_invalid_token_should_redirect | |
280 | get :lost_password, :token => "abcdef" |
|
298 | get :lost_password, :token => "abcdef" | |
281 | assert_redirected_to '/' |
|
299 | assert_redirected_to '/' | |
282 | end |
|
300 | end | |
283 |
|
301 | |||
284 | def test_post_lost_password_with_token_should_change_the_user_password |
|
302 | def test_post_lost_password_with_token_should_change_the_user_password | |
285 | user = User.find(2) |
|
303 | user = User.find(2) | |
286 | token = Token.create!(:action => 'recovery', :user => user) |
|
304 | token = Token.create!(:action => 'recovery', :user => user) | |
287 |
|
305 | |||
288 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
|
306 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
289 | assert_redirected_to '/login' |
|
307 | assert_redirected_to '/login' | |
290 | user.reload |
|
308 | user.reload | |
291 | assert user.check_password?('newpass123') |
|
309 | assert user.check_password?('newpass123') | |
292 | assert_nil Token.find_by_id(token.id), "Token was not deleted" |
|
310 | assert_nil Token.find_by_id(token.id), "Token was not deleted" | |
293 | end |
|
311 | end | |
294 |
|
312 | |||
295 | def test_post_lost_password_with_token_for_non_active_user_should_fail |
|
313 | def test_post_lost_password_with_token_for_non_active_user_should_fail | |
296 | user = User.find(2) |
|
314 | user = User.find(2) | |
297 | token = Token.create!(:action => 'recovery', :user => user) |
|
315 | token = Token.create!(:action => 'recovery', :user => user) | |
298 | user.lock! |
|
316 | user.lock! | |
299 |
|
317 | |||
300 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
|
318 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
301 | assert_redirected_to '/' |
|
319 | assert_redirected_to '/' | |
302 | assert ! user.check_password?('newpass123') |
|
320 | assert ! user.check_password?('newpass123') | |
303 | end |
|
321 | end | |
304 |
|
322 | |||
305 | def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form |
|
323 | def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form | |
306 | user = User.find(2) |
|
324 | user = User.find(2) | |
307 | token = Token.create!(:action => 'recovery', :user => user) |
|
325 | token = Token.create!(:action => 'recovery', :user => user) | |
308 |
|
326 | |||
309 | post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' |
|
327 | post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' | |
310 | assert_response :success |
|
328 | assert_response :success | |
311 | assert_template 'password_recovery' |
|
329 | assert_template 'password_recovery' | |
312 | assert_not_nil Token.find_by_id(token.id), "Token was deleted" |
|
330 | assert_not_nil Token.find_by_id(token.id), "Token was deleted" | |
313 |
|
331 | |||
314 | assert_select 'input[type=hidden][name=token][value=?]', token.value |
|
332 | assert_select 'input[type=hidden][name=token][value=?]', token.value | |
315 | end |
|
333 | end | |
316 |
|
334 | |||
317 | def test_post_lost_password_with_invalid_token_should_redirect |
|
335 | def test_post_lost_password_with_invalid_token_should_redirect | |
318 | post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' |
|
336 | post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' | |
319 | assert_redirected_to '/' |
|
337 | assert_redirected_to '/' | |
320 | end |
|
338 | end | |
321 |
|
339 | |||
322 | def test_activation_email_should_send_an_activation_email |
|
340 | def test_activation_email_should_send_an_activation_email | |
323 | User.find(2).update_attribute :status, User::STATUS_REGISTERED |
|
341 | User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
324 | @request.session[:registered_user_id] = 2 |
|
342 | @request.session[:registered_user_id] = 2 | |
325 |
|
343 | |||
326 | with_settings :self_registration => '1' do |
|
344 | with_settings :self_registration => '1' do | |
327 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
345 | assert_difference 'ActionMailer::Base.deliveries.size' do | |
328 | get :activation_email |
|
346 | get :activation_email | |
329 | assert_redirected_to '/login' |
|
347 | assert_redirected_to '/login' | |
330 | end |
|
348 | end | |
331 | end |
|
349 | end | |
332 | end |
|
350 | end | |
333 |
|
351 | |||
334 | def test_activation_email_without_session_data_should_fail |
|
352 | def test_activation_email_without_session_data_should_fail | |
335 | User.find(2).update_attribute :status, User::STATUS_REGISTERED |
|
353 | User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
336 |
|
354 | |||
337 | with_settings :self_registration => '1' do |
|
355 | with_settings :self_registration => '1' do | |
338 | assert_no_difference 'ActionMailer::Base.deliveries.size' do |
|
356 | assert_no_difference 'ActionMailer::Base.deliveries.size' do | |
339 | get :activation_email |
|
357 | get :activation_email | |
340 | assert_redirected_to '/' |
|
358 | assert_redirected_to '/' | |
341 | end |
|
359 | end | |
342 | end |
|
360 | end | |
343 | end |
|
361 | end | |
344 | end |
|
362 | end |
General Comments 0
You need to be logged in to leave comments.
Login now