##// END OF EJS Templates
Log the user in after registration if account activation is not needed....
Jean-Philippe Lang -
r1507:19cb6f96f4bf
parent child
Show More
@@ -1,177 +1,178
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 class AccountController < ApplicationController
19 19 layout 'base'
20 20 helper :custom_fields
21 21 include CustomFieldsHelper
22 22
23 23 # prevents login action to be filtered by check_if_login_required application scope filter
24 24 skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate]
25 25
26 26 # Show user's account
27 27 def show
28 28 @user = User.find_active(params[:id])
29 29 @custom_values = @user.custom_values.find(:all, :include => :custom_field)
30 30
31 31 # show only public projects and private projects that the logged in user is also a member of
32 32 @memberships = @user.memberships.select do |membership|
33 33 membership.project.is_public? || (User.current.member_of?(membership.project))
34 34 end
35 35 rescue ActiveRecord::RecordNotFound
36 36 render_404
37 37 end
38 38
39 39 # Login request and validation
40 40 def login
41 41 if request.get?
42 42 # Logout user
43 43 self.logged_user = nil
44 44 else
45 45 # Authenticate user
46 46 user = User.try_to_login(params[:username], params[:password])
47 47 if user
48 48 self.logged_user = user
49 49 # generate a key and set cookie if autologin
50 50 if params[:autologin] && Setting.autologin?
51 51 token = Token.create(:user => user, :action => 'autologin')
52 52 cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
53 53 end
54 54 redirect_back_or_default :controller => 'my', :action => 'page'
55 55 else
56 56 flash.now[:error] = l(:notice_account_invalid_creditentials)
57 57 end
58 58 end
59 59 rescue User::OnTheFlyCreationFailure
60 60 flash.now[:error] = 'Redmine could not retrieve the required information from the LDAP to create your account. Please, contact your Redmine administrator.'
61 61 end
62 62
63 63 # Log out current user and redirect to welcome page
64 64 def logout
65 65 cookies.delete :autologin
66 66 Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged?
67 67 self.logged_user = nil
68 68 redirect_to home_url
69 69 end
70 70
71 71 # Enable user to choose a new password
72 72 def lost_password
73 73 redirect_to(home_url) && return unless Setting.lost_password?
74 74 if params[:token]
75 75 @token = Token.find_by_action_and_value("recovery", params[:token])
76 76 redirect_to(home_url) && return unless @token and !@token.expired?
77 77 @user = @token.user
78 78 if request.post?
79 79 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
80 80 if @user.save
81 81 @token.destroy
82 82 flash[:notice] = l(:notice_account_password_updated)
83 83 redirect_to :action => 'login'
84 84 return
85 85 end
86 86 end
87 87 render :template => "account/password_recovery"
88 88 return
89 89 else
90 90 if request.post?
91 91 user = User.find_by_mail(params[:mail])
92 92 # user not found in db
93 93 flash.now[:error] = l(:notice_account_unknown_email) and return unless user
94 94 # user uses an external authentification
95 95 flash.now[:error] = l(:notice_can_t_change_password) and return if user.auth_source_id
96 96 # create a new token for password recovery
97 97 token = Token.new(:user => user, :action => "recovery")
98 98 if token.save
99 99 Mailer.deliver_lost_password(token)
100 100 flash[:notice] = l(:notice_account_lost_email_sent)
101 101 redirect_to :action => 'login'
102 102 return
103 103 end
104 104 end
105 105 end
106 106 end
107 107
108 108 # User self-registration
109 109 def register
110 110 redirect_to(home_url) && return unless Setting.self_registration?
111 111 if request.get?
112 112 @user = User.new(:language => Setting.default_language)
113 113 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) }
114 114 else
115 115 @user = User.new(params[:user])
116 116 @user.admin = false
117 117 @user.login = params[:user][:login]
118 118 @user.status = User::STATUS_REGISTERED
119 119 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
120 120 @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x,
121 121 :customized => @user,
122 122 :value => (params["custom_fields"] ? params["custom_fields"][x.id.to_s] : nil)) }
123 123 @user.custom_values = @custom_values
124 124 case Setting.self_registration
125 125 when '1'
126 126 # Email activation
127 127 token = Token.new(:user => @user, :action => "register")
128 128 if @user.save and token.save
129 129 Mailer.deliver_register(token)
130 130 flash[:notice] = l(:notice_account_register_done)
131 131 redirect_to :action => 'login'
132 132 end
133 133 when '3'
134 134 # Automatic activation
135 135 @user.status = User::STATUS_ACTIVE
136 136 if @user.save
137 self.logged_user = @user
137 138 flash[:notice] = l(:notice_account_activated)
138 redirect_to :action => 'login'
139 redirect_to :controller => 'my', :action => 'account'
139 140 end
140 141 else
141 142 # Manual activation by the administrator
142 143 if @user.save
143 144 # Sends an email to the administrators
144 145 Mailer.deliver_account_activation_request(@user)
145 146 flash[:notice] = l(:notice_account_pending)
146 147 redirect_to :action => 'login'
147 148 end
148 149 end
149 150 end
150 151 end
151 152
152 153 # Token based account activation
153 154 def activate
154 155 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
155 156 token = Token.find_by_action_and_value('register', params[:token])
156 157 redirect_to(home_url) && return unless token and !token.expired?
157 158 user = token.user
158 159 redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED
159 160 user.status = User::STATUS_ACTIVE
160 161 if user.save
161 162 token.destroy
162 163 flash[:notice] = l(:notice_account_activated)
163 164 end
164 165 redirect_to :action => 'login'
165 166 end
166 167
167 168 private
168 169 def logged_user=(user)
169 170 if user && user.is_a?(User)
170 171 User.current = user
171 172 session[:user_id] = user.id
172 173 else
173 174 User.current = User.anonymous
174 175 session[:user_id] = nil
175 176 end
176 177 end
177 178 end
@@ -1,101 +1,105
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
20 20 class AccountTest < ActionController::IntegrationTest
21 21 fixtures :users
22 22
23 23 # Replace this with your real tests.
24 24 def test_login
25 25 get "my/page"
26 26 assert_redirected_to "account/login"
27 27 log_user('jsmith', 'jsmith')
28 28
29 29 get "my/account"
30 30 assert_response :success
31 31 assert_template "my/account"
32 32 end
33 33
34 34 def test_lost_password
35 35 Token.delete_all
36 36
37 37 get "account/lost_password"
38 38 assert_response :success
39 39 assert_template "account/lost_password"
40 40
41 41 post "account/lost_password", :mail => 'jsmith@somenet.foo'
42 42 assert_redirected_to "account/login"
43 43
44 44 token = Token.find(:first)
45 45 assert_equal 'recovery', token.action
46 46 assert_equal 'jsmith@somenet.foo', token.user.mail
47 47 assert !token.expired?
48 48
49 49 get "account/lost_password", :token => token.value
50 50 assert_response :success
51 51 assert_template "account/password_recovery"
52 52
53 53 post "account/lost_password", :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
54 54 assert_redirected_to "account/login"
55 55 assert_equal 'Password was successfully updated.', flash[:notice]
56 56
57 57 log_user('jsmith', 'newpass')
58 58 assert_equal 0, Token.count
59 59 end
60 60
61 61 def test_register_with_automatic_activation
62 62 Setting.self_registration = '3'
63 63
64 64 get 'account/register'
65 65 assert_response :success
66 66 assert_template 'account/register'
67 67
68 68 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar"},
69 69 :password => "newpass", :password_confirmation => "newpass"
70 assert_redirected_to 'account/login'
71 log_user('newuser', 'newpass')
70 assert_redirected_to 'my/account'
71 follow_redirect!
72 assert_response :success
73 assert_template 'my/account'
74
75 assert User.find_by_login('newuser').active?
72 76 end
73 77
74 78 def test_register_with_manual_activation
75 79 Setting.self_registration = '2'
76 80
77 81 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar"},
78 82 :password => "newpass", :password_confirmation => "newpass"
79 83 assert_redirected_to 'account/login'
80 84 assert !User.find_by_login('newuser').active?
81 85 end
82 86
83 87 def test_register_with_email_activation
84 88 Setting.self_registration = '1'
85 89 Token.delete_all
86 90
87 91 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar"},
88 92 :password => "newpass", :password_confirmation => "newpass"
89 93 assert_redirected_to 'account/login'
90 94 assert !User.find_by_login('newuser').active?
91 95
92 96 token = Token.find(:first)
93 97 assert_equal 'register', token.action
94 98 assert_equal 'newuser@foo.bar', token.user.mail
95 99 assert !token.expired?
96 100
97 101 get 'account/activate', :token => token.value
98 102 assert_redirected_to 'account/login'
99 103 log_user('newuser', 'newpass')
100 104 end
101 105 end
General Comments 0
You need to be logged in to leave comments. Login now