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