annotate .svn/pristine/36/36f96aa6819066dbc18f6f0375ee24e3c2d52b96.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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