##// END OF EJS Templates
Fixed the bug in the OpenID registration where the form wouldn't take a login...
Eric Davis -
r2421:24ee6b9a1b16
parent child
Show More
@@ -1,286 +1,286
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 if Setting.openid? && using_open_id?
50 50 open_id_authenticate(params[:openid_url])
51 51 else
52 52 password_authentication
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 125
126 126 case Setting.self_registration
127 127 when '1'
128 128 register_by_email_activation(@user)
129 129 when '3'
130 130 register_automatically(@user)
131 131 else
132 132 register_manually_by_administrator(@user)
133 133 end
134 134 end
135 135 end
136 136 end
137 137
138 138 # Token based account activation
139 139 def activate
140 140 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
141 141 token = Token.find_by_action_and_value('register', params[:token])
142 142 redirect_to(home_url) && return unless token and !token.expired?
143 143 user = token.user
144 144 redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED
145 145 user.status = User::STATUS_ACTIVE
146 146 if user.save
147 147 token.destroy
148 148 flash[:notice] = l(:notice_account_activated)
149 149 end
150 150 redirect_to :action => 'login'
151 151 end
152 152
153 153 private
154 154 def logged_user=(user)
155 155 if user && user.is_a?(User)
156 156 User.current = user
157 157 session[:user_id] = user.id
158 158 else
159 159 User.current = User.anonymous
160 160 session[:user_id] = nil
161 161 end
162 162 end
163 163
164 164 def password_authentication
165 165 user = User.try_to_login(params[:username], params[:password])
166 166 if user.nil?
167 167 # Invalid credentials
168 168 flash.now[:error] = l(:notice_account_invalid_creditentials)
169 169 elsif user.new_record?
170 170 # Onthefly creation failed, display the registration form to fill/fix attributes
171 171 @user = user
172 172 session[:auth_source_registration] = {:login => user.login, :auth_source_id => user.auth_source_id }
173 173 render :action => 'register'
174 174 else
175 175 # Valid user
176 176 successful_authentication(user)
177 177 end
178 178 end
179 179
180 180
181 181 def open_id_authenticate(openid_url)
182 182 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration|
183 183 if result.successful?
184 184 user = User.find_or_initialize_by_identity_url(identity_url)
185 185 if user.new_record?
186 186 # Self-registration off
187 187 redirect_to(home_url) && return unless Setting.self_registration?
188 188
189 189 # Create on the fly
190 190 user.login = registration['nickname'] unless registration['nickname'].nil?
191 191 user.mail = registration['email'] unless registration['email'].nil?
192 192 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
193 193 user.random_password
194 194 user.status = User::STATUS_REGISTERED
195 195
196 196 case Setting.self_registration
197 197 when '1'
198 198 register_by_email_activation(user) do
199 onthefly_creation_failed(user, {:login => user.login, :identity_url => identity_url })
199 onthefly_creation_failed(user)
200 200 end
201 201 when '3'
202 202 register_automatically(user) do
203 onthefly_creation_failed(user, {:login => user.login, :identity_url => identity_url })
203 onthefly_creation_failed(user)
204 204 end
205 205 else
206 206 register_manually_by_administrator(user) do
207 onthefly_creation_failed(user, {:login => user.login, :identity_url => identity_url })
207 onthefly_creation_failed(user)
208 208 end
209 209 end
210 210 else
211 211 # Existing record
212 212 if user.active?
213 213 successful_authentication(user)
214 214 else
215 215 account_pending
216 216 end
217 217 end
218 218 end
219 219 end
220 220 end
221 221
222 222 def successful_authentication(user)
223 223 # Valid user
224 224 self.logged_user = user
225 225 # generate a key and set cookie if autologin
226 226 if params[:autologin] && Setting.autologin?
227 227 token = Token.create(:user => user, :action => 'autologin')
228 228 cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
229 229 end
230 230 redirect_back_or_default :controller => 'my', :action => 'page'
231 231 end
232 232
233 233 # Onthefly creation failed, display the registration form to fill/fix attributes
234 234 def onthefly_creation_failed(user, auth_source_options = { })
235 235 @user = user
236 236 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
237 237 render :action => 'register'
238 238 end
239 239
240 240 # Register a user for email activation.
241 241 #
242 242 # Pass a block for behavior when a user fails to save
243 243 def register_by_email_activation(user, &block)
244 244 token = Token.new(:user => user, :action => "register")
245 245 if user.save and token.save
246 246 Mailer.deliver_register(token)
247 247 flash[:notice] = l(:notice_account_register_done)
248 248 redirect_to :action => 'login'
249 249 else
250 250 yield if block_given?
251 251 end
252 252 end
253 253
254 254 # Automatically register a user
255 255 #
256 256 # Pass a block for behavior when a user fails to save
257 257 def register_automatically(user, &block)
258 258 # Automatic activation
259 259 user.status = User::STATUS_ACTIVE
260 260 if user.save
261 261 self.logged_user = user
262 262 flash[:notice] = l(:notice_account_activated)
263 263 redirect_to :controller => 'my', :action => 'account'
264 264 else
265 265 yield if block_given?
266 266 end
267 267 end
268 268
269 269 # Manual activation by the administrator
270 270 #
271 271 # Pass a block for behavior when a user fails to save
272 272 def register_manually_by_administrator(user, &block)
273 273 if user.save
274 274 # Sends an email to the administrators
275 275 Mailer.deliver_account_activation_request(user)
276 276 account_pending
277 277 else
278 278 yield if block_given?
279 279 end
280 280 end
281 281
282 282 def account_pending
283 283 flash[:notice] = l(:notice_account_pending)
284 284 redirect_to :action => 'login'
285 285 end
286 286 end
General Comments 0
You need to be logged in to leave comments. Login now