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 @ 1011:f44860e089c5
History | View | Annotate | Download (9.46 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2011 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 |
end
|
| 33 |
|
| 34 |
# Log out current user and redirect to welcome page
|
| 35 |
def logout |
| 36 |
logout_user |
| 37 |
redirect_to home_url |
| 38 |
end
|
| 39 |
|
| 40 |
# Enable user to choose a new password
|
| 41 |
def lost_password |
| 42 |
redirect_to(home_url) && return unless Setting.lost_password? |
| 43 |
if params[:token] |
| 44 |
@token = Token.find_by_action_and_value("recovery", params[:token]) |
| 45 |
redirect_to(home_url) && return unless @token and !@token.expired? |
| 46 |
@user = @token.user |
| 47 |
if request.post?
|
| 48 |
@user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
| 49 |
if @user.save |
| 50 |
@token.destroy
|
| 51 |
flash[:notice] = l(:notice_account_password_updated) |
| 52 |
redirect_to :action => 'login' |
| 53 |
return
|
| 54 |
end
|
| 55 |
end
|
| 56 |
render :template => "account/password_recovery" |
| 57 |
return
|
| 58 |
else
|
| 59 |
if request.post?
|
| 60 |
user = User.find_by_mail(params[:mail]) |
| 61 |
# user not found in db
|
| 62 |
(flash.now[:error] = l(:notice_account_unknown_email); return) unless user |
| 63 |
# user uses an external authentification
|
| 64 |
(flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id |
| 65 |
# create a new token for password recovery
|
| 66 |
token = Token.new(:user => user, :action => "recovery") |
| 67 |
if token.save
|
| 68 |
Mailer.deliver_lost_password(token)
|
| 69 |
flash[:notice] = l(:notice_account_lost_email_sent) |
| 70 |
redirect_to :action => 'login' |
| 71 |
return
|
| 72 |
end
|
| 73 |
end
|
| 74 |
end
|
| 75 |
end
|
| 76 |
|
| 77 |
# User self-registration
|
| 78 |
def register |
| 79 |
redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration] |
| 80 |
|
| 81 |
if request.get?
|
| 82 |
session[:auth_source_registration] = nil |
| 83 |
@user = User.new(:language => Setting.default_language) |
| 84 |
|
| 85 |
@ssamr_user_details = SsamrUserDetail.new |
| 86 |
|
| 87 |
else
|
| 88 |
@user = User.new(params[:user]) |
| 89 |
@user.admin = false |
| 90 |
|
| 91 |
@user.register
|
| 92 |
|
| 93 |
if session[:auth_source_registration] |
| 94 |
@user.activate
|
| 95 |
@user.login = session[:auth_source_registration][:login] |
| 96 |
@user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
| 97 |
if @user.save |
| 98 |
session[:auth_source_registration] = nil |
| 99 |
self.logged_user = @user |
| 100 |
flash[:notice] = l(:notice_account_activated) |
| 101 |
redirect_to :controller => 'my', :action => 'account' |
| 102 |
end
|
| 103 |
else
|
| 104 |
@user.login = params[:user][:login] |
| 105 |
@user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
| 106 |
|
| 107 |
@ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details]) |
| 108 |
|
| 109 |
# associates the 2 objects
|
| 110 |
@user.ssamr_user_detail = @ssamr_user_details |
| 111 |
@selected_institution_id = params[:ssamr_user_details][:institution_id].to_i |
| 112 |
|
| 113 |
|
| 114 |
case Setting.self_registration |
| 115 |
when '1' |
| 116 |
register_by_email_activation(@user)
|
| 117 |
when '3' |
| 118 |
register_automatically(@user)
|
| 119 |
else
|
| 120 |
register_manually_by_administrator(@user)
|
| 121 |
end
|
| 122 |
end
|
| 123 |
end
|
| 124 |
end
|
| 125 |
|
| 126 |
# Token based account activation
|
| 127 |
def activate |
| 128 |
redirect_to(home_url) && return unless Setting.self_registration? && params[:token] |
| 129 |
token = Token.find_by_action_and_value('register', params[:token]) |
| 130 |
redirect_to(home_url) && return unless token and !token.expired? |
| 131 |
user = token.user |
| 132 |
redirect_to(home_url) && return unless user.registered? |
| 133 |
user.activate |
| 134 |
if user.save
|
| 135 |
token.destroy |
| 136 |
flash[:notice] = l(:notice_account_activated) |
| 137 |
end
|
| 138 |
redirect_to :action => 'login' |
| 139 |
end
|
| 140 |
|
| 141 |
private |
| 142 |
|
| 143 |
def logout_user |
| 144 |
if User.current.logged? |
| 145 |
cookies.delete :autologin
|
| 146 |
Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) |
| 147 |
self.logged_user = nil |
| 148 |
end
|
| 149 |
end
|
| 150 |
|
| 151 |
def authenticate_user |
| 152 |
if Setting.openid? && using_open_id? |
| 153 |
open_id_authenticate(params[:openid_url])
|
| 154 |
else
|
| 155 |
password_authentication |
| 156 |
end
|
| 157 |
end
|
| 158 |
|
| 159 |
def password_authentication |
| 160 |
user = User.try_to_login(params[:username], params[:password]) |
| 161 |
|
| 162 |
if user.nil?
|
| 163 |
invalid_credentials |
| 164 |
elsif user.new_record?
|
| 165 |
onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
|
| 166 |
else
|
| 167 |
# Valid user
|
| 168 |
successful_authentication(user) |
| 169 |
end
|
| 170 |
end
|
| 171 |
|
| 172 |
def open_id_authenticate(openid_url) |
| 173 |
authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration| |
| 174 |
if result.successful?
|
| 175 |
user = User.find_or_initialize_by_identity_url(identity_url)
|
| 176 |
if user.new_record?
|
| 177 |
# Self-registration off
|
| 178 |
redirect_to(home_url) && return unless Setting.self_registration? |
| 179 |
|
| 180 |
# Create on the fly
|
| 181 |
user.login = registration['nickname'] unless registration['nickname'].nil? |
| 182 |
user.mail = registration['email'] unless registration['email'].nil? |
| 183 |
user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
| 184 |
user.random_password |
| 185 |
user.register |
| 186 |
|
| 187 |
case Setting.self_registration |
| 188 |
when '1' |
| 189 |
register_by_email_activation(user) do
|
| 190 |
onthefly_creation_failed(user) |
| 191 |
end
|
| 192 |
when '3' |
| 193 |
register_automatically(user) do
|
| 194 |
onthefly_creation_failed(user) |
| 195 |
end
|
| 196 |
else
|
| 197 |
register_manually_by_administrator(user) do
|
| 198 |
onthefly_creation_failed(user) |
| 199 |
end
|
| 200 |
end
|
| 201 |
else
|
| 202 |
# Existing record
|
| 203 |
if user.active?
|
| 204 |
successful_authentication(user) |
| 205 |
else
|
| 206 |
account_pending |
| 207 |
end
|
| 208 |
end
|
| 209 |
end
|
| 210 |
end
|
| 211 |
end
|
| 212 |
|
| 213 |
def successful_authentication(user) |
| 214 |
# Valid user
|
| 215 |
self.logged_user = user
|
| 216 |
# generate a key and set cookie if autologin
|
| 217 |
if params[:autologin] && Setting.autologin? |
| 218 |
set_autologin_cookie(user) |
| 219 |
end
|
| 220 |
call_hook(:controller_account_success_authentication_after, {:user => user }) |
| 221 |
redirect_back_or_default :controller => 'my', :action => 'page' |
| 222 |
end
|
| 223 |
|
| 224 |
def set_autologin_cookie(user) |
| 225 |
token = Token.create(:user => user, :action => 'autologin') |
| 226 |
cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin' |
| 227 |
cookie_options = {
|
| 228 |
:value => token.value,
|
| 229 |
:expires => 1.year.from_now, |
| 230 |
:path => (Redmine::Configuration['autologin_cookie_path'] || '/'), |
| 231 |
:secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), |
| 232 |
:httponly => true |
| 233 |
} |
| 234 |
cookies[cookie_name] = cookie_options |
| 235 |
end
|
| 236 |
|
| 237 |
# Onthefly creation failed, display the registration form to fill/fix attributes
|
| 238 |
def onthefly_creation_failed(user, auth_source_options = { }) |
| 239 |
@user = user
|
| 240 |
session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? |
| 241 |
render :action => 'register' |
| 242 |
end
|
| 243 |
|
| 244 |
def invalid_credentials |
| 245 |
logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
|
| 246 |
flash.now[:error] = l(:notice_account_invalid_creditentials) |
| 247 |
end
|
| 248 |
|
| 249 |
# Register a user for email activation.
|
| 250 |
#
|
| 251 |
# Pass a block for behavior when a user fails to save
|
| 252 |
def register_by_email_activation(user, &block) |
| 253 |
token = Token.new(:user => user, :action => "register") |
| 254 |
if user.save and token.save |
| 255 |
Mailer.deliver_register(token)
|
| 256 |
flash[:notice] = l(:notice_account_register_done) |
| 257 |
redirect_to :action => 'login' |
| 258 |
else
|
| 259 |
yield if block_given? |
| 260 |
end
|
| 261 |
end
|
| 262 |
|
| 263 |
# Automatically register a user
|
| 264 |
#
|
| 265 |
# Pass a block for behavior when a user fails to save
|
| 266 |
def register_automatically(user, &block) |
| 267 |
# Automatic activation
|
| 268 |
user.activate |
| 269 |
user.last_login_on = Time.now
|
| 270 |
if user.save
|
| 271 |
self.logged_user = user
|
| 272 |
flash[:notice] = l(:notice_account_activated) |
| 273 |
redirect_to :controller => 'my', :action => 'account' |
| 274 |
else
|
| 275 |
yield if block_given? |
| 276 |
end
|
| 277 |
end
|
| 278 |
|
| 279 |
# Manual activation by the administrator
|
| 280 |
#
|
| 281 |
# Pass a block for behavior when a user fails to save
|
| 282 |
def register_manually_by_administrator(user, &block) |
| 283 |
if user.save
|
| 284 |
|
| 285 |
@ssamr_user_details.save!
|
| 286 |
|
| 287 |
# Sends an email to the administrators
|
| 288 |
Mailer.deliver_account_activation_request(user)
|
| 289 |
account_pending |
| 290 |
else
|
| 291 |
yield if block_given? |
| 292 |
end
|
| 293 |
end
|
| 294 |
|
| 295 |
def account_pending |
| 296 |
flash[:notice] = l(:notice_account_pending) |
| 297 |
redirect_to :action => 'login' |
| 298 |
end
|
| 299 |
end
|