2008-11-30 14:18:22 +03:00
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2008 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
include CustomFieldsHelper
|
|
|
|
|
|
|
|
# prevents login action to be filtered by check_if_login_required application scope filter
|
2007-11-18 20:46:55 +03:00
|
|
|
skip_before_filter :check_if_login_required, :only => [:login, :lost_password, :register, :activate]
|
2007-03-12 20:59:02 +03:00
|
|
|
|
|
|
|
# Show user's account
|
|
|
|
def show
|
2008-11-30 19:57:56 +03:00
|
|
|
@user = User.active.find(params[:id])
|
2008-08-11 22:24:39 +04:00
|
|
|
@custom_values = @user.custom_values
|
2007-06-23 18:06:21 +04:00
|
|
|
|
|
|
|
# show only public projects and private projects that the logged in user is also a member of
|
|
|
|
@memberships = @user.memberships.select do |membership|
|
2008-01-04 21:02:34 +03:00
|
|
|
membership.project.is_public? || (User.current.member_of?(membership.project))
|
2007-06-23 18:06:21 +04:00
|
|
|
end
|
2008-11-30 14:18:22 +03:00
|
|
|
|
|
|
|
events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
|
|
|
|
@events_by_day = events.group_by(&:event_date)
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
|
|
|
|
# Login request and validation
|
|
|
|
def login
|
|
|
|
if request.get?
|
|
|
|
# Logout user
|
2007-08-29 20:52:35 +04:00
|
|
|
self.logged_user = nil
|
2007-03-12 20:59:02 +03:00
|
|
|
else
|
|
|
|
# Authenticate user
|
2009-02-13 00:25:50 +03:00
|
|
|
if Setting.openid? && using_open_id?
|
2009-02-11 22:07:07 +03:00
|
|
|
open_id_authenticate(params[:openid_url])
|
2009-02-11 22:24:28 +03:00
|
|
|
else
|
|
|
|
password_authentication
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Log out current user and redirect to welcome page
|
|
|
|
def logout
|
2007-05-06 16:49:32 +04:00
|
|
|
cookies.delete :autologin
|
2007-08-29 20:52:35 +04:00
|
|
|
Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) if User.current.logged?
|
|
|
|
self.logged_user = nil
|
2007-09-24 22:38:45 +04:00
|
|
|
redirect_to home_url
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
render :template => "account/password_recovery"
|
|
|
|
return
|
|
|
|
else
|
|
|
|
if request.post?
|
|
|
|
user = User.find_by_mail(params[:mail])
|
|
|
|
# user not found in db
|
2007-08-02 21:42:20 +04:00
|
|
|
flash.now[:error] = l(:notice_account_unknown_email) and return unless user
|
2007-03-12 20:59:02 +03:00
|
|
|
# user uses an external authentification
|
2007-08-02 21:42:20 +04:00
|
|
|
flash.now[:error] = l(:notice_can_t_change_password) and 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)
|
|
|
|
redirect_to :action => 'login'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
@user.status = User::STATUS_REGISTERED
|
2008-07-19 14:47:19 +04:00
|
|
|
if session[:auth_source_registration]
|
2007-11-18 20:46:55 +03:00
|
|
|
@user.status = User::STATUS_ACTIVE
|
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
|
2007-08-29 20:52:35 +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
|
|
|
|
redirect_to(home_url) && return unless user.status == User::STATUS_REGISTERED
|
|
|
|
user.status = User::STATUS_ACTIVE
|
|
|
|
if user.save
|
|
|
|
token.destroy
|
|
|
|
flash[:notice] = l(:notice_account_activated)
|
|
|
|
end
|
|
|
|
redirect_to :action => 'login'
|
|
|
|
end
|
|
|
|
|
2009-02-25 17:59:33 +03:00
|
|
|
private
|
|
|
|
|
2009-02-11 22:07:07 +03:00
|
|
|
def password_authentication
|
|
|
|
user = User.try_to_login(params[:username], params[:password])
|
|
|
|
if user.nil?
|
|
|
|
# Invalid credentials
|
|
|
|
flash.now[:error] = l(:notice_account_invalid_creditentials)
|
|
|
|
elsif user.new_record?
|
|
|
|
# Onthefly creation failed, display the registration form to fill/fix attributes
|
|
|
|
@user = user
|
|
|
|
session[:auth_source_registration] = {:login => user.login, :auth_source_id => user.auth_source_id }
|
|
|
|
render :action => 'register'
|
|
|
|
else
|
|
|
|
# Valid user
|
|
|
|
successful_authentication(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2009-02-11 22:07:28 +03:00
|
|
|
user.status = User::STATUS_REGISTERED
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
def successful_authentication(user)
|
|
|
|
# Valid user
|
|
|
|
self.logged_user = user
|
|
|
|
# generate a key and set cookie if autologin
|
|
|
|
if params[:autologin] && Setting.autologin?
|
|
|
|
token = Token.create(:user => user, :action => 'autologin')
|
|
|
|
cookies[:autologin] = { :value => token.value, :expires => 1.year.from_now }
|
|
|
|
end
|
|
|
|
redirect_back_or_default :controller => 'my', :action => 'page'
|
|
|
|
end
|
|
|
|
|
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-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
|
|
|
|
|
|
|
|
# Automatically register a user
|
|
|
|
#
|
|
|
|
# Pass a block for behavior when a user fails to save
|
|
|
|
def register_automatically(user, &block)
|
|
|
|
# Automatic activation
|
|
|
|
user.status = User::STATUS_ACTIVE
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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
|