2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# Copyright (C) 2010-2011 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04: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-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
class AccountController < ApplicationController
|
2011-05-30 22:52:25 +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?
|
2010-03-04 08:33:49 +03:00
|
|
|
logout_user
|
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
|
|
|
|
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-05-30 22:52:25 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
# Enable user to choose a new password
|
|
|
|
def lost_password
|
2007-09-24 22:38:45 +04:00
|
|
|
redirect_to(home_url) && return unless Setting.lost_password?
|
2007-03-12 20:59:02 +03:00
|
|
|
if params[:token]
|
|
|
|
@token = Token.find_by_action_and_value("recovery", params[:token])
|
2007-09-24 22:38:45 +04:00
|
|
|
redirect_to(home_url) && return unless @token and !@token.expired?
|
2007-03-12 20:59:02 +03:00
|
|
|
@user = @token.user
|
|
|
|
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)
|
|
|
|
redirect_to :action => 'login'
|
|
|
|
return
|
2011-05-30 22:52:25 +04:00
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
render :template => "account/password_recovery"
|
|
|
|
return
|
|
|
|
else
|
|
|
|
if request.post?
|
|
|
|
user = User.find_by_mail(params[:mail])
|
|
|
|
# user not found in db
|
2009-12-18 17:22:18 +03:00
|
|
|
(flash.now[:error] = l(:notice_account_unknown_email); return) unless user
|
2007-03-12 20:59:02 +03:00
|
|
|
# user uses an external authentification
|
2009-12-18 17:22:18 +03:00
|
|
|
(flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id
|
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
|
|
|
|
Mailer.deliver_lost_password(token)
|
|
|
|
flash[:notice] = l(:notice_account_lost_email_sent)
|
2011-04-21 03:23:40 +04:00
|
|
|
redirect_to :action => 'login', :back_url => home_url
|
2007-03-12 20:59:02 +03:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
# User self-registration
|
|
|
|
def register
|
2008-07-19 14:47:19 +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
|
2007-11-18 20:46:55 +03:00
|
|
|
@user = User.new(:language => Setting.default_language)
|
2007-03-12 20:59:02 +03:00
|
|
|
else
|
2007-11-18 20:46:55 +03:00
|
|
|
@user = User.new(params[:user])
|
|
|
|
@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)
|
2008-06-09 22:59:15 +04:00
|
|
|
redirect_to :controller => 'my', :action => 'account'
|
2007-11-18 20:46:55 +03:00
|
|
|
end
|
|
|
|
else
|
2008-07-19 14:47:19 +04:00
|
|
|
@user.login = params[:user][:login]
|
|
|
|
@user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
|
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-05-30 22:52:25 +04:00
|
|
|
|
2007-11-18 20:46:55 +03:00
|
|
|
# Token based account activation
|
|
|
|
def activate
|
|
|
|
redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
|
|
|
|
token = Token.find_by_action_and_value('register', params[:token])
|
|
|
|
redirect_to(home_url) && return unless token and !token.expired?
|
|
|
|
user = token.user
|
2010-08-03 19:26:50 +04:00
|
|
|
redirect_to(home_url) && return unless user.registered?
|
|
|
|
user.activate
|
2007-11-18 20:46:55 +03:00
|
|
|
if user.save
|
|
|
|
token.destroy
|
|
|
|
flash[:notice] = l(:notice_account_activated)
|
|
|
|
end
|
|
|
|
redirect_to :action => 'login'
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-25 17:59:33 +03:00
|
|
|
private
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-03-04 08:33:49 +03:00
|
|
|
def logout_user
|
2010-03-12 22:22:11 +03:00
|
|
|
if User.current.logged?
|
2011-03-20 12:18:17 +03:00
|
|
|
cookies.delete Redmine::Configuration['autologin_cookie_name']
|
2010-03-12 22:22:11 +03:00
|
|
|
Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
|
|
|
|
self.logged_user = nil
|
|
|
|
end
|
2010-03-04 08:33:49 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +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
|
|
|
|
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-11 22:07:07 +03:00
|
|
|
def open_id_authenticate(openid_url)
|
|
|
|
authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration|
|
|
|
|
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
|
|
|
|
redirect_to(home_url) && return unless Setting.self_registration?
|
|
|
|
|
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-05-30 22:52:25 +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-05-30 22:52:25 +04:00
|
|
|
|
2009-02-11 22:07:07 +03:00
|
|
|
def successful_authentication(user)
|
|
|
|
# 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 })
|
2009-02-11 22:07:07 +03:00
|
|
|
redirect_back_or_default :controller => 'my', :action => 'page'
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2011-01-23 14:20:46 +03:00
|
|
|
def set_autologin_cookie(user)
|
|
|
|
token = Token.create(:user => user, :action => 'autologin')
|
|
|
|
cookie_options = {
|
|
|
|
:value => token.value,
|
|
|
|
:expires => 1.year.from_now,
|
2011-03-20 12:18:17 +03:00
|
|
|
:path => Redmine::Configuration['autologin_cookie_path'],
|
|
|
|
:secure => Redmine::Configuration['autologin_cookie_secure'],
|
2011-01-23 14:40:07 +03:00
|
|
|
:httponly => true
|
2011-01-23 14:20:46 +03:00
|
|
|
}
|
2011-03-20 12:18:17 +03:00
|
|
|
cookies[Redmine::Configuration['autologin_cookie_name']] = cookie_options
|
2011-01-23 14:20:46 +03:00
|
|
|
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?
|
|
|
|
render :action => 'register'
|
|
|
|
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
|
|
|
|
Mailer.deliver_register(token)
|
|
|
|
flash[:notice] = l(:notice_account_register_done)
|
|
|
|
redirect_to :action => 'login'
|
|
|
|
else
|
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
2011-05-30 22:52:25 +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)
|
|
|
|
redirect_to :controller => 'my', :action => 'account'
|
|
|
|
else
|
|
|
|
yield if block_given?
|
|
|
|
end
|
|
|
|
end
|
2011-05-30 22:52:25 +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
|
|
|
|
Mailer.deliver_account_activation_request(user)
|
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)
|
|
|
|
redirect_to :action => 'login'
|
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|