##// END OF EJS Templates
Merged r2986 from trunk....
Jean-Philippe Lang -
r2873:d09afa64c805
parent child
Show More
@@ -1,195 +1,199
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 if @user != User.current && !User.current.admin? && @memberships.empty? && events.empty?
39 render_404 and return
40 end
41
38 42 rescue ActiveRecord::RecordNotFound
39 43 render_404
40 44 end
41 45
42 46 # Login request and validation
43 47 def login
44 48 if request.get?
45 49 # Logout user
46 50 self.logged_user = nil
47 51 else
48 52 # Authenticate user
49 53 user = User.try_to_login(params[:username], params[:password])
50 54 if user.nil?
51 55 # Invalid credentials
52 56 flash.now[:error] = l(:notice_account_invalid_creditentials)
53 57 elsif user.new_record?
54 58 # Onthefly creation failed, display the registration form to fill/fix attributes
55 59 @user = user
56 60 session[:auth_source_registration] = {:login => user.login, :auth_source_id => user.auth_source_id }
57 61 render :action => 'register'
58 62 else
59 63 # Valid user
60 64 self.logged_user = user
61 65 # generate a key and set cookie if autologin
62 66 if params[:autologin] && Setting.autologin?
63 67 token = Token.create(:user => user, :action => 'autologin')
64 68 cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
65 69 end
66 70 call_hook(:controller_account_success_authentication_after, {:user => user })
67 71 redirect_back_or_default :controller => 'my', :action => 'page'
68 72 end
69 73 end
70 74 end
71 75
72 76 # Log out current user and redirect to welcome page
73 77 def logout
74 78 cookies.delete :autologin
75 79 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged?
76 80 self.logged_user = nil
77 81 redirect_to home_url
78 82 end
79 83
80 84 # Enable user to choose a new password
81 85 def lost_password
82 86 redirect_to(home_url) && return unless Setting.lost_password?
83 87 if params[:token]
84 88 @token = Token.find_by_action_and_value("recovery", params[:token])
85 89 redirect_to(home_url) && return unless @token and !@token.expired?
86 90 @user = @token.user
87 91 if request.post?
88 92 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
89 93 if @user.save
90 94 @token.destroy
91 95 flash[:notice] = l(:notice_account_password_updated)
92 96 redirect_to :action => 'login'
93 97 return
94 98 end
95 99 end
96 100 render :template => "account/password_recovery"
97 101 return
98 102 else
99 103 if request.post?
100 104 user = User.find_by_mail(params[:mail])
101 105 # user not found in db
102 106 flash.now[:error] = l(:notice_account_unknown_email) and return unless user
103 107 # user uses an external authentification
104 108 flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id
105 109 # create a new token for password recovery
106 110 token = Token.new(:user => user, :action => "recovery")
107 111 if token.save
108 112 Mailer.deliver_lost_password(token)
109 113 flash[:notice] = l(:notice_account_lost_email_sent)
110 114 redirect_to :action => 'login'
111 115 return
112 116 end
113 117 end
114 118 end
115 119 end
116 120
117 121 # User self-registration
118 122 def register
119 123 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
120 124 if request.get?
121 125 session[:auth_source_registration] = nil
122 126 @user = User.new(:language => Setting.default_language)
123 127 else
124 128 @user = User.new(params[:user])
125 129 @user.admin = false
126 130 @user.status = User::STATUS_REGISTERED
127 131 if session[:auth_source_registration]
128 132 @user.status = User::STATUS_ACTIVE
129 133 @user.login = session[:auth_source_registration][:login]
130 134 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
131 135 if @user.save
132 136 session[:auth_source_registration] = nil
133 137 self.logged_user = @user
134 138 flash[:notice] = l(:notice_account_activated)
135 139 redirect_to :controller => 'my', :action => 'account'
136 140 end
137 141 else
138 142 @user.login = params[:user][:login]
139 143 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
140 144 case Setting.self_registration
141 145 when '1'
142 146 # Email activation
143 147 token = Token.new(:user => @user, :action => "register")
144 148 if @user.save and token.save
145 149 Mailer.deliver_register(token)
146 150 flash[:notice] = l(:notice_account_register_done)
147 151 redirect_to :action => 'login'
148 152 end
149 153 when '3'
150 154 # Automatic activation
151 155 @user.status = User::STATUS_ACTIVE
152 156 if @user.save
153 157 self.logged_user = @user
154 158 flash[:notice] = l(:notice_account_activated)
155 159 redirect_to :controller => 'my', :action => 'account'
156 160 end
157 161 else
158 162 # Manual activation by the administrator
159 163 if @user.save
160 164 # Sends an email to the administrators
161 165 Mailer.deliver_account_activation_request(@user)
162 166 flash[:notice] = l(:notice_account_pending)
163 167 redirect_to :action => 'login'
164 168 end
165 169 end
166 170 end
167 171 end
168 172 end
169 173
170 174 # Token based account activation
171 175 def activate
172 176 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
173 177 token = Token.find_by_action_and_value('register', params[:token])
174 178 redirect_to(home_url) && return unless token and !token.expired?
175 179 user = token.user
176 180 redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED
177 181 user.status = User::STATUS_ACTIVE
178 182 if user.save
179 183 token.destroy
180 184 flash[:notice] = l(:notice_account_activated)
181 185 end
182 186 redirect_to :action => 'login'
183 187 end
184 188
185 189 private
186 190 def logged_user=(user)
187 191 if user && user.is_a?(User)
188 192 User.current = user
189 193 session[:user_id] = user.id
190 194 else
191 195 User.current = User.anonymous
192 196 session[:user_id] = nil
193 197 end
194 198 end
195 199 end
@@ -1,100 +1,148
1 1 ---
2 2 users_004:
3 3 created_on: 2006-07-19 19:34:07 +02:00
4 4 status: 1
5 5 last_login_on:
6 6 language: en
7 7 hashed_password: 4e4aeb7baaf0706bd670263fef42dad15763b608
8 8 updated_on: 2006-07-19 19:34:07 +02:00
9 9 admin: false
10 10 mail: rhill@somenet.foo
11 11 lastname: Hill
12 12 firstname: Robert
13 13 id: 4
14 14 auth_source_id:
15 15 mail_notification: true
16 16 login: rhill
17 17 type: User
18 18 users_001:
19 19 created_on: 2006-07-19 19:12:21 +02:00
20 20 status: 1
21 21 last_login_on: 2006-07-19 22:57:52 +02:00
22 22 language: en
23 23 hashed_password: d033e22ae348aeb5660fc2140aec35850c4da997
24 24 updated_on: 2006-07-19 22:57:52 +02:00
25 25 admin: true
26 26 mail: admin@somenet.foo
27 27 lastname: Admin
28 28 firstname: redMine
29 29 id: 1
30 30 auth_source_id:
31 31 mail_notification: true
32 32 login: admin
33 33 type: User
34 34 users_002:
35 35 created_on: 2006-07-19 19:32:09 +02:00
36 36 status: 1
37 37 last_login_on: 2006-07-19 22:42:15 +02:00
38 38 language: en
39 39 hashed_password: a9a653d4151fa2c081ba1ffc2c2726f3b80b7d7d
40 40 updated_on: 2006-07-19 22:42:15 +02:00
41 41 admin: false
42 42 mail: jsmith@somenet.foo
43 43 lastname: Smith
44 44 firstname: John
45 45 id: 2
46 46 auth_source_id:
47 47 mail_notification: true
48 48 login: jsmith
49 49 type: User
50 50 users_003:
51 51 created_on: 2006-07-19 19:33:19 +02:00
52 52 status: 1
53 53 last_login_on:
54 54 language: en
55 55 hashed_password: 7feb7657aa7a7bf5aef3414a5084875f27192415
56 56 updated_on: 2006-07-19 19:33:19 +02:00
57 57 admin: false
58 58 mail: dlopper@somenet.foo
59 59 lastname: Lopper
60 60 firstname: Dave
61 61 id: 3
62 62 auth_source_id:
63 63 mail_notification: true
64 64 login: dlopper
65 65 type: User
66 66 users_005:
67 67 id: 5
68 68 created_on: 2006-07-19 19:33:19 +02:00
69 69 # Locked
70 70 status: 3
71 71 last_login_on:
72 72 language: en
73 73 hashed_password: 7feb7657aa7a7bf5aef3414a5084875f27192415
74 74 updated_on: 2006-07-19 19:33:19 +02:00
75 75 admin: false
76 76 mail: dlopper2@somenet.foo
77 77 lastname: Lopper2
78 78 firstname: Dave2
79 79 auth_source_id:
80 80 mail_notification: true
81 81 login: dlopper2
82 82 type: User
83 83 users_006:
84 84 id: 6
85 85 created_on: 2006-07-19 19:33:19 +02:00
86 86 status: 1
87 87 last_login_on:
88 88 language: ''
89 89 hashed_password: 1
90 90 updated_on: 2006-07-19 19:33:19 +02:00
91 91 admin: false
92 92 mail: ''
93 93 lastname: Anonymous
94 94 firstname: ''
95 95 auth_source_id:
96 96 mail_notification: false
97 97 login: ''
98 98 type: AnonymousUser
99 users_007:
100 id: 7
101 created_on: 2006-07-19 19:33:19 +02:00
102 status: 1
103 last_login_on:
104 language: ''
105 hashed_password: 1
106 updated_on: 2006-07-19 19:33:19 +02:00
107 admin: false
108 mail: someone@foo.bar
109 lastname: One
110 firstname: Some
111 auth_source_id:
112 mail_notification: false
113 login: someone
114 type: User
115 users_008:
116 id: 8
117 created_on: 2006-07-19 19:33:19 +02:00
118 status: 1
119 last_login_on:
120 language: 'it'
121 hashed_password: 1
122 updated_on: 2006-07-19 19:33:19 +02:00
123 admin: false
124 mail: miscuser8@foo.bar
125 lastname: Misc
126 firstname: User
127 auth_source_id:
128 mail_notification: false
129 login: miscuser8
130 type: User
131 users_009:
132 id: 9
133 created_on: 2006-07-19 19:33:19 +02:00
134 status: 1
135 last_login_on:
136 language: 'it'
137 hashed_password: 1
138 updated_on: 2006-07-19 19:33:19 +02:00
139 admin: false
140 mail: miscuser9@foo.bar
141 lastname: Misc
142 firstname: User
143 auth_source_id:
144 mail_notification: false
145 login: miscuser9
146 type: User
99 147
100 148 No newline at end of file
@@ -1,84 +1,89
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
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 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
48 get :show, :id => 9
49 assert_response 404
50 end
51
47 52 def test_login_should_redirect_to_back_url_param
48 53 # request.uri is "test.host" in test environment
49 54 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1'
50 55 assert_redirected_to '/issues/show/1'
51 56 end
52 57
53 58 def test_login_should_not_redirect_to_another_host
54 59 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake'
55 60 assert_redirected_to '/my/page'
56 61 end
57 62
58 63 def test_login_with_wrong_password
59 64 post :login, :username => 'admin', :password => 'bad'
60 65 assert_response :success
61 66 assert_template 'login'
62 67 assert_tag 'div',
63 68 :attributes => { :class => "flash error" },
64 69 :content => /Invalid user or password/
65 70 end
66 71
67 72 def test_autologin
68 73 Setting.autologin = "7"
69 74 Token.delete_all
70 75 post :login, :username => 'admin', :password => 'admin', :autologin => 1
71 76 assert_redirected_to 'my/page'
72 77 token = Token.find :first
73 78 assert_not_nil token
74 79 assert_equal User.find_by_login('admin'), token.user
75 80 assert_equal 'autologin', token.action
76 81 end
77 82
78 83 def test_logout
79 84 @request.session[:user_id] = 2
80 85 get :logout
81 86 assert_redirected_to ''
82 87 assert_nil @request.session[:user_id]
83 88 end
84 89 end
General Comments 0
You need to be logged in to leave comments. Login now