2009-09-12 12:36:46 +04:00
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2009 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.
|
|
|
|
|
|
|
|
require "digest/sha1"
|
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
class User < Principal
|
2008-04-13 13:12:43 +04:00
|
|
|
|
2007-05-23 21:18:21 +04:00
|
|
|
# Account statuses
|
2007-11-20 18:40:16 +03:00
|
|
|
STATUS_ANONYMOUS = 0
|
2007-05-23 21:18:21 +04:00
|
|
|
STATUS_ACTIVE = 1
|
|
|
|
STATUS_REGISTERED = 2
|
|
|
|
STATUS_LOCKED = 3
|
2008-01-25 13:31:06 +03:00
|
|
|
|
|
|
|
USER_FORMATS = {
|
|
|
|
:firstname_lastname => '#{firstname} #{lastname}',
|
|
|
|
:firstname => '#{firstname}',
|
|
|
|
:lastname_firstname => '#{lastname} #{firstname}',
|
|
|
|
:lastname_coma_firstname => '#{lastname}, #{firstname}',
|
|
|
|
:username => '#{login}'
|
|
|
|
}
|
2007-05-23 21:18:21 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)},
|
|
|
|
:after_remove => Proc.new {|user, group| group.user_removed(user)}
|
2007-06-29 21:21:37 +04:00
|
|
|
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
|
2008-11-10 21:59:06 +03:00
|
|
|
has_many :changesets, :dependent => :nullify
|
2007-03-12 20:59:02 +03:00
|
|
|
has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
|
2007-08-29 20:52:35 +04:00
|
|
|
has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
|
2009-12-23 09:27:28 +03:00
|
|
|
has_one :api_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='api'"
|
2007-03-12 20:59:02 +03:00
|
|
|
belongs_to :auth_source
|
|
|
|
|
2008-11-30 19:57:56 +03:00
|
|
|
# Active non-anonymous users scope
|
|
|
|
named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
|
|
|
|
|
2008-06-28 00:13:56 +04:00
|
|
|
acts_as_customizable
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
attr_accessor :password, :password_confirmation
|
|
|
|
attr_accessor :last_before_login_on
|
|
|
|
# Prevents unauthorized assignments
|
2009-09-12 12:36:46 +04:00
|
|
|
attr_protected :login, :admin, :password, :password_confirmation, :hashed_password, :group_ids
|
2007-03-12 20:59:02 +03:00
|
|
|
|
2007-11-20 18:40:16 +03:00
|
|
|
validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
|
2008-01-06 16:09:23 +03:00
|
|
|
validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }
|
2009-01-10 14:29:35 +03:00
|
|
|
validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false
|
2007-03-12 20:59:02 +03:00
|
|
|
# Login must contain lettres, numbers, underscores only
|
2007-11-20 18:40:16 +03:00
|
|
|
validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i
|
2007-04-02 22:44:35 +04:00
|
|
|
validates_length_of :login, :maximum => 30
|
2008-06-18 00:14:31 +04:00
|
|
|
validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-\.]*$/i
|
2007-04-02 22:44:35 +04:00
|
|
|
validates_length_of :firstname, :lastname, :maximum => 30
|
2007-11-20 18:40:16 +03:00
|
|
|
validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
|
|
|
|
validates_length_of :mail, :maximum => 60, :allow_nil => true
|
2007-03-12 20:59:02 +03:00
|
|
|
validates_confirmation_of :password, :allow_nil => true
|
|
|
|
|
2007-10-20 16:47:05 +04:00
|
|
|
def before_create
|
|
|
|
self.mail_notification = false
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def before_save
|
|
|
|
# update hashed_password if password was set
|
|
|
|
self.hashed_password = User.hash_password(self.password) if self.password
|
|
|
|
end
|
2008-11-11 15:59:28 +03:00
|
|
|
|
|
|
|
def reload(*args)
|
|
|
|
@name = nil
|
|
|
|
super
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
|
2009-02-12 07:31:28 +03:00
|
|
|
def identity_url=(url)
|
2009-02-15 21:46:50 +03:00
|
|
|
if url.blank?
|
|
|
|
write_attribute(:identity_url, '')
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url))
|
|
|
|
rescue OpenIdAuthentication::InvalidOpenId
|
|
|
|
# Invlaid url, don't save
|
|
|
|
end
|
2009-02-12 07:31:28 +03:00
|
|
|
end
|
|
|
|
self.read_attribute(:identity_url)
|
|
|
|
end
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
# Returns the user that matches provided login and password, or nil
|
|
|
|
def self.try_to_login(login, password)
|
2008-03-12 20:56:19 +03:00
|
|
|
# Make sure no one can sign in with an empty password
|
|
|
|
return nil if password.to_s.empty?
|
2007-03-12 20:59:02 +03:00
|
|
|
user = find(:first, :conditions => ["login=?", login])
|
|
|
|
if user
|
|
|
|
# user is already in local database
|
|
|
|
return nil if !user.active?
|
|
|
|
if user.auth_source
|
|
|
|
# user has an external authentication method
|
|
|
|
return nil unless user.auth_source.authenticate(login, password)
|
|
|
|
else
|
|
|
|
# authentication with local password
|
|
|
|
return nil unless User.hash_password(password) == user.hashed_password
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# user is not yet registered, try to authenticate with available sources
|
|
|
|
attrs = AuthSource.authenticate(login, password)
|
|
|
|
if attrs
|
2010-02-25 19:19:55 +03:00
|
|
|
attributes = *attrs
|
|
|
|
user = new(attributes.symbolize_keys.except(:dn))
|
2008-07-19 14:47:19 +04:00
|
|
|
user.login = login
|
|
|
|
user.language = Setting.default_language
|
|
|
|
if user.save
|
|
|
|
user.reload
|
2008-04-13 13:12:43 +04:00
|
|
|
logger.info("User '#{user.login}' created from the LDAP") if logger
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-07-19 14:47:19 +04:00
|
|
|
user.update_attribute(:last_login_on, Time.now) if user && !user.new_record?
|
2007-03-12 20:59:02 +03:00
|
|
|
user
|
2008-04-13 13:12:43 +04:00
|
|
|
rescue => text
|
|
|
|
raise text
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2009-02-25 17:59:33 +03:00
|
|
|
|
|
|
|
# Returns the user who matches the given autologin +key+ or nil
|
|
|
|
def self.try_to_autologin(key)
|
2009-05-13 20:56:31 +04:00
|
|
|
tokens = Token.find_all_by_action_and_value('autologin', key)
|
|
|
|
# Make sure there's only 1 token that matches the key
|
|
|
|
if tokens.size == 1
|
|
|
|
token = tokens.first
|
|
|
|
if (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active?
|
|
|
|
token.user.update_attribute(:last_login_on, Time.now)
|
|
|
|
token.user
|
|
|
|
end
|
2009-02-25 17:59:33 +03:00
|
|
|
end
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
|
|
|
|
# Return user's full name for display
|
2008-01-25 13:31:06 +03:00
|
|
|
def name(formatter = nil)
|
2008-11-13 19:43:39 +03:00
|
|
|
if formatter
|
|
|
|
eval('"' + (USER_FORMATS[formatter] || USER_FORMATS[:firstname_lastname]) + '"')
|
|
|
|
else
|
|
|
|
@name ||= eval('"' + (USER_FORMATS[Setting.user_format] || USER_FORMATS[:firstname_lastname]) + '"')
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
|
|
|
self.status == STATUS_ACTIVE
|
|
|
|
end
|
|
|
|
|
|
|
|
def registered?
|
|
|
|
self.status == STATUS_REGISTERED
|
|
|
|
end
|
|
|
|
|
|
|
|
def locked?
|
|
|
|
self.status == STATUS_LOCKED
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_password?(clear_password)
|
|
|
|
User.hash_password(clear_password) == self.hashed_password
|
|
|
|
end
|
2009-02-11 22:07:12 +03:00
|
|
|
|
|
|
|
# Generate and set a random password. Useful for automated user creation
|
|
|
|
# Based on Token#generate_token_value
|
|
|
|
#
|
|
|
|
def random_password
|
|
|
|
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
|
|
|
|
password = ''
|
|
|
|
40.times { |i| password << chars[rand(chars.size-1)] }
|
|
|
|
self.password = password
|
|
|
|
self.password_confirmation = password
|
|
|
|
self
|
|
|
|
end
|
2007-03-12 20:59:02 +03:00
|
|
|
|
|
|
|
def pref
|
|
|
|
self.preference ||= UserPreference.new(:user => self)
|
|
|
|
end
|
2007-03-17 18:18:50 +03:00
|
|
|
|
2007-11-20 01:28:43 +03:00
|
|
|
def time_zone
|
2008-12-14 18:36:59 +03:00
|
|
|
@time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone])
|
2007-11-20 01:28:43 +03:00
|
|
|
end
|
|
|
|
|
2008-03-05 18:41:54 +03:00
|
|
|
def wants_comments_in_reverse_order?
|
|
|
|
self.pref[:comments_sorting] == 'desc'
|
|
|
|
end
|
|
|
|
|
2007-08-29 20:52:35 +04:00
|
|
|
# Return user's RSS key (a 40 chars long string), used to access feeds
|
|
|
|
def rss_key
|
|
|
|
token = self.rss_token || Token.create(:user => self, :action => 'feeds')
|
|
|
|
token.value
|
2007-03-17 18:18:50 +03:00
|
|
|
end
|
2009-12-23 09:27:28 +03:00
|
|
|
|
|
|
|
# Return user's API key (a 40 chars long string), used to access the API
|
|
|
|
def api_key
|
2009-12-30 03:25:53 +03:00
|
|
|
token = self.api_token || self.create_api_token(:action => 'api')
|
2009-12-23 09:27:28 +03:00
|
|
|
token.value
|
|
|
|
end
|
2007-03-17 18:18:50 +03:00
|
|
|
|
2007-10-20 16:47:05 +04:00
|
|
|
# Return an array of project ids for which the user has explicitly turned mail notifications on
|
|
|
|
def notified_projects_ids
|
|
|
|
@notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notified_project_ids=(ids)
|
|
|
|
Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
|
|
|
|
Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
|
|
|
|
@notified_projects_ids = nil
|
|
|
|
notified_projects_ids
|
|
|
|
end
|
|
|
|
|
2007-03-17 18:18:50 +03:00
|
|
|
def self.find_by_rss_key(key)
|
|
|
|
token = Token.find_by_value(key)
|
|
|
|
token && token.user.active? ? token.user : nil
|
|
|
|
end
|
2007-05-06 16:49:32 +04:00
|
|
|
|
2009-12-23 09:27:28 +03:00
|
|
|
def self.find_by_api_key(key)
|
|
|
|
token = Token.find_by_action_and_value('api', key)
|
|
|
|
token && token.user.active? ? token.user : nil
|
|
|
|
end
|
|
|
|
|
2008-12-12 15:07:09 +03:00
|
|
|
# Makes find_by_mail case-insensitive
|
|
|
|
def self.find_by_mail(mail)
|
|
|
|
find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase])
|
|
|
|
end
|
2007-04-08 21:44:54 +04:00
|
|
|
|
2007-08-29 20:52:35 +04:00
|
|
|
def to_s
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
2009-11-06 22:41:03 +03:00
|
|
|
# Returns the current day according to user's time zone
|
|
|
|
def today
|
|
|
|
if time_zone.nil?
|
|
|
|
Date.today
|
|
|
|
else
|
|
|
|
Time.now.in_time_zone(time_zone).to_date
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-29 20:52:35 +04:00
|
|
|
def logged?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2008-07-16 23:33:15 +04:00
|
|
|
def anonymous?
|
|
|
|
!logged?
|
|
|
|
end
|
|
|
|
|
2009-05-10 14:54:31 +04:00
|
|
|
# Return user's roles for project
|
|
|
|
def roles_for_project(project)
|
|
|
|
roles = []
|
2007-08-29 20:52:35 +04:00
|
|
|
# No role on archived projects
|
2009-05-10 14:54:31 +04:00
|
|
|
return roles unless project && project.active?
|
2007-11-24 18:14:32 +03:00
|
|
|
if logged?
|
|
|
|
# Find project membership
|
|
|
|
membership = memberships.detect {|m| m.project_id == project.id}
|
|
|
|
if membership
|
2009-05-10 14:54:31 +04:00
|
|
|
roles = membership.roles
|
2007-11-24 18:14:32 +03:00
|
|
|
else
|
|
|
|
@role_non_member ||= Role.non_member
|
2009-05-10 14:54:31 +04:00
|
|
|
roles << @role_non_member
|
2007-11-24 18:14:32 +03:00
|
|
|
end
|
2007-08-29 20:52:35 +04:00
|
|
|
else
|
2007-11-24 18:14:32 +03:00
|
|
|
@role_anonymous ||= Role.anonymous
|
2009-05-10 14:54:31 +04:00
|
|
|
roles << @role_anonymous
|
2007-08-29 20:52:35 +04:00
|
|
|
end
|
2009-05-10 14:54:31 +04:00
|
|
|
roles
|
2007-08-29 20:52:35 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return true if the user is a member of project
|
|
|
|
def member_of?(project)
|
2009-05-10 14:54:31 +04:00
|
|
|
!roles_for_project(project).detect {|role| role.member?}.nil?
|
2007-08-29 20:52:35 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return true if the user is allowed to do the specified action on project
|
|
|
|
# action can be:
|
|
|
|
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
|
|
|
|
# * a permission Symbol (eg. :edit_project)
|
2008-03-30 18:20:07 +04:00
|
|
|
def allowed_to?(action, project, options={})
|
|
|
|
if project
|
|
|
|
# No action allowed on archived projects
|
|
|
|
return false unless project.active?
|
|
|
|
# No action allowed on disabled modules
|
|
|
|
return false unless project.allows_to?(action)
|
|
|
|
# Admin users are authorized for anything else
|
|
|
|
return true if admin?
|
|
|
|
|
2009-05-10 14:54:31 +04:00
|
|
|
roles = roles_for_project(project)
|
|
|
|
return false unless roles
|
|
|
|
roles.detect {|role| (project.is_public? || role.member?) && role.allowed_to?(action)}
|
2008-03-30 18:20:07 +04:00
|
|
|
|
|
|
|
elsif options[:global]
|
2009-05-17 16:59:14 +04:00
|
|
|
# Admin users are always authorized
|
|
|
|
return true if admin?
|
|
|
|
|
2008-03-30 18:20:07 +04:00
|
|
|
# authorize if user has at least one role that has this permission
|
2009-05-10 14:54:31 +04:00
|
|
|
roles = memberships.collect {|m| m.roles}.flatten.uniq
|
2008-08-31 20:34:54 +04:00
|
|
|
roles.detect {|r| r.allowed_to?(action)} || (self.logged? ? Role.non_member.allowed_to?(action) : Role.anonymous.allowed_to?(action))
|
2008-03-30 18:20:07 +04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2007-08-29 20:52:35 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.current=(user)
|
|
|
|
@current_user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.current
|
2007-11-20 18:40:16 +03:00
|
|
|
@current_user ||= User.anonymous
|
2007-08-29 20:52:35 +04:00
|
|
|
end
|
|
|
|
|
2009-03-21 03:39:53 +03:00
|
|
|
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
|
|
|
|
# one anonymous user per database.
|
2007-08-29 20:52:35 +04:00
|
|
|
def self.anonymous
|
2007-11-20 18:40:16 +03:00
|
|
|
anonymous_user = AnonymousUser.find(:first)
|
|
|
|
if anonymous_user.nil?
|
|
|
|
anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0)
|
|
|
|
raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
|
|
|
|
end
|
2008-05-22 22:08:54 +04:00
|
|
|
anonymous_user
|
2007-08-29 20:52:35 +04:00
|
|
|
end
|
|
|
|
|
2009-04-21 17:43:57 +04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def validate
|
|
|
|
# Password length validation based on setting
|
|
|
|
if !password.nil? && password.size < Setting.password_min_length.to_i
|
|
|
|
errors.add(:password, :too_short, :count => Setting.password_min_length.to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2009-09-12 12:36:46 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
# Return password digest
|
|
|
|
def self.hash_password(clear_password)
|
|
|
|
Digest::SHA1.hexdigest(clear_password || "")
|
2006-07-09 20:30:01 +04:00
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|
2007-08-29 20:52:35 +04:00
|
|
|
|
|
|
|
class AnonymousUser < User
|
|
|
|
|
2007-11-20 18:40:16 +03:00
|
|
|
def validate_on_create
|
|
|
|
# There should be only one AnonymousUser in the database
|
|
|
|
errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first)
|
2007-11-20 01:28:43 +03:00
|
|
|
end
|
|
|
|
|
2008-06-28 00:13:56 +04:00
|
|
|
def available_custom_fields
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2007-11-20 18:40:16 +03:00
|
|
|
# Overrides a few properties
|
|
|
|
def logged?; false end
|
|
|
|
def admin; false end
|
2009-11-11 13:48:54 +03:00
|
|
|
def name(*args); I18n.t(:label_user_anonymous) end
|
2007-11-20 18:40:16 +03:00
|
|
|
def mail; nil end
|
|
|
|
def time_zone; nil end
|
|
|
|
def rss_key; nil end
|
2007-08-29 20:52:35 +04:00
|
|
|
end
|