2008-11-30 14:18:22 +03:00
|
|
|
# Redmine - project management software
|
2013-01-12 13:29:31 +04:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2007-03-12 20:59:02 +03:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-31 16:08:29 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-31 16:08:29 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class AccountController < ApplicationController
|
|
|
|
helper :custom_fields
|
2011-08-31 16:08:29 +04:00
|
|
|
include CustomFieldsHelper
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
# prevents login action to be filtered by check_if_login_required application scope filter
|
2009-10-29 21:37:00 +03:00
|
|
|
skip_before_filter :check_if_login_required
|
2007-03-12 20:59:02 +03:00
|
|
|
|
|
|
|
# Login request and validation
|
|
|
|
def login
|
|
|
|
if request.get?
|
2013-01-05 17:53:18 +04:00
|
|
|
if User.current.logged?
|
|
|
|
redirect_to home_url
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
else
|
2010-03-04 08:33:54 +03:00
|
|
|
authenticate_user
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2012-02-19 22:13:46 +04:00
|
|
|
rescue AuthSourceException => e
|
|
|
|
logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
|
|
|
|
render_error :message => e.message
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# Log out current user and redirect to welcome page
|
|
|
|
def logout
|
2010-03-04 08:33:49 +03:00
|
|
|
logout_user
|
2007-09-24 22:38:45 +04:00
|
|
|
redirect_to home_url
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2012-07-07 22:05:32 +04:00
|
|
|
# Lets user choose a new password
|
2007-03-12 20:59:02 +03:00
|
|
|
def lost_password
|
2013-01-20 17:54:42 +04:00
|
|
|
(redirect_to(home_url); return) unless Setting.lost_password?
|
2007-03-12 20:59:02 +03:00
|
|
|
if params[:token]
|
2012-07-07 22:05:32 +04:00
|
|
|
@token = Token.find_by_action_and_value("recovery", params[:token].to_s)
|
|
|
|
if @token.nil? || @token.expired?
|
|
|
|
redirect_to home_url
|
|
|
|
return
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
@user = @token.user
|
2012-07-08 11:36:58 +04:00
|
|
|
unless @user && @user.active?
|
|
|
|
redirect_to home_url
|
|
|
|
return
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
if request.post?
|
|
|
|
@user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
|
|
|
|
if @user.save
|
|
|
|
@token.destroy
|
|
|
|
flash[:notice] = l(:notice_account_password_updated)
|
2012-07-07 22:05:32 +04:00
|
|
|
redirect_to signin_path
|
2007-03-12 20:59:02 +03:00
|
|
|
return
|
2011-08-31 16:08:29 +04:00
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
render :template => "account/password_recovery"
|
|
|
|
return
|
|
|
|
else
|
|
|
|
if request.post?
|
2012-07-07 22:27:34 +04:00
|
|
|
user = User.find_by_mail(params[:mail].to_s)
|
|
|
|
# user not found or not active
|
|
|
|
unless user && user.active?
|
|
|
|
flash.now[:error] = l(:notice_account_unknown_email)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
# user cannot change its password
|
|
|
|
unless user.change_password_allowed?
|
|
|
|
flash.now[:error] = l(:notice_can_t_change_password)
|
|
|
|
return
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
# create a new token for password recovery
|
|
|
|
token = Token.new(:user => user, :action => "recovery")
|
|
|
|
if token.save
|
2012-05-05 17:07:12 +04:00
|
|
|
Mailer.lost_password(token).deliver
|
2007-03-12 20:59:02 +03:00
|
|
|
flash[:notice] = l(:notice_account_lost_email_sent)
|
2012-07-07 21:45:12 +04:00
|
|
|
redirect_to signin_path
|
2007-03-12 20:59:02 +03:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
# User self-registration
|
|
|
|
def register
|
2013-01-20 17:54:42 +04:00
|
|
|
(redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration]
|
2007-11-18 20:46:55 +03:00
|
|
|
if request.get?
|
2008-07-19 14:47:19 +04:00
|
|
|
session[:auth_source_registration] = nil
|
2012-12-12 10:05:35 +04:00
|
|
|
@user = User.new(:language => current_language.to_s)
|
2007-03-12 20:59:02 +03:00
|
|
|
else
|
2012-07-07 10:22:35 +04:00
|
|
|
user_params = params[:user] || {}
|
2012-02-05 16:21:21 +04:00
|
|
|
@user = User.new
|
2012-07-07 10:22:35 +04:00
|
|
|
@user.safe_attributes = user_params
|
2007-11-18 20:46:55 +03:00
|
|
|
@user.admin = false
|
2010-08-03 19:26:50 +04:00
|
|
|
@user.register
|
2008-07-19 14:47:19 +04:00
|
|
|
if session[:auth_source_registration]
|
2010-08-03 19:26:50 +04:00
|
|
|
@user.activate
|
2008-07-19 14:47:19 +04:00
|
|
|
@user.login = session[:auth_source_registration][:login]
|
|
|
|
@user.auth_source_id = session[:auth_source_registration][:auth_source_id]
|
2007-11-18 20:46:55 +03:00
|
|
|
if @user.save
|
2008-07-19 14:47:19 +04:00
|
|
|
session[:auth_source_registration] = nil
|
2008-06-09 22:59:15 +04:00
|
|
|
self.logged_user = @user
|
2007-11-18 20:46:55 +03:00
|
|
|
flash[:notice] = l(:notice_account_activated)
|
2012-12-11 21:51:30 +04:00
|
|
|
redirect_to my_account_path
|
2007-11-18 20:46:55 +03:00
|
|
|
end
|
|
|
|
else
|
2008-07-19 14:47:19 +04:00
|
|
|
@user.login = params[:user][:login]
|
2012-07-07 10:22:35 +04:00
|
|
|
unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
|
|
|
|
@user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
|
|
|
|
end
|
2009-02-11 22:07:34 +03:00
|
|
|
|
2008-07-19 14:47:19 +04:00
|
|
|
case Setting.self_registration
|
|
|
|
when '1'
|
2009-02-11 22:07:34 +03:00
|
|
|
register_by_email_activation(@user)
|
2008-07-19 14:47:19 +04:00
|
|
|
when '3'
|
2009-02-11 22:07:34 +03:00
|
|
|
register_automatically(@user)
|
2008-07-19 14:47:19 +04:00
|
|
|
else
|
2009-02-11 22:07:34 +03:00
|
|
|
register_manually_by_administrator(@user)
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2007-11-18 20:46:55 +03:00
|
|
|
# Token based account activation
|
|
|
|
def activate
|
2013-01-20 17:53:26 +04:00
|
|
|
(redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present?
|
2013-01-20 17:50:51 +04:00
|
|
|
token = Token.find_by_action_and_value('register', params[:token].to_s)
|
2013-01-20 17:53:26 +04:00
|
|
|
(redirect_to(home_url); return) unless token and !token.expired?
|
2007-11-18 20:46:55 +03:00
|
|
|
user = token.user
|
2013-01-20 17:53:26 +04:00
|
|
|
(redirect_to(home_url); return) unless user.registered?
|
2010-08-03 19:26:50 +04:00
|
|
|
user.activate
|
2007-11-18 20:46:55 +03:00
|
|
|
if user.save
|
|
|
|
token.destroy
|
|
|
|
flash[:notice] = l(:notice_account_activated)
|
|
|
|
end
|
2012-07-07 21:45:12 +04:00
|
|
|
redirect_to signin_path
|
2007-11-18 20:46:55 +03:00
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2009-02-25 17:59:33 +03:00
|
|
|
private
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2010-03-04 08:33:54 +03:00
|
|
|
def authenticate_user
|
|
|
|
if Setting.openid? && using_open_id?
|
|
|
|
open_id_authenticate(params[:openid_url])
|
|
|
|
else
|
|
|
|
password_authentication
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-11 22:07:07 +03:00
|
|
|
def password_authentication
|
|
|
|
user = User.try_to_login(params[:username], params[:password])
|
2009-12-21 05:24:44 +03:00
|
|
|
|
2009-02-11 22:07:07 +03:00
|
|
|
if user.nil?
|
2009-12-21 05:24:44 +03:00
|
|
|
invalid_credentials
|
2009-02-11 22:07:07 +03:00
|
|
|
elsif user.new_record?
|
2009-12-20 23:13:50 +03:00
|
|
|
onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
|
2009-02-11 22:07:07 +03:00
|
|
|
else
|
|
|
|
# Valid user
|
|
|
|
successful_authentication(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_id_authenticate(openid_url)
|
2012-04-29 12:48:50 +04:00
|
|
|
authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
|
2009-02-11 22:07:07 +03:00
|
|
|
if result.successful?
|
|
|
|
user = User.find_or_initialize_by_identity_url(identity_url)
|
|
|
|
if user.new_record?
|
2009-02-11 22:07:41 +03:00
|
|
|
# Self-registration off
|
2013-01-20 17:54:42 +04:00
|
|
|
(redirect_to(home_url); return) unless Setting.self_registration?
|
2009-02-11 22:07:41 +03:00
|
|
|
|
2009-02-11 22:07:07 +03:00
|
|
|
# Create on the fly
|
2009-02-11 22:07:23 +03:00
|
|
|
user.login = registration['nickname'] unless registration['nickname'].nil?
|
|
|
|
user.mail = registration['email'] unless registration['email'].nil?
|
|
|
|
user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
|
2009-02-11 22:07:12 +03:00
|
|
|
user.random_password
|
2010-08-03 19:26:50 +04:00
|
|
|
user.register
|
2009-02-11 22:07:28 +03:00
|
|
|
|
|
|
|
case Setting.self_registration
|
|
|
|
when '1'
|
2009-02-11 22:07:34 +03:00
|
|
|
register_by_email_activation(user) do
|
2009-02-20 03:31:50 +03:00
|
|
|
onthefly_creation_failed(user)
|
2009-02-11 22:07:28 +03:00
|
|
|
end
|
|
|
|
when '3'
|
2009-02-11 22:07:34 +03:00
|
|
|
register_automatically(user) do
|
2009-02-20 03:31:50 +03:00
|
|
|
onthefly_creation_failed(user)
|
2009-02-11 22:07:28 +03:00
|
|
|
end
|
2009-02-11 22:07:12 +03:00
|
|
|
else
|
2009-02-11 22:07:34 +03:00
|
|
|
register_manually_by_administrator(user) do
|
2009-02-20 03:31:50 +03:00
|
|
|
onthefly_creation_failed(user)
|
2009-02-11 22:07:28 +03:00
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
end
|
2009-02-11 22:07:07 +03:00
|
|
|
else
|
2009-02-11 22:07:12 +03:00
|
|
|
# Existing record
|
2009-02-20 03:16:45 +03:00
|
|
|
if user.active?
|
|
|
|
successful_authentication(user)
|
|
|
|
else
|
|
|
|
account_pending
|
|
|
|
end
|
2009-02-11 22:07:07 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2009-02-11 22:07:07 +03:00
|
|
|
def successful_authentication(user)
|
2012-08-07 23:50:18 +04:00
|
|
|
logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
|
2009-02-11 22:07:07 +03:00
|
|
|
# Valid user
|
|
|
|
self.logged_user = user
|
|
|
|
# generate a key and set cookie if autologin
|
|
|
|
if params[:autologin] && Setting.autologin?
|
2011-01-23 14:20:46 +03:00
|
|
|
set_autologin_cookie(user)
|
2009-02-11 22:07:07 +03:00
|
|
|
end
|
2009-03-21 02:52:42 +03:00
|
|
|
call_hook(:controller_account_success_authentication_after, {:user => user })
|
2012-12-11 21:51:30 +04:00
|
|
|
redirect_back_or_default my_page_path
|
2009-02-11 22:07:07 +03:00
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2011-01-23 14:20:46 +03:00
|
|
|
def set_autologin_cookie(user)
|
|
|
|
token = Token.create(:user => user, :action => 'autologin')
|
|
|
|
cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
|
|
|
|
cookie_options = {
|
|
|
|
:value => token.value,
|
|
|
|
:expires => 1.year.from_now,
|
|
|
|
:path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
|
2011-01-23 14:40:07 +03:00
|
|
|
:secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
|
|
|
|
:httponly => true
|
2011-01-23 14:20:46 +03:00
|
|
|
}
|
|
|
|
cookies[cookie_name] = cookie_options
|
|
|
|
end
|
2009-02-11 22:07:07 +03:00
|
|
|
|
2009-02-11 22:07:28 +03:00
|
|
|
# Onthefly creation failed, display the registration form to fill/fix attributes
|
|
|
|
def onthefly_creation_failed(user, auth_source_options = { })
|
|
|
|
@user = user
|
|
|
|
session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
|
2012-09-17 22:31:20 +04:00
|
|
|
render :action => 'register'
|
2009-02-11 22:07:28 +03:00
|
|
|
end
|
|
|
|
|
2009-12-21 05:24:44 +03:00
|
|
|
def invalid_credentials
|
2010-02-11 21:25:38 +03:00
|
|
|
logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
|
2009-12-21 05:24:44 +03:00
|
|
|
flash.now[:error] = l(:notice_account_invalid_creditentials)
|
|
|
|
end
|
|
|
|
|
2009-02-11 22:07:34 +03:00
|
|
|
# Register a user for email activation.
|
|
|
|
#
|
|
|
|
# Pass a block for behavior when a user fails to save
|
|
|
|
def register_by_email_activation(user, &block)
|
|
|
|
token = Token.new(:user => user, :action => "register")
|
|
|
|
if user.save and token.save
|
2012-05-05 17:07:12 +04:00
|
|
|
Mailer.register(token).deliver
|
2009-02-11 22:07:34 +03:00
|
|
|
flash[:notice] = l(:notice_account_register_done)
|
2012-07-07 21:45:12 +04:00
|
|
|
redirect_to signin_path
|
2009-02-11 22:07:34 +03:00
|
|
|
else
|
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2009-02-11 22:07:34 +03:00
|
|
|
# Automatically register a user
|
|
|
|
#
|
|
|
|
# Pass a block for behavior when a user fails to save
|
|
|
|
def register_automatically(user, &block)
|
|
|
|
# Automatic activation
|
2010-08-03 19:26:50 +04:00
|
|
|
user.activate
|
2009-03-15 13:49:18 +03:00
|
|
|
user.last_login_on = Time.now
|
2009-02-11 22:07:34 +03:00
|
|
|
if user.save
|
|
|
|
self.logged_user = user
|
|
|
|
flash[:notice] = l(:notice_account_activated)
|
2012-12-11 21:51:30 +04:00
|
|
|
redirect_to my_account_path
|
2009-02-11 22:07:34 +03:00
|
|
|
else
|
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
2011-08-31 16:08:29 +04:00
|
|
|
|
2009-02-11 22:07:34 +03:00
|
|
|
# Manual activation by the administrator
|
|
|
|
#
|
|
|
|
# Pass a block for behavior when a user fails to save
|
|
|
|
def register_manually_by_administrator(user, &block)
|
|
|
|
if user.save
|
|
|
|
# Sends an email to the administrators
|
2012-05-05 17:07:12 +04:00
|
|
|
Mailer.account_activation_request(user).deliver
|
2009-02-20 03:16:45 +03:00
|
|
|
account_pending
|
2009-02-11 22:07:34 +03:00
|
|
|
else
|
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
2009-02-20 03:16:45 +03:00
|
|
|
|
|
|
|
def account_pending
|
|
|
|
flash[:notice] = l(:notice_account_pending)
|
2012-07-07 21:45:12 +04:00
|
|
|
redirect_to signin_path
|
2009-02-20 03:16:45 +03:00
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|