@@ -1,296 +1,298 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class 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 |
|
23 | skip_before_filter :check_if_login_required | |
24 |
|
24 | |||
25 | # Login request and validation |
|
25 | # Login request and validation | |
26 | def login |
|
26 | def login | |
27 | if request.get? |
|
27 | if request.get? | |
28 | logout_user |
|
28 | if User.current.logged? | |
|
29 | redirect_to home_url | |||
|
30 | end | |||
29 | else |
|
31 | else | |
30 | authenticate_user |
|
32 | authenticate_user | |
31 | end |
|
33 | end | |
32 | rescue AuthSourceException => e |
|
34 | rescue AuthSourceException => e | |
33 | logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" |
|
35 | logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" | |
34 | render_error :message => e.message |
|
36 | render_error :message => e.message | |
35 | end |
|
37 | end | |
36 |
|
38 | |||
37 | # Log out current user and redirect to welcome page |
|
39 | # Log out current user and redirect to welcome page | |
38 | def logout |
|
40 | def logout | |
39 | logout_user |
|
41 | logout_user | |
40 | redirect_to home_url |
|
42 | redirect_to home_url | |
41 | end |
|
43 | end | |
42 |
|
44 | |||
43 | # Lets user choose a new password |
|
45 | # Lets user choose a new password | |
44 | def lost_password |
|
46 | def lost_password | |
45 | redirect_to(home_url) && return unless Setting.lost_password? |
|
47 | redirect_to(home_url) && return unless Setting.lost_password? | |
46 | if params[:token] |
|
48 | if params[:token] | |
47 | @token = Token.find_by_action_and_value("recovery", params[:token].to_s) |
|
49 | @token = Token.find_by_action_and_value("recovery", params[:token].to_s) | |
48 | if @token.nil? || @token.expired? |
|
50 | if @token.nil? || @token.expired? | |
49 | redirect_to home_url |
|
51 | redirect_to home_url | |
50 | return |
|
52 | return | |
51 | end |
|
53 | end | |
52 | @user = @token.user |
|
54 | @user = @token.user | |
53 | unless @user && @user.active? |
|
55 | unless @user && @user.active? | |
54 | redirect_to home_url |
|
56 | redirect_to home_url | |
55 | return |
|
57 | return | |
56 | end |
|
58 | end | |
57 | if request.post? |
|
59 | if request.post? | |
58 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
60 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | |
59 | if @user.save |
|
61 | if @user.save | |
60 | @token.destroy |
|
62 | @token.destroy | |
61 | flash[:notice] = l(:notice_account_password_updated) |
|
63 | flash[:notice] = l(:notice_account_password_updated) | |
62 | redirect_to signin_path |
|
64 | redirect_to signin_path | |
63 | return |
|
65 | return | |
64 | end |
|
66 | end | |
65 | end |
|
67 | end | |
66 | render :template => "account/password_recovery" |
|
68 | render :template => "account/password_recovery" | |
67 | return |
|
69 | return | |
68 | else |
|
70 | else | |
69 | if request.post? |
|
71 | if request.post? | |
70 | user = User.find_by_mail(params[:mail].to_s) |
|
72 | user = User.find_by_mail(params[:mail].to_s) | |
71 | # user not found or not active |
|
73 | # user not found or not active | |
72 | unless user && user.active? |
|
74 | unless user && user.active? | |
73 | flash.now[:error] = l(:notice_account_unknown_email) |
|
75 | flash.now[:error] = l(:notice_account_unknown_email) | |
74 | return |
|
76 | return | |
75 | end |
|
77 | end | |
76 | # user cannot change its password |
|
78 | # user cannot change its password | |
77 | unless user.change_password_allowed? |
|
79 | unless user.change_password_allowed? | |
78 | flash.now[:error] = l(:notice_can_t_change_password) |
|
80 | flash.now[:error] = l(:notice_can_t_change_password) | |
79 | return |
|
81 | return | |
80 | end |
|
82 | end | |
81 | # create a new token for password recovery |
|
83 | # create a new token for password recovery | |
82 | token = Token.new(:user => user, :action => "recovery") |
|
84 | token = Token.new(:user => user, :action => "recovery") | |
83 | if token.save |
|
85 | if token.save | |
84 | Mailer.lost_password(token).deliver |
|
86 | Mailer.lost_password(token).deliver | |
85 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
87 | flash[:notice] = l(:notice_account_lost_email_sent) | |
86 | redirect_to signin_path |
|
88 | redirect_to signin_path | |
87 | return |
|
89 | return | |
88 | end |
|
90 | end | |
89 | end |
|
91 | end | |
90 | end |
|
92 | end | |
91 | end |
|
93 | end | |
92 |
|
94 | |||
93 | # User self-registration |
|
95 | # User self-registration | |
94 | def register |
|
96 | def register | |
95 | redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] |
|
97 | redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] | |
96 | if request.get? |
|
98 | if request.get? | |
97 | session[:auth_source_registration] = nil |
|
99 | session[:auth_source_registration] = nil | |
98 | @user = User.new(:language => current_language.to_s) |
|
100 | @user = User.new(:language => current_language.to_s) | |
99 | else |
|
101 | else | |
100 | user_params = params[:user] || {} |
|
102 | user_params = params[:user] || {} | |
101 | @user = User.new |
|
103 | @user = User.new | |
102 | @user.safe_attributes = user_params |
|
104 | @user.safe_attributes = user_params | |
103 | @user.admin = false |
|
105 | @user.admin = false | |
104 | @user.register |
|
106 | @user.register | |
105 | if session[:auth_source_registration] |
|
107 | if session[:auth_source_registration] | |
106 | @user.activate |
|
108 | @user.activate | |
107 | @user.login = session[:auth_source_registration][:login] |
|
109 | @user.login = session[:auth_source_registration][:login] | |
108 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
|
110 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] | |
109 | if @user.save |
|
111 | if @user.save | |
110 | session[:auth_source_registration] = nil |
|
112 | session[:auth_source_registration] = nil | |
111 | self.logged_user = @user |
|
113 | self.logged_user = @user | |
112 | flash[:notice] = l(:notice_account_activated) |
|
114 | flash[:notice] = l(:notice_account_activated) | |
113 | redirect_to my_account_path |
|
115 | redirect_to my_account_path | |
114 | end |
|
116 | end | |
115 | else |
|
117 | else | |
116 | @user.login = params[:user][:login] |
|
118 | @user.login = params[:user][:login] | |
117 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? |
|
119 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? | |
118 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] |
|
120 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] | |
119 | end |
|
121 | end | |
120 |
|
122 | |||
121 | case Setting.self_registration |
|
123 | case Setting.self_registration | |
122 | when '1' |
|
124 | when '1' | |
123 | register_by_email_activation(@user) |
|
125 | register_by_email_activation(@user) | |
124 | when '3' |
|
126 | when '3' | |
125 | register_automatically(@user) |
|
127 | register_automatically(@user) | |
126 | else |
|
128 | else | |
127 | register_manually_by_administrator(@user) |
|
129 | register_manually_by_administrator(@user) | |
128 | end |
|
130 | end | |
129 | end |
|
131 | end | |
130 | end |
|
132 | end | |
131 | end |
|
133 | end | |
132 |
|
134 | |||
133 | # Token based account activation |
|
135 | # Token based account activation | |
134 | def activate |
|
136 | def activate | |
135 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] |
|
137 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] | |
136 | token = Token.find_by_action_and_value('register', params[:token]) |
|
138 | token = Token.find_by_action_and_value('register', params[:token]) | |
137 | redirect_to(home_url) && return unless token and !token.expired? |
|
139 | redirect_to(home_url) && return unless token and !token.expired? | |
138 | user = token.user |
|
140 | user = token.user | |
139 | redirect_to(home_url) && return unless user.registered? |
|
141 | redirect_to(home_url) && return unless user.registered? | |
140 | user.activate |
|
142 | user.activate | |
141 | if user.save |
|
143 | if user.save | |
142 | token.destroy |
|
144 | token.destroy | |
143 | flash[:notice] = l(:notice_account_activated) |
|
145 | flash[:notice] = l(:notice_account_activated) | |
144 | end |
|
146 | end | |
145 | redirect_to signin_path |
|
147 | redirect_to signin_path | |
146 | end |
|
148 | end | |
147 |
|
149 | |||
148 | private |
|
150 | private | |
149 |
|
151 | |||
150 | def authenticate_user |
|
152 | def authenticate_user | |
151 | if Setting.openid? && using_open_id? |
|
153 | if Setting.openid? && using_open_id? | |
152 | open_id_authenticate(params[:openid_url]) |
|
154 | open_id_authenticate(params[:openid_url]) | |
153 | else |
|
155 | else | |
154 | password_authentication |
|
156 | password_authentication | |
155 | end |
|
157 | end | |
156 | end |
|
158 | end | |
157 |
|
159 | |||
158 | def password_authentication |
|
160 | def password_authentication | |
159 | user = User.try_to_login(params[:username], params[:password]) |
|
161 | user = User.try_to_login(params[:username], params[:password]) | |
160 |
|
162 | |||
161 | if user.nil? |
|
163 | if user.nil? | |
162 | invalid_credentials |
|
164 | invalid_credentials | |
163 | elsif user.new_record? |
|
165 | elsif user.new_record? | |
164 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) |
|
166 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) | |
165 | else |
|
167 | else | |
166 | # Valid user |
|
168 | # Valid user | |
167 | successful_authentication(user) |
|
169 | successful_authentication(user) | |
168 | end |
|
170 | end | |
169 | end |
|
171 | end | |
170 |
|
172 | |||
171 | def open_id_authenticate(openid_url) |
|
173 | def open_id_authenticate(openid_url) | |
172 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration| |
|
174 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration| | |
173 | if result.successful? |
|
175 | if result.successful? | |
174 | user = User.find_or_initialize_by_identity_url(identity_url) |
|
176 | user = User.find_or_initialize_by_identity_url(identity_url) | |
175 | if user.new_record? |
|
177 | if user.new_record? | |
176 | # Self-registration off |
|
178 | # Self-registration off | |
177 | redirect_to(home_url) && return unless Setting.self_registration? |
|
179 | redirect_to(home_url) && return unless Setting.self_registration? | |
178 |
|
180 | |||
179 | # Create on the fly |
|
181 | # Create on the fly | |
180 | user.login = registration['nickname'] unless registration['nickname'].nil? |
|
182 | user.login = registration['nickname'] unless registration['nickname'].nil? | |
181 | user.mail = registration['email'] unless registration['email'].nil? |
|
183 | user.mail = registration['email'] unless registration['email'].nil? | |
182 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
|
184 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? | |
183 | user.random_password |
|
185 | user.random_password | |
184 | user.register |
|
186 | user.register | |
185 |
|
187 | |||
186 | case Setting.self_registration |
|
188 | case Setting.self_registration | |
187 | when '1' |
|
189 | when '1' | |
188 | register_by_email_activation(user) do |
|
190 | register_by_email_activation(user) do | |
189 | onthefly_creation_failed(user) |
|
191 | onthefly_creation_failed(user) | |
190 | end |
|
192 | end | |
191 | when '3' |
|
193 | when '3' | |
192 | register_automatically(user) do |
|
194 | register_automatically(user) do | |
193 | onthefly_creation_failed(user) |
|
195 | onthefly_creation_failed(user) | |
194 | end |
|
196 | end | |
195 | else |
|
197 | else | |
196 | register_manually_by_administrator(user) do |
|
198 | register_manually_by_administrator(user) do | |
197 | onthefly_creation_failed(user) |
|
199 | onthefly_creation_failed(user) | |
198 | end |
|
200 | end | |
199 | end |
|
201 | end | |
200 | else |
|
202 | else | |
201 | # Existing record |
|
203 | # Existing record | |
202 | if user.active? |
|
204 | if user.active? | |
203 | successful_authentication(user) |
|
205 | successful_authentication(user) | |
204 | else |
|
206 | else | |
205 | account_pending |
|
207 | account_pending | |
206 | end |
|
208 | end | |
207 | end |
|
209 | end | |
208 | end |
|
210 | end | |
209 | end |
|
211 | end | |
210 | end |
|
212 | end | |
211 |
|
213 | |||
212 | def successful_authentication(user) |
|
214 | def successful_authentication(user) | |
213 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" |
|
215 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" | |
214 | # Valid user |
|
216 | # Valid user | |
215 | self.logged_user = user |
|
217 | self.logged_user = user | |
216 | # generate a key and set cookie if autologin |
|
218 | # generate a key and set cookie if autologin | |
217 | if params[:autologin] && Setting.autologin? |
|
219 | if params[:autologin] && Setting.autologin? | |
218 | set_autologin_cookie(user) |
|
220 | set_autologin_cookie(user) | |
219 | end |
|
221 | end | |
220 | call_hook(:controller_account_success_authentication_after, {:user => user }) |
|
222 | call_hook(:controller_account_success_authentication_after, {:user => user }) | |
221 | redirect_back_or_default my_page_path |
|
223 | redirect_back_or_default my_page_path | |
222 | end |
|
224 | end | |
223 |
|
225 | |||
224 | def set_autologin_cookie(user) |
|
226 | def set_autologin_cookie(user) | |
225 | token = Token.create(:user => user, :action => 'autologin') |
|
227 | token = Token.create(:user => user, :action => 'autologin') | |
226 | cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin' |
|
228 | cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin' | |
227 | cookie_options = { |
|
229 | cookie_options = { | |
228 | :value => token.value, |
|
230 | :value => token.value, | |
229 | :expires => 1.year.from_now, |
|
231 | :expires => 1.year.from_now, | |
230 | :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), |
|
232 | :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), | |
231 | :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), |
|
233 | :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), | |
232 | :httponly => true |
|
234 | :httponly => true | |
233 | } |
|
235 | } | |
234 | cookies[cookie_name] = cookie_options |
|
236 | cookies[cookie_name] = cookie_options | |
235 | end |
|
237 | end | |
236 |
|
238 | |||
237 | # Onthefly creation failed, display the registration form to fill/fix attributes |
|
239 | # Onthefly creation failed, display the registration form to fill/fix attributes | |
238 | def onthefly_creation_failed(user, auth_source_options = { }) |
|
240 | def onthefly_creation_failed(user, auth_source_options = { }) | |
239 | @user = user |
|
241 | @user = user | |
240 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? |
|
242 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? | |
241 | render :action => 'register' |
|
243 | render :action => 'register' | |
242 | end |
|
244 | end | |
243 |
|
245 | |||
244 | def invalid_credentials |
|
246 | def invalid_credentials | |
245 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" |
|
247 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" | |
246 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
248 | flash.now[:error] = l(:notice_account_invalid_creditentials) | |
247 | end |
|
249 | end | |
248 |
|
250 | |||
249 | # Register a user for email activation. |
|
251 | # Register a user for email activation. | |
250 | # |
|
252 | # | |
251 | # Pass a block for behavior when a user fails to save |
|
253 | # Pass a block for behavior when a user fails to save | |
252 | def register_by_email_activation(user, &block) |
|
254 | def register_by_email_activation(user, &block) | |
253 | token = Token.new(:user => user, :action => "register") |
|
255 | token = Token.new(:user => user, :action => "register") | |
254 | if user.save and token.save |
|
256 | if user.save and token.save | |
255 | Mailer.register(token).deliver |
|
257 | Mailer.register(token).deliver | |
256 | flash[:notice] = l(:notice_account_register_done) |
|
258 | flash[:notice] = l(:notice_account_register_done) | |
257 | redirect_to signin_path |
|
259 | redirect_to signin_path | |
258 | else |
|
260 | else | |
259 | yield if block_given? |
|
261 | yield if block_given? | |
260 | end |
|
262 | end | |
261 | end |
|
263 | end | |
262 |
|
264 | |||
263 | # Automatically register a user |
|
265 | # Automatically register a user | |
264 | # |
|
266 | # | |
265 | # Pass a block for behavior when a user fails to save |
|
267 | # Pass a block for behavior when a user fails to save | |
266 | def register_automatically(user, &block) |
|
268 | def register_automatically(user, &block) | |
267 | # Automatic activation |
|
269 | # Automatic activation | |
268 | user.activate |
|
270 | user.activate | |
269 | user.last_login_on = Time.now |
|
271 | user.last_login_on = Time.now | |
270 | if user.save |
|
272 | if user.save | |
271 | self.logged_user = user |
|
273 | self.logged_user = user | |
272 | flash[:notice] = l(:notice_account_activated) |
|
274 | flash[:notice] = l(:notice_account_activated) | |
273 | redirect_to my_account_path |
|
275 | redirect_to my_account_path | |
274 | else |
|
276 | else | |
275 | yield if block_given? |
|
277 | yield if block_given? | |
276 | end |
|
278 | end | |
277 | end |
|
279 | end | |
278 |
|
280 | |||
279 | # Manual activation by the administrator |
|
281 | # Manual activation by the administrator | |
280 | # |
|
282 | # | |
281 | # Pass a block for behavior when a user fails to save |
|
283 | # Pass a block for behavior when a user fails to save | |
282 | def register_manually_by_administrator(user, &block) |
|
284 | def register_manually_by_administrator(user, &block) | |
283 | if user.save |
|
285 | if user.save | |
284 | # Sends an email to the administrators |
|
286 | # Sends an email to the administrators | |
285 | Mailer.account_activation_request(user).deliver |
|
287 | Mailer.account_activation_request(user).deliver | |
286 | account_pending |
|
288 | account_pending | |
287 | else |
|
289 | else | |
288 | yield if block_given? |
|
290 | yield if block_given? | |
289 | end |
|
291 | end | |
290 | end |
|
292 | end | |
291 |
|
293 | |||
292 | def account_pending |
|
294 | def account_pending | |
293 | flash[:notice] = l(:notice_account_pending) |
|
295 | flash[:notice] = l(:notice_account_pending) | |
294 | redirect_to signin_path |
|
296 | redirect_to signin_path | |
295 | end |
|
297 | end | |
296 | end |
|
298 | end |
@@ -1,260 +1,268 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | 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_home | |||
|
37 | @request.session[:user_id] = 2 | |||
|
38 | ||||
|
39 | get :login | |||
|
40 | assert_redirected_to '/' | |||
|
41 | assert_equal 2, @request.session[:user_id] | |||
|
42 | end | |||
|
43 | ||||
36 | def test_login_should_redirect_to_back_url_param |
|
44 | def test_login_should_redirect_to_back_url_param | |
37 | # request.uri is "test.host" in test environment |
|
45 | # request.uri is "test.host" in test environment | |
38 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1' |
|
46 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1' | |
39 | assert_redirected_to '/issues/show/1' |
|
47 | assert_redirected_to '/issues/show/1' | |
40 | end |
|
48 | end | |
41 |
|
49 | |||
42 | def test_login_should_not_redirect_to_another_host |
|
50 | def test_login_should_not_redirect_to_another_host | |
43 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake' |
|
51 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake' | |
44 | assert_redirected_to '/my/page' |
|
52 | assert_redirected_to '/my/page' | |
45 | end |
|
53 | end | |
46 |
|
54 | |||
47 | def test_login_with_wrong_password |
|
55 | def test_login_with_wrong_password | |
48 | post :login, :username => 'admin', :password => 'bad' |
|
56 | post :login, :username => 'admin', :password => 'bad' | |
49 | assert_response :success |
|
57 | assert_response :success | |
50 | assert_template 'login' |
|
58 | assert_template 'login' | |
51 |
|
59 | |||
52 | assert_select 'div.flash.error', :text => /Invalid user or password/ |
|
60 | assert_select 'div.flash.error', :text => /Invalid user or password/ | |
53 | assert_select 'input[name=username][value=admin]' |
|
61 | assert_select 'input[name=username][value=admin]' | |
54 | assert_select 'input[name=password]' |
|
62 | assert_select 'input[name=password]' | |
55 | assert_select 'input[name=password][value]', 0 |
|
63 | assert_select 'input[name=password][value]', 0 | |
56 | end |
|
64 | end | |
57 |
|
65 | |||
58 | def test_login_should_rescue_auth_source_exception |
|
66 | def test_login_should_rescue_auth_source_exception | |
59 | source = AuthSource.create!(:name => 'Test') |
|
67 | source = AuthSource.create!(:name => 'Test') | |
60 | User.find(2).update_attribute :auth_source_id, source.id |
|
68 | User.find(2).update_attribute :auth_source_id, source.id | |
61 | AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) |
|
69 | AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) | |
62 |
|
70 | |||
63 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
71 | post :login, :username => 'jsmith', :password => 'jsmith' | |
64 | assert_response 500 |
|
72 | assert_response 500 | |
65 | assert_error_tag :content => /Something wrong/ |
|
73 | assert_error_tag :content => /Something wrong/ | |
66 | end |
|
74 | end | |
67 |
|
75 | |||
68 | def test_login_should_reset_session |
|
76 | def test_login_should_reset_session | |
69 | @controller.expects(:reset_session).once |
|
77 | @controller.expects(:reset_session).once | |
70 |
|
78 | |||
71 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
79 | post :login, :username => 'jsmith', :password => 'jsmith' | |
72 | assert_response 302 |
|
80 | assert_response 302 | |
73 | end |
|
81 | end | |
74 |
|
82 | |||
75 | def test_logout |
|
83 | def test_logout | |
76 | @request.session[:user_id] = 2 |
|
84 | @request.session[:user_id] = 2 | |
77 | get :logout |
|
85 | get :logout | |
78 | assert_redirected_to '/' |
|
86 | assert_redirected_to '/' | |
79 | assert_nil @request.session[:user_id] |
|
87 | assert_nil @request.session[:user_id] | |
80 | end |
|
88 | end | |
81 |
|
89 | |||
82 | def test_logout_should_reset_session |
|
90 | def test_logout_should_reset_session | |
83 | @controller.expects(:reset_session).once |
|
91 | @controller.expects(:reset_session).once | |
84 |
|
92 | |||
85 | @request.session[:user_id] = 2 |
|
93 | @request.session[:user_id] = 2 | |
86 | get :logout |
|
94 | get :logout | |
87 | assert_response 302 |
|
95 | assert_response 302 | |
88 | end |
|
96 | end | |
89 |
|
97 | |||
90 | def test_get_register_with_registration_on |
|
98 | def test_get_register_with_registration_on | |
91 | with_settings :self_registration => '3' do |
|
99 | with_settings :self_registration => '3' do | |
92 | get :register |
|
100 | get :register | |
93 | assert_response :success |
|
101 | assert_response :success | |
94 | assert_template 'register' |
|
102 | assert_template 'register' | |
95 | assert_not_nil assigns(:user) |
|
103 | assert_not_nil assigns(:user) | |
96 |
|
104 | |||
97 | assert_select 'input[name=?]', 'user[password]' |
|
105 | assert_select 'input[name=?]', 'user[password]' | |
98 | assert_select 'input[name=?]', 'user[password_confirmation]' |
|
106 | assert_select 'input[name=?]', 'user[password_confirmation]' | |
99 | end |
|
107 | end | |
100 | end |
|
108 | end | |
101 |
|
109 | |||
102 | def test_get_register_should_detect_user_language |
|
110 | def test_get_register_should_detect_user_language | |
103 | with_settings :self_registration => '3' do |
|
111 | with_settings :self_registration => '3' do | |
104 | @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' |
|
112 | @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' | |
105 | get :register |
|
113 | get :register | |
106 | assert_response :success |
|
114 | assert_response :success | |
107 | assert_not_nil assigns(:user) |
|
115 | assert_not_nil assigns(:user) | |
108 | assert_equal 'fr', assigns(:user).language |
|
116 | assert_equal 'fr', assigns(:user).language | |
109 | assert_select 'select[name=?]', 'user[language]' do |
|
117 | assert_select 'select[name=?]', 'user[language]' do | |
110 | assert_select 'option[value=fr][selected=selected]' |
|
118 | assert_select 'option[value=fr][selected=selected]' | |
111 | end |
|
119 | end | |
112 | end |
|
120 | end | |
113 | end |
|
121 | end | |
114 |
|
122 | |||
115 | def test_get_register_with_registration_off_should_redirect |
|
123 | def test_get_register_with_registration_off_should_redirect | |
116 | with_settings :self_registration => '0' do |
|
124 | with_settings :self_registration => '0' do | |
117 | get :register |
|
125 | get :register | |
118 | assert_redirected_to '/' |
|
126 | assert_redirected_to '/' | |
119 | end |
|
127 | end | |
120 | end |
|
128 | end | |
121 |
|
129 | |||
122 | # See integration/account_test.rb for the full test |
|
130 | # See integration/account_test.rb for the full test | |
123 | def test_post_register_with_registration_on |
|
131 | def test_post_register_with_registration_on | |
124 | with_settings :self_registration => '3' do |
|
132 | with_settings :self_registration => '3' do | |
125 | assert_difference 'User.count' do |
|
133 | assert_difference 'User.count' do | |
126 | post :register, :user => { |
|
134 | post :register, :user => { | |
127 | :login => 'register', |
|
135 | :login => 'register', | |
128 | :password => 'secret123', |
|
136 | :password => 'secret123', | |
129 | :password_confirmation => 'secret123', |
|
137 | :password_confirmation => 'secret123', | |
130 | :firstname => 'John', |
|
138 | :firstname => 'John', | |
131 | :lastname => 'Doe', |
|
139 | :lastname => 'Doe', | |
132 | :mail => 'register@example.com' |
|
140 | :mail => 'register@example.com' | |
133 | } |
|
141 | } | |
134 | assert_redirected_to '/my/account' |
|
142 | assert_redirected_to '/my/account' | |
135 | end |
|
143 | end | |
136 | user = User.first(:order => 'id DESC') |
|
144 | user = User.first(:order => 'id DESC') | |
137 | assert_equal 'register', user.login |
|
145 | assert_equal 'register', user.login | |
138 | assert_equal 'John', user.firstname |
|
146 | assert_equal 'John', user.firstname | |
139 | assert_equal 'Doe', user.lastname |
|
147 | assert_equal 'Doe', user.lastname | |
140 | assert_equal 'register@example.com', user.mail |
|
148 | assert_equal 'register@example.com', user.mail | |
141 | assert user.check_password?('secret123') |
|
149 | assert user.check_password?('secret123') | |
142 | assert user.active? |
|
150 | assert user.active? | |
143 | end |
|
151 | end | |
144 | end |
|
152 | end | |
145 |
|
153 | |||
146 | def test_post_register_with_registration_off_should_redirect |
|
154 | def test_post_register_with_registration_off_should_redirect | |
147 | with_settings :self_registration => '0' do |
|
155 | with_settings :self_registration => '0' do | |
148 | assert_no_difference 'User.count' do |
|
156 | assert_no_difference 'User.count' do | |
149 | post :register, :user => { |
|
157 | post :register, :user => { | |
150 | :login => 'register', |
|
158 | :login => 'register', | |
151 | :password => 'test', |
|
159 | :password => 'test', | |
152 | :password_confirmation => 'test', |
|
160 | :password_confirmation => 'test', | |
153 | :firstname => 'John', |
|
161 | :firstname => 'John', | |
154 | :lastname => 'Doe', |
|
162 | :lastname => 'Doe', | |
155 | :mail => 'register@example.com' |
|
163 | :mail => 'register@example.com' | |
156 | } |
|
164 | } | |
157 | assert_redirected_to '/' |
|
165 | assert_redirected_to '/' | |
158 | end |
|
166 | end | |
159 | end |
|
167 | end | |
160 | end |
|
168 | end | |
161 |
|
169 | |||
162 | def test_get_lost_password_should_display_lost_password_form |
|
170 | def test_get_lost_password_should_display_lost_password_form | |
163 | get :lost_password |
|
171 | get :lost_password | |
164 | assert_response :success |
|
172 | assert_response :success | |
165 | assert_select 'input[name=mail]' |
|
173 | assert_select 'input[name=mail]' | |
166 | end |
|
174 | end | |
167 |
|
175 | |||
168 | def test_lost_password_for_active_user_should_create_a_token |
|
176 | def test_lost_password_for_active_user_should_create_a_token | |
169 | Token.delete_all |
|
177 | Token.delete_all | |
170 | ActionMailer::Base.deliveries.clear |
|
178 | ActionMailer::Base.deliveries.clear | |
171 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
179 | assert_difference 'ActionMailer::Base.deliveries.size' do | |
172 | assert_difference 'Token.count' do |
|
180 | assert_difference 'Token.count' do | |
173 | with_settings :host_name => 'mydomain.foo', :protocol => 'http' do |
|
181 | with_settings :host_name => 'mydomain.foo', :protocol => 'http' do | |
174 | post :lost_password, :mail => 'JSmith@somenet.foo' |
|
182 | post :lost_password, :mail => 'JSmith@somenet.foo' | |
175 | assert_redirected_to '/login' |
|
183 | assert_redirected_to '/login' | |
176 | end |
|
184 | end | |
177 | end |
|
185 | end | |
178 | end |
|
186 | end | |
179 |
|
187 | |||
180 | token = Token.order('id DESC').first |
|
188 | token = Token.order('id DESC').first | |
181 | assert_equal User.find(2), token.user |
|
189 | assert_equal User.find(2), token.user | |
182 | assert_equal 'recovery', token.action |
|
190 | assert_equal 'recovery', token.action | |
183 |
|
191 | |||
184 | assert_select_email do |
|
192 | assert_select_email do | |
185 | assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}" |
|
193 | assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}" | |
186 | end |
|
194 | end | |
187 | end |
|
195 | end | |
188 |
|
196 | |||
189 | def test_lost_password_for_unknown_user_should_fail |
|
197 | def test_lost_password_for_unknown_user_should_fail | |
190 | Token.delete_all |
|
198 | Token.delete_all | |
191 | assert_no_difference 'Token.count' do |
|
199 | assert_no_difference 'Token.count' do | |
192 | post :lost_password, :mail => 'invalid@somenet.foo' |
|
200 | post :lost_password, :mail => 'invalid@somenet.foo' | |
193 | assert_response :success |
|
201 | assert_response :success | |
194 | end |
|
202 | end | |
195 | end |
|
203 | end | |
196 |
|
204 | |||
197 | def test_lost_password_for_non_active_user_should_fail |
|
205 | def test_lost_password_for_non_active_user_should_fail | |
198 | Token.delete_all |
|
206 | Token.delete_all | |
199 | assert User.find(2).lock! |
|
207 | assert User.find(2).lock! | |
200 |
|
208 | |||
201 | assert_no_difference 'Token.count' do |
|
209 | assert_no_difference 'Token.count' do | |
202 | post :lost_password, :mail => 'JSmith@somenet.foo' |
|
210 | post :lost_password, :mail => 'JSmith@somenet.foo' | |
203 | assert_response :success |
|
211 | assert_response :success | |
204 | end |
|
212 | end | |
205 | end |
|
213 | end | |
206 |
|
214 | |||
207 | def test_get_lost_password_with_token_should_display_the_password_recovery_form |
|
215 | def test_get_lost_password_with_token_should_display_the_password_recovery_form | |
208 | user = User.find(2) |
|
216 | user = User.find(2) | |
209 | token = Token.create!(:action => 'recovery', :user => user) |
|
217 | token = Token.create!(:action => 'recovery', :user => user) | |
210 |
|
218 | |||
211 | get :lost_password, :token => token.value |
|
219 | get :lost_password, :token => token.value | |
212 | assert_response :success |
|
220 | assert_response :success | |
213 | assert_template 'password_recovery' |
|
221 | assert_template 'password_recovery' | |
214 |
|
222 | |||
215 | assert_select 'input[type=hidden][name=token][value=?]', token.value |
|
223 | assert_select 'input[type=hidden][name=token][value=?]', token.value | |
216 | end |
|
224 | end | |
217 |
|
225 | |||
218 | def test_get_lost_password_with_invalid_token_should_redirect |
|
226 | def test_get_lost_password_with_invalid_token_should_redirect | |
219 | get :lost_password, :token => "abcdef" |
|
227 | get :lost_password, :token => "abcdef" | |
220 | assert_redirected_to '/' |
|
228 | assert_redirected_to '/' | |
221 | end |
|
229 | end | |
222 |
|
230 | |||
223 | def test_post_lost_password_with_token_should_change_the_user_password |
|
231 | def test_post_lost_password_with_token_should_change_the_user_password | |
224 | user = User.find(2) |
|
232 | user = User.find(2) | |
225 | token = Token.create!(:action => 'recovery', :user => user) |
|
233 | token = Token.create!(:action => 'recovery', :user => user) | |
226 |
|
234 | |||
227 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
|
235 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
228 | assert_redirected_to '/login' |
|
236 | assert_redirected_to '/login' | |
229 | user.reload |
|
237 | user.reload | |
230 | assert user.check_password?('newpass123') |
|
238 | assert user.check_password?('newpass123') | |
231 | assert_nil Token.find_by_id(token.id), "Token was not deleted" |
|
239 | assert_nil Token.find_by_id(token.id), "Token was not deleted" | |
232 | end |
|
240 | end | |
233 |
|
241 | |||
234 | def test_post_lost_password_with_token_for_non_active_user_should_fail |
|
242 | def test_post_lost_password_with_token_for_non_active_user_should_fail | |
235 | user = User.find(2) |
|
243 | user = User.find(2) | |
236 | token = Token.create!(:action => 'recovery', :user => user) |
|
244 | token = Token.create!(:action => 'recovery', :user => user) | |
237 | user.lock! |
|
245 | user.lock! | |
238 |
|
246 | |||
239 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
|
247 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
240 | assert_redirected_to '/' |
|
248 | assert_redirected_to '/' | |
241 | assert ! user.check_password?('newpass123') |
|
249 | assert ! user.check_password?('newpass123') | |
242 | end |
|
250 | end | |
243 |
|
251 | |||
244 | def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form |
|
252 | def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form | |
245 | user = User.find(2) |
|
253 | user = User.find(2) | |
246 | token = Token.create!(:action => 'recovery', :user => user) |
|
254 | token = Token.create!(:action => 'recovery', :user => user) | |
247 |
|
255 | |||
248 | post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' |
|
256 | post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' | |
249 | assert_response :success |
|
257 | assert_response :success | |
250 | assert_template 'password_recovery' |
|
258 | assert_template 'password_recovery' | |
251 | assert_not_nil Token.find_by_id(token.id), "Token was deleted" |
|
259 | assert_not_nil Token.find_by_id(token.id), "Token was deleted" | |
252 |
|
260 | |||
253 | assert_select 'input[type=hidden][name=token][value=?]', token.value |
|
261 | assert_select 'input[type=hidden][name=token][value=?]', token.value | |
254 | end |
|
262 | end | |
255 |
|
263 | |||
256 | def test_post_lost_password_with_invalid_token_should_redirect |
|
264 | def test_post_lost_password_with_invalid_token_should_redirect | |
257 | post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' |
|
265 | post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' | |
258 | assert_redirected_to '/' |
|
266 | assert_redirected_to '/' | |
259 | end |
|
267 | end | |
260 | end |
|
268 | end |
General Comments 0
You need to be logged in to leave comments.
Login now