@@ -1,235 +1,269 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2008 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class AccountController < ApplicationController |
|
19 | 19 | helper :custom_fields |
|
20 | 20 | include CustomFieldsHelper |
|
21 | 21 | |
|
22 | 22 | # prevents login action to be filtered by check_if_login_required application scope filter |
|
23 | 23 | skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate] |
|
24 | 24 | |
|
25 | 25 | # Show user's account |
|
26 | 26 | def show |
|
27 | 27 | @user = User.active.find(params[:id]) |
|
28 | 28 | @custom_values = @user.custom_values |
|
29 | 29 | |
|
30 | 30 | # show only public projects and private projects that the logged in user is also a member of |
|
31 | 31 | @memberships = @user.memberships.select do |membership| |
|
32 | 32 | membership.project.is_public? || (User.current.member_of?(membership.project)) |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
|
36 | 36 | @events_by_day = events.group_by(&:event_date) |
|
37 | 37 | |
|
38 | 38 | rescue ActiveRecord::RecordNotFound |
|
39 | 39 | render_404 |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | # Login request and validation |
|
43 | 43 | def login |
|
44 | 44 | if request.get? |
|
45 | 45 | # Logout user |
|
46 | 46 | self.logged_user = nil |
|
47 | 47 | else |
|
48 | 48 | # Authenticate user |
|
49 | 49 | unless using_open_id? |
|
50 | 50 | password_authentication |
|
51 | 51 | else |
|
52 | 52 | open_id_authenticate(params[:openid_url]) |
|
53 | 53 | end |
|
54 | 54 | end |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | # Log out current user and redirect to welcome page |
|
58 | 58 | def logout |
|
59 | 59 | cookies.delete :autologin |
|
60 | 60 | Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged? |
|
61 | 61 | self.logged_user = nil |
|
62 | 62 | redirect_to home_url |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | # Enable user to choose a new password |
|
66 | 66 | def lost_password |
|
67 | 67 | redirect_to(home_url) && return unless Setting.lost_password? |
|
68 | 68 | if params[:token] |
|
69 | 69 | @token = Token.find_by_action_and_value("recovery", params[:token]) |
|
70 | 70 | redirect_to(home_url) && return unless @token and !@token.expired? |
|
71 | 71 | @user = @token.user |
|
72 | 72 | if request.post? |
|
73 | 73 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
74 | 74 | if @user.save |
|
75 | 75 | @token.destroy |
|
76 | 76 | flash[:notice] = l(:notice_account_password_updated) |
|
77 | 77 | redirect_to :action => 'login' |
|
78 | 78 | return |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | render :template => "account/password_recovery" |
|
82 | 82 | return |
|
83 | 83 | else |
|
84 | 84 | if request.post? |
|
85 | 85 | user = User.find_by_mail(params[:mail]) |
|
86 | 86 | # user not found in db |
|
87 | 87 | flash.now[:error] = l(:notice_account_unknown_email) and return unless user |
|
88 | 88 | # user uses an external authentification |
|
89 | 89 | flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id |
|
90 | 90 | # create a new token for password recovery |
|
91 | 91 | token = Token.new(:user => user, :action => "recovery") |
|
92 | 92 | if token.save |
|
93 | 93 | Mailer.deliver_lost_password(token) |
|
94 | 94 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
95 | 95 | redirect_to :action => 'login' |
|
96 | 96 | return |
|
97 | 97 | end |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | |
|
102 | 102 | # User self-registration |
|
103 | 103 | def register |
|
104 | 104 | redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] |
|
105 | 105 | if request.get? |
|
106 | 106 | session[:auth_source_registration] = nil |
|
107 | 107 | @user = User.new(:language => Setting.default_language) |
|
108 | 108 | else |
|
109 | 109 | @user = User.new(params[:user]) |
|
110 | 110 | @user.admin = false |
|
111 | 111 | @user.status = User::STATUS_REGISTERED |
|
112 | 112 | if session[:auth_source_registration] |
|
113 | 113 | @user.status = User::STATUS_ACTIVE |
|
114 | 114 | @user.login = session[:auth_source_registration][:login] |
|
115 | 115 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
|
116 | 116 | if @user.save |
|
117 | 117 | session[:auth_source_registration] = nil |
|
118 | 118 | self.logged_user = @user |
|
119 | 119 | flash[:notice] = l(:notice_account_activated) |
|
120 | 120 | redirect_to :controller => 'my', :action => 'account' |
|
121 | 121 | end |
|
122 | 122 | else |
|
123 | 123 | @user.login = params[:user][:login] |
|
124 | 124 | @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
|
125 | # TODO: Duplicated in open_id_authenticate action. A good sized refactoring would be good here | |
|
125 | 126 | case Setting.self_registration |
|
126 | 127 | when '1' |
|
127 | 128 | # Email activation |
|
128 | 129 | token = Token.new(:user => @user, :action => "register") |
|
129 | 130 | if @user.save and token.save |
|
130 | 131 | Mailer.deliver_register(token) |
|
131 | 132 | flash[:notice] = l(:notice_account_register_done) |
|
132 | 133 | redirect_to :action => 'login' |
|
133 | 134 | end |
|
134 | 135 | when '3' |
|
135 | 136 | # Automatic activation |
|
136 | 137 | @user.status = User::STATUS_ACTIVE |
|
137 | 138 | if @user.save |
|
138 | 139 | self.logged_user = @user |
|
139 | 140 | flash[:notice] = l(:notice_account_activated) |
|
140 | 141 | redirect_to :controller => 'my', :action => 'account' |
|
141 | 142 | end |
|
142 | 143 | else |
|
143 | 144 | # Manual activation by the administrator |
|
144 | 145 | if @user.save |
|
145 | 146 | # Sends an email to the administrators |
|
146 | 147 | Mailer.deliver_account_activation_request(@user) |
|
147 | 148 | flash[:notice] = l(:notice_account_pending) |
|
148 | 149 | redirect_to :action => 'login' |
|
149 | 150 | end |
|
150 | 151 | end |
|
151 | 152 | end |
|
152 | 153 | end |
|
153 | 154 | end |
|
154 | 155 | |
|
155 | 156 | # Token based account activation |
|
156 | 157 | def activate |
|
157 | 158 | redirect_to(home_url) && return unless Setting.self_registration? && params[:token] |
|
158 | 159 | token = Token.find_by_action_and_value('register', params[:token]) |
|
159 | 160 | redirect_to(home_url) && return unless token and !token.expired? |
|
160 | 161 | user = token.user |
|
161 | 162 | redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED |
|
162 | 163 | user.status = User::STATUS_ACTIVE |
|
163 | 164 | if user.save |
|
164 | 165 | token.destroy |
|
165 | 166 | flash[:notice] = l(:notice_account_activated) |
|
166 | 167 | end |
|
167 | 168 | redirect_to :action => 'login' |
|
168 | 169 | end |
|
169 | 170 | |
|
170 | 171 | private |
|
171 | 172 | def logged_user=(user) |
|
172 | 173 | if user && user.is_a?(User) |
|
173 | 174 | User.current = user |
|
174 | 175 | session[:user_id] = user.id |
|
175 | 176 | else |
|
176 | 177 | User.current = User.anonymous |
|
177 | 178 | session[:user_id] = nil |
|
178 | 179 | end |
|
179 | 180 | end |
|
180 | 181 | |
|
181 | 182 | def password_authentication |
|
182 | 183 | user = User.try_to_login(params[:username], params[:password]) |
|
183 | 184 | if user.nil? |
|
184 | 185 | # Invalid credentials |
|
185 | 186 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
186 | 187 | elsif user.new_record? |
|
187 | 188 | # Onthefly creation failed, display the registration form to fill/fix attributes |
|
188 | 189 | @user = user |
|
189 | 190 | session[:auth_source_registration] = {:login => user.login, :auth_source_id => user.auth_source_id } |
|
190 | 191 | render :action => 'register' |
|
191 | 192 | else |
|
192 | 193 | # Valid user |
|
193 | 194 | successful_authentication(user) |
|
194 | 195 | end |
|
195 | 196 | end |
|
196 | 197 | |
|
197 | 198 | |
|
198 | 199 | def open_id_authenticate(openid_url) |
|
199 | 200 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration| |
|
200 | 201 | if result.successful? |
|
201 | 202 | user = User.find_or_initialize_by_identity_url(identity_url) |
|
202 | 203 | if user.new_record? |
|
203 | 204 | # Create on the fly |
|
204 | 205 | user.login = registration['nickname'] unless registration['nickname'].nil? |
|
205 | 206 | user.mail = registration['email'] unless registration['email'].nil? |
|
206 | 207 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
|
207 | 208 | user.random_password |
|
209 | user.status = User::STATUS_REGISTERED | |
|
210 | ||
|
211 | # TODO: Duplicated in register action. A good sized refactoring would be good here | |
|
212 | case Setting.self_registration | |
|
213 | when '1' | |
|
214 | # Email activation | |
|
215 | token = Token.new(:user => user, :action => "register") | |
|
216 | if user.save and token.save | |
|
217 | Mailer.deliver_register(token) | |
|
218 | flash[:notice] = l(:notice_account_register_done) | |
|
219 | redirect_to :action => 'login' | |
|
220 | else | |
|
221 | onthefly_creation_failed(user, {:login => user.login, :identity_url => identity_url }) | |
|
222 | end | |
|
223 | when '3' | |
|
224 | # Automatic activation | |
|
225 | user.status = User::STATUS_ACTIVE | |
|
208 | 226 | if user.save |
|
227 | flash[:notice] = l(:notice_account_activated) | |
|
209 | 228 | successful_authentication(user) |
|
210 | 229 | else |
|
211 | # Onthefly creation failed, display the registration form to fill/fix attributes | |
|
212 |
|
|
|
213 | session[:auth_source_registration] = {:login => user.login, :identity_url => identity_url } | |
|
214 | render :action => 'register' | |
|
230 | onthefly_creation_failed(user, {:login => user.login, :identity_url => identity_url }) | |
|
231 | end | |
|
232 | else | |
|
233 | # Manual activation by the administrator | |
|
234 | if user.save | |
|
235 | # Sends an email to the administrators | |
|
236 | Mailer.deliver_account_activation_request(user) | |
|
237 | flash[:notice] = l(:notice_account_pending) | |
|
238 | redirect_to :action => 'login' | |
|
239 | else | |
|
240 | onthefly_creation_failed(user, {:login => user.login, :identity_url => identity_url }) | |
|
241 | end | |
|
215 | 242 | end |
|
216 | 243 | else |
|
217 | 244 | # Existing record |
|
218 | 245 | successful_authentication(user) |
|
219 | 246 | end |
|
220 | 247 | end |
|
221 | 248 | end |
|
222 | 249 | end |
|
223 | 250 | |
|
224 | 251 | def successful_authentication(user) |
|
225 | 252 | # Valid user |
|
226 | 253 | self.logged_user = user |
|
227 | 254 | # generate a key and set cookie if autologin |
|
228 | 255 | if params[:autologin] && Setting.autologin? |
|
229 | 256 | token = Token.create(:user => user, :action => 'autologin') |
|
230 | 257 | cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now } |
|
231 | 258 | end |
|
232 | 259 | redirect_back_or_default :controller => 'my', :action => 'page' |
|
233 | 260 | end |
|
234 | 261 | |
|
262 | # Onthefly creation failed, display the registration form to fill/fix attributes | |
|
263 | def onthefly_creation_failed(user, auth_source_options = { }) | |
|
264 | @user = user | |
|
265 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? | |
|
266 | render :action => 'register' | |
|
267 | end | |
|
268 | ||
|
235 | 269 | end |
@@ -1,110 +1,133 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.dirname(__FILE__) + '/../test_helper' |
|
19 | 19 | require 'account_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class AccountController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class AccountControllerTest < Test::Unit::TestCase |
|
25 | 25 | fixtures :users, :roles |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | @controller = AccountController.new |
|
29 | 29 | @request = ActionController::TestRequest.new |
|
30 | 30 | @response = ActionController::TestResponse.new |
|
31 | 31 | User.current = nil |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_show |
|
35 | 35 | get :show, :id => 2 |
|
36 | 36 | assert_response :success |
|
37 | 37 | assert_template 'show' |
|
38 | 38 | assert_not_nil assigns(:user) |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def test_show_inactive |
|
42 | 42 | get :show, :id => 5 |
|
43 | 43 | assert_response 404 |
|
44 | 44 | assert_nil assigns(:user) |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def test_login_should_redirect_to_back_url_param |
|
48 | 48 | # request.uri is "test.host" in test environment |
|
49 | 49 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1' |
|
50 | 50 | assert_redirected_to '/issues/show/1' |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def test_login_should_not_redirect_to_another_host |
|
54 | 54 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' |
|
55 | 55 | assert_redirected_to '/my/page' |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def test_login_with_wrong_password |
|
59 | 59 | post :login, :username => 'admin', :password => 'bad' |
|
60 | 60 | assert_response :success |
|
61 | 61 | assert_template 'login' |
|
62 | 62 | assert_tag 'div', |
|
63 | 63 | :attributes => { :class => "flash error" }, |
|
64 | 64 | :content => /Invalid user or password/ |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def test_login_with_openid |
|
68 | Setting.self_registration = '3' | |
|
68 | 69 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
69 | 70 | assert_redirected_to 'my/page' |
|
70 | 71 | end |
|
71 | 72 | |
|
72 | 73 | def test_login_with_openid_with_new_user_created |
|
74 | Setting.self_registration = '3' | |
|
73 | 75 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
74 | 76 | assert_redirected_to 'my/page' |
|
75 | 77 | user = User.find_by_login('cool_user') |
|
76 | 78 | assert user |
|
77 | 79 | assert_equal 'Cool', user.firstname |
|
78 | 80 | assert_equal 'User', user.lastname |
|
79 | 81 | end |
|
80 | 82 | |
|
83 | def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token | |
|
84 | Setting.self_registration = '1' | |
|
85 | post :login, :openid_url => 'http://openid.example.com/good_user' | |
|
86 | assert_redirected_to 'login' | |
|
87 | user = User.find_by_login('cool_user') | |
|
88 | assert user | |
|
89 | ||
|
90 | token = Token.find_by_user_id_and_action(user.id, 'register') | |
|
91 | assert token | |
|
92 | end | |
|
93 | ||
|
94 | def test_login_with_openid_with_new_user_created_with_manual_activation | |
|
95 | Setting.self_registration = '2' | |
|
96 | post :login, :openid_url => 'http://openid.example.com/good_user' | |
|
97 | assert_redirected_to 'login' | |
|
98 | user = User.find_by_login('cool_user') | |
|
99 | assert user | |
|
100 | assert_equal User::STATUS_REGISTERED, user.status | |
|
101 | end | |
|
102 | ||
|
81 | 103 | def test_login_with_openid_with_new_user_with_conflict_should_register |
|
104 | Setting.self_registration = '3' | |
|
82 | 105 | existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com') |
|
83 | 106 | existing_user.login = 'cool_user' |
|
84 | 107 | assert existing_user.save! |
|
85 | 108 | |
|
86 | 109 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
87 | 110 | assert_response :success |
|
88 | 111 | assert_template 'register' |
|
89 | 112 | assert assigns(:user) |
|
90 | 113 | assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url] |
|
91 | 114 | end |
|
92 | 115 | |
|
93 | 116 | def test_autologin |
|
94 | 117 | Setting.autologin = "7" |
|
95 | 118 | Token.delete_all |
|
96 | 119 | post :login, :username => 'admin', :password => 'admin', :autologin => 1 |
|
97 | 120 | assert_redirected_to 'my/page' |
|
98 | 121 | token = Token.find :first |
|
99 | 122 | assert_not_nil token |
|
100 | 123 | assert_equal User.find_by_login('admin'), token.user |
|
101 | 124 | assert_equal 'autologin', token.action |
|
102 | 125 | end |
|
103 | 126 | |
|
104 | 127 | def test_logout |
|
105 | 128 | @request.session[:user_id] = 2 |
|
106 | 129 | get :logout |
|
107 | 130 | assert_redirected_to '' |
|
108 | 131 | assert_nil @request.session[:user_id] |
|
109 | 132 | end |
|
110 | 133 | end |
General Comments 0
You need to be logged in to leave comments.
Login now