annotate .svn/pristine/73/736844820e97591097b5dc584954a779de2b5787.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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