annotate app/controllers/account_controller.rb @ 1296:038ba2d95de8 redmine-2.2

Fix redmine-2.2 branch update (add missing svn files)
author Chris Cannam
date Fri, 14 Jun 2013 09:05:06 +0100
parents 433d4f72a19b
children bb32da3bea34 622f24f53b42 261b3d9a4903
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class AccountController < ApplicationController
Chris@0 19 helper :custom_fields
Chris@909 20 include CustomFieldsHelper
Chris@909 21
Chris@0 22 # prevents login action to be filtered by check_if_login_required application scope filter
Chris@0 23 skip_before_filter :check_if_login_required
Chris@0 24
Chris@0 25 # Login request and validation
Chris@0 26 def login
Chris@0 27 if request.get?
Chris@0 28 logout_user
Chris@0 29 else
Chris@0 30 authenticate_user
Chris@0 31 end
Chris@1115 32 rescue AuthSourceException => e
Chris@1115 33 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
Chris@1115 34 render_error :message => e.message
Chris@0 35 end
Chris@0 36
Chris@0 37 # Log out current user and redirect to welcome page
Chris@0 38 def logout
Chris@0 39 logout_user
Chris@0 40 redirect_to home_url
Chris@0 41 end
Chris@909 42
Chris@1115 43 # Lets user choose a new password
Chris@0 44 def lost_password
Chris@0 45 redirect_to(home_url) && return unless Setting.lost_password?
Chris@0 46 if params[:token]
Chris@1115 47 @token = Token.find_by_action_and_value("recovery", params[:token].to_s)
Chris@1115 48 if @token.nil? || @token.expired?
Chris@1115 49 redirect_to home_url
Chris@1115 50 return
Chris@1115 51 end
Chris@0 52 @user = @token.user
Chris@1115 53 unless @user && @user.active?
Chris@1115 54 redirect_to home_url
Chris@1115 55 return
Chris@1115 56 end
Chris@0 57 if request.post?
Chris@0 58 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
Chris@0 59 if @user.save
Chris@0 60 @token.destroy
Chris@0 61 flash[:notice] = l(:notice_account_password_updated)
Chris@1115 62 redirect_to signin_path
Chris@0 63 return
Chris@909 64 end
Chris@0 65 end
Chris@0 66 render :template => "account/password_recovery"
Chris@0 67 return
Chris@0 68 else
Chris@0 69 if request.post?
Chris@1115 70 user = User.find_by_mail(params[:mail].to_s)
Chris@1115 71 # user not found or not active
Chris@1115 72 unless user && user.active?
Chris@1115 73 flash.now[:error] = l(:notice_account_unknown_email)
Chris@1115 74 return
Chris@1115 75 end
Chris@1115 76 # user cannot change its password
Chris@1115 77 unless user.change_password_allowed?
Chris@1115 78 flash.now[:error] = l(:notice_can_t_change_password)
Chris@1115 79 return
Chris@1115 80 end
Chris@0 81 # create a new token for password recovery
Chris@0 82 token = Token.new(:user => user, :action => "recovery")
Chris@0 83 if token.save
Chris@1115 84 Mailer.lost_password(token).deliver
Chris@0 85 flash[:notice] = l(:notice_account_lost_email_sent)
Chris@1115 86 redirect_to signin_path
Chris@0 87 return
Chris@0 88 end
Chris@0 89 end
Chris@0 90 end
Chris@0 91 end
Chris@909 92
Chris@0 93 # User self-registration
Chris@0 94 def register
Chris@0 95 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
Chris@0 96 if request.get?
Chris@0 97 session[:auth_source_registration] = nil
Chris@0 98 @user = User.new(:language => Setting.default_language)
Chris@0 99 else
Chris@1115 100 user_params = params[:user] || {}
Chris@1115 101 @user = User.new
Chris@1115 102 @user.safe_attributes = user_params
Chris@0 103 @user.admin = false
Chris@14 104 @user.register
Chris@0 105 if session[:auth_source_registration]
Chris@14 106 @user.activate
Chris@0 107 @user.login = session[:auth_source_registration][:login]
Chris@0 108 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
Chris@0 109 if @user.save
Chris@0 110 session[:auth_source_registration] = nil
Chris@0 111 self.logged_user = @user
Chris@0 112 flash[:notice] = l(:notice_account_activated)
Chris@0 113 redirect_to :controller => 'my', :action => 'account'
Chris@0 114 end
Chris@0 115 else
Chris@0 116 @user.login = params[:user][:login]
Chris@1115 117 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
Chris@1115 118 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
Chris@1115 119 end
Chris@0 120
Chris@0 121 case Setting.self_registration
Chris@0 122 when '1'
Chris@0 123 register_by_email_activation(@user)
Chris@0 124 when '3'
Chris@0 125 register_automatically(@user)
Chris@0 126 else
Chris@0 127 register_manually_by_administrator(@user)
Chris@0 128 end
Chris@0 129 end
Chris@0 130 end
Chris@0 131 end
Chris@909 132
Chris@0 133 # Token based account activation
Chris@0 134 def activate
Chris@0 135 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
Chris@0 136 token = Token.find_by_action_and_value('register', params[:token])
Chris@0 137 redirect_to(home_url) && return unless token and !token.expired?
Chris@0 138 user = token.user
Chris@14 139 redirect_to(home_url) && return unless user.registered?
Chris@14 140 user.activate
Chris@0 141 if user.save
Chris@0 142 token.destroy
Chris@0 143 flash[:notice] = l(:notice_account_activated)
Chris@0 144 end
Chris@1115 145 redirect_to signin_path
Chris@0 146 end
Chris@909 147
Chris@0 148 private
Chris@909 149
Chris@0 150 def authenticate_user
Chris@0 151 if Setting.openid? && using_open_id?
Chris@0 152 open_id_authenticate(params[:openid_url])
Chris@0 153 else
Chris@0 154 password_authentication
Chris@0 155 end
Chris@0 156 end
Chris@0 157
Chris@0 158 def password_authentication
Chris@0 159 user = User.try_to_login(params[:username], params[:password])
Chris@0 160
Chris@0 161 if user.nil?
Chris@0 162 invalid_credentials
Chris@0 163 elsif user.new_record?
Chris@0 164 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
Chris@0 165 else
Chris@0 166 # Valid user
Chris@0 167 successful_authentication(user)
Chris@0 168 end
Chris@0 169 end
Chris@0 170
Chris@0 171 def open_id_authenticate(openid_url)
Chris@1115 172 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
Chris@0 173 if result.successful?
Chris@0 174 user = User.find_or_initialize_by_identity_url(identity_url)
Chris@0 175 if user.new_record?
Chris@0 176 # Self-registration off
Chris@0 177 redirect_to(home_url) && return unless Setting.self_registration?
Chris@0 178
Chris@0 179 # Create on the fly
Chris@0 180 user.login = registration['nickname'] unless registration['nickname'].nil?
Chris@0 181 user.mail = registration['email'] unless registration['email'].nil?
Chris@0 182 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
Chris@0 183 user.random_password
Chris@14 184 user.register
Chris@0 185
Chris@0 186 case Setting.self_registration
Chris@0 187 when '1'
Chris@0 188 register_by_email_activation(user) do
Chris@0 189 onthefly_creation_failed(user)
Chris@0 190 end
Chris@0 191 when '3'
Chris@0 192 register_automatically(user) do
Chris@0 193 onthefly_creation_failed(user)
Chris@0 194 end
Chris@0 195 else
Chris@0 196 register_manually_by_administrator(user) do
Chris@0 197 onthefly_creation_failed(user)
Chris@0 198 end
Chris@909 199 end
Chris@0 200 else
Chris@0 201 # Existing record
Chris@0 202 if user.active?
Chris@0 203 successful_authentication(user)
Chris@0 204 else
Chris@0 205 account_pending
Chris@0 206 end
Chris@0 207 end
Chris@0 208 end
Chris@0 209 end
Chris@0 210 end
Chris@909 211
Chris@0 212 def successful_authentication(user)
Chris@1115 213 logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
Chris@0 214 # Valid user
Chris@0 215 self.logged_user = user
Chris@0 216 # generate a key and set cookie if autologin
Chris@0 217 if params[:autologin] && Setting.autologin?
Chris@210 218 set_autologin_cookie(user)
Chris@0 219 end
Chris@0 220 call_hook(:controller_account_success_authentication_after, {:user => user })
Chris@0 221 redirect_back_or_default :controller => 'my', :action => 'page'
Chris@0 222 end
Chris@909 223
Chris@210 224 def set_autologin_cookie(user)
Chris@210 225 token = Token.create(:user => user, :action => 'autologin')
Chris@210 226 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
Chris@210 227 cookie_options = {
Chris@210 228 :value => token.value,
Chris@210 229 :expires => 1.year.from_now,
Chris@210 230 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
Chris@210 231 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
Chris@210 232 :httponly => true
Chris@210 233 }
Chris@210 234 cookies[cookie_name] = cookie_options
Chris@210 235 end
Chris@0 236
Chris@0 237 # Onthefly creation failed, display the registration form to fill/fix attributes
Chris@0 238 def onthefly_creation_failed(user, auth_source_options = { })
Chris@0 239 @user = user
Chris@0 240 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
Chris@0 241 render :action => 'register'
Chris@0 242 end
Chris@0 243
Chris@0 244 def invalid_credentials
Chris@0 245 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
Chris@0 246 flash.now[:error] = l(:notice_account_invalid_creditentials)
Chris@0 247 end
Chris@0 248
Chris@0 249 # Register a user for email activation.
Chris@0 250 #
Chris@0 251 # Pass a block for behavior when a user fails to save
Chris@0 252 def register_by_email_activation(user, &block)
Chris@0 253 token = Token.new(:user => user, :action => "register")
Chris@0 254 if user.save and token.save
Chris@1115 255 Mailer.register(token).deliver
Chris@0 256 flash[:notice] = l(:notice_account_register_done)
Chris@1115 257 redirect_to signin_path
Chris@0 258 else
Chris@0 259 yield if block_given?
Chris@0 260 end
Chris@0 261 end
Chris@909 262
Chris@0 263 # Automatically register a user
Chris@0 264 #
Chris@0 265 # Pass a block for behavior when a user fails to save
Chris@0 266 def register_automatically(user, &block)
Chris@0 267 # Automatic activation
Chris@14 268 user.activate
Chris@0 269 user.last_login_on = Time.now
Chris@0 270 if user.save
Chris@0 271 self.logged_user = user
Chris@0 272 flash[:notice] = l(:notice_account_activated)
Chris@0 273 redirect_to :controller => 'my', :action => 'account'
Chris@0 274 else
Chris@0 275 yield if block_given?
Chris@0 276 end
Chris@0 277 end
Chris@909 278
Chris@0 279 # Manual activation by the administrator
Chris@0 280 #
Chris@0 281 # Pass a block for behavior when a user fails to save
Chris@0 282 def register_manually_by_administrator(user, &block)
Chris@0 283 if user.save
Chris@0 284 # Sends an email to the administrators
Chris@1115 285 Mailer.account_activation_request(user).deliver
Chris@0 286 account_pending
Chris@0 287 else
Chris@0 288 yield if block_given?
Chris@0 289 end
Chris@0 290 end
Chris@0 291
Chris@0 292 def account_pending
Chris@0 293 flash[:notice] = l(:notice_account_pending)
Chris@1115 294 redirect_to signin_path
Chris@0 295 end
Chris@0 296 end