annotate .svn/pristine/36/36f96aa6819066dbc18f6f0375ee24e3c2d52b96.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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