user.rb
734 lines
| 23.0 KiB
| text/x-ruby
|
RubyLexer
|
r2755 | # Redmine - project management software | ||
|
r10939 | # Copyright (C) 2006-2013 Jean-Philippe Lang | ||
|
r330 | # | ||
# 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. | ||||
|
r6387 | # | ||
|
r330 | # 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. | ||||
|
r6387 | # | ||
|
r330 | # 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" | ||||
|
r2755 | class User < Principal | ||
|
r4378 | include Redmine::SafeAttributes | ||
|
r6387 | |||
|
r7818 | # Different ways of displaying/sorting users | ||
|
r1089 | USER_FORMATS = { | ||
|
r10331 | :firstname_lastname => { | ||
:string => '#{firstname} #{lastname}', | ||||
:order => %w(firstname lastname id), | ||||
:setting_order => 1 | ||||
}, | ||||
|
r10548 | :firstname_lastinitial => { | ||
:string => '#{firstname} #{lastname.to_s.chars.first}.', | ||||
:order => %w(firstname lastname id), | ||||
:setting_order => 2 | ||||
}, | ||||
|
r10331 | :firstname => { | ||
:string => '#{firstname}', | ||||
:order => %w(firstname id), | ||||
|
r10548 | :setting_order => 3 | ||
|
r10331 | }, | ||
:lastname_firstname => { | ||||
:string => '#{lastname} #{firstname}', | ||||
:order => %w(lastname firstname id), | ||||
|
r10548 | :setting_order => 4 | ||
|
r10331 | }, | ||
:lastname_coma_firstname => { | ||||
:string => '#{lastname}, #{firstname}', | ||||
:order => %w(lastname firstname id), | ||||
|
r10548 | :setting_order => 5 | ||
|
r10331 | }, | ||
|
r10332 | :lastname => { | ||
:string => '#{lastname}', | ||||
:order => %w(lastname id), | ||||
|
r10548 | :setting_order => 6 | ||
|
r10332 | }, | ||
|
r10331 | :username => { | ||
:string => '#{login}', | ||||
:order => %w(login id), | ||||
|
r10548 | :setting_order => 7 | ||
|
r10331 | }, | ||
|
r1089 | } | ||
|
r535 | |||
|
r4102 | MAIL_NOTIFICATION_OPTIONS = [ | ||
|
r4380 | ['all', :label_user_mail_option_all], | ||
['selected', :label_user_mail_option_selected], | ||||
['only_my_events', :label_user_mail_option_only_my_events], | ||||
['only_assigned', :label_user_mail_option_only_assigned], | ||||
['only_owner', :label_user_mail_option_only_owner], | ||||
['none', :label_user_mail_option_none] | ||||
] | ||||
|
r4102 | |||
|
r2755 | 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)} | ||||
|
r2004 | has_many :changesets, :dependent => :nullify | ||
|
r330 | has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' | ||
|
r4606 | has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" | ||
has_one :api_token, :class_name => 'Token', :conditions => "action='api'" | ||||
|
r330 | belongs_to :auth_source | ||
|
r6387 | |||
|
r10723 | scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") } | ||
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } | ||||
|
r6387 | |||
|
r1578 | acts_as_customizable | ||
|
r6387 | |||
|
r11226 | attr_accessor :password, :password_confirmation, :generate_password | ||
|
r330 | attr_accessor :last_before_login_on | ||
# Prevents unauthorized assignments | ||||
|
r4385 | attr_protected :login, :admin, :password, :password_confirmation, :hashed_password | ||
|
r8658 | |||
LOGIN_LENGTH_LIMIT = 60 | ||||
MAIL_LENGTH_LIMIT = 60 | ||||
|
r906 | validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } | ||
|
r9315 | validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, :case_sensitive => false | ||
|
r10656 | validates_uniqueness_of :mail, :if => Proc.new { |user| user.mail_changed? && user.mail.present? }, :case_sensitive => false | ||
|
r11197 | # Login must contain letters, numbers, underscores only | ||
|
r10733 | validates_format_of :login, :with => /\A[a-z0-9_\-@\.]*\z/i | ||
|
r8658 | validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT | ||
|
r397 | validates_length_of :firstname, :lastname, :maximum => 30 | ||
|
r10733 | validates_format_of :mail, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true | ||
|
r8658 | validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true | ||
|
r330 | validates_confirmation_of :password, :allow_nil => true | ||
|
r4380 | validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true | ||
|
r7311 | validate :validate_password_length | ||
|
r330 | |||
|
r6723 | before_create :set_mail_notification | ||
|
r11226 | before_save :generate_password_if_needed, :update_hashed_password | ||
|
r4606 | before_destroy :remove_references_before_destroy | ||
|
r11610 | after_save :update_notified_project_ids | ||
|
r6387 | |||
|
r9355 | scope :in_group, lambda {|group| | ||
|
r5030 | group_id = group.is_a?(Group) ? group.id : group.to_i | ||
|
r10654 | where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) | ||
|
r5030 | } | ||
|
r9355 | scope :not_in_group, lambda {|group| | ||
|
r5164 | group_id = group.is_a?(Group) ? group.id : group.to_i | ||
|
r10654 | where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) | ||
|
r5164 | } | ||
|
r11029 | scope :sorted, lambda { order(*User.fields_for_order_statement)} | ||
|
r6387 | |||
|
r6723 | def set_mail_notification | ||
|
r4109 | self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? | ||
|
r842 | true | ||
end | ||||
|
r6387 | |||
|
r6803 | def update_hashed_password | ||
|
r330 | # update hashed_password if password was set | ||
|
r4816 | if self.password && self.auth_source_id.blank? | ||
salt_password(password) | ||||
end | ||||
|
r330 | end | ||
|
r6387 | |||
|
r11267 | alias :base_reload :reload | ||
|
r2008 | def reload(*args) | ||
@name = nil | ||||
|
r5020 | @projects_by_role = nil | ||
|
r11278 | @membership_by_project_id = nil | ||
|
r11610 | @notified_projects_ids = nil | ||
@notified_projects_ids_changed = false | ||||
|
r11736 | @builtin_role = nil | ||
|
r11267 | base_reload(*args) | ||
|
r2008 | end | ||
|
r6387 | |||
|
r3759 | def mail=(arg) | ||
write_attribute(:mail, arg.to_s.strip) | ||||
end | ||||
|
r6387 | |||
|
r2392 | def identity_url=(url) | ||
|
r2415 | if url.blank? | ||
write_attribute(:identity_url, '') | ||||
else | ||||
begin | ||||
write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) | ||||
rescue OpenIdAuthentication::InvalidOpenId | ||||
|
r11197 | # Invalid url, don't save | ||
|
r2415 | end | ||
|
r2392 | end | ||
self.read_attribute(:identity_url) | ||||
end | ||||
|
r6387 | |||
|
r330 | # Returns the user that matches provided login and password, or nil | ||
|
r11716 | def self.try_to_login(login, password, active_only=true) | ||
|
r9616 | login = login.to_s | ||
password = password.to_s | ||||
|
r11145 | # Make sure no one can sign in with an empty login or password | ||
return nil if login.empty? || password.empty? | ||||
|
r8910 | user = find_by_login(login) | ||
|
r330 | if user | ||
# user is already in local database | ||||
|
r11145 | return nil unless user.check_password?(password) | ||
|
r11724 | return nil if !user.active? && active_only | ||
|
r330 | else | ||
# user is not yet registered, try to authenticate with available sources | ||||
attrs = AuthSource.authenticate(login, password) | ||||
if attrs | ||||
|
r3378 | user = new(attrs) | ||
|
r1661 | user.login = login | ||
user.language = Setting.default_language | ||||
if user.save | ||||
user.reload | ||||
|
r3631 | logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source | ||
|
r330 | end | ||
end | ||||
|
r6387 | end | ||
|
r11716 | user.update_column(:last_login_on, Time.now) if user && !user.new_record? && user.active? | ||
|
r330 | user | ||
|
r1330 | rescue => text | ||
raise text | ||||
|
r330 | end | ||
|
r6387 | |||
|
r2460 | # Returns the user who matches the given autologin +key+ or nil | ||
def self.try_to_autologin(key) | ||||
|
r11066 | user = Token.find_active_user('autologin', key, Setting.autologin.to_i) | ||
if user | ||||
user.update_column(:last_login_on, Time.now) | ||||
user | ||||
|
r2460 | end | ||
end | ||||
|
r7818 | |||
def self.name_formatter(formatter = nil) | ||||
USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] | ||||
end | ||||
# Returns an array of fields names than can be used to make an order statement for users | ||||
# according to how user names are displayed | ||||
# Examples: | ||||
# | ||||
# User.fields_for_order_statement => ['users.login', 'users.id'] | ||||
# User.fields_for_order_statement('authors') => ['authors.login', 'authors.id'] | ||||
def self.fields_for_order_statement(table=nil) | ||||
table ||= table_name | ||||
name_formatter[:order].map {|field| "#{table}.#{field}"} | ||||
end | ||||
|
r330 | # Return user's full name for display | ||
|
r1089 | def name(formatter = nil) | ||
|
r7818 | f = self.class.name_formatter(formatter) | ||
|
r2029 | if formatter | ||
|
r7818 | eval('"' + f[:string] + '"') | ||
|
r2029 | else | ||
|
r7818 | @name ||= eval('"' + f[:string] + '"') | ||
|
r2029 | end | ||
|
r330 | end | ||
|
r6387 | |||
|
r330 | def active? | ||
self.status == STATUS_ACTIVE | ||||
end | ||||
def registered? | ||||
self.status == STATUS_REGISTERED | ||||
end | ||||
|
r6387 | |||
|
r330 | def locked? | ||
self.status == STATUS_LOCKED | ||||
end | ||||
|
r3792 | def activate | ||
self.status = STATUS_ACTIVE | ||||
end | ||||
def register | ||||
self.status = STATUS_REGISTERED | ||||
end | ||||
def lock | ||||
self.status = STATUS_LOCKED | ||||
end | ||||
def activate! | ||||
update_attribute(:status, STATUS_ACTIVE) | ||||
end | ||||
def register! | ||||
update_attribute(:status, STATUS_REGISTERED) | ||||
end | ||||
def lock! | ||||
update_attribute(:status, STATUS_LOCKED) | ||||
end | ||||
|
r4816 | # Returns true if +clear_password+ is the correct user's password, otherwise false | ||
|
r330 | def check_password?(clear_password) | ||
|
r3631 | if auth_source_id.present? | ||
auth_source.authenticate(self.login, clear_password) | ||||
else | ||||
|
r4816 | User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password | ||
|
r3631 | end | ||
end | ||||
|
r6387 | |||
|
r4816 | # Generates a random salt and computes hashed_password for +clear_password+ | ||
# The hashed password is stored in the following form: SHA1(salt + SHA1(password)) | ||||
def salt_password(clear_password) | ||||
self.salt = User.generate_salt | ||||
self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}") | ||||
end | ||||
|
r3631 | |||
# Does the backend storage allow this user to change their password? | ||||
def change_password_allowed? | ||||
|
r8173 | return true if auth_source.nil? | ||
|
r3631 | return auth_source.allow_password_changes? | ||
|
r330 | end | ||
|
r2382 | |||
|
r11226 | def generate_password? | ||
generate_password == '1' || generate_password == true | ||||
end | ||||
# Generate and set a random password on given length | ||||
def random_password(length=40) | ||||
|
r2382 | chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a | ||
|
r11226 | chars -= %w(0 O 1 l) | ||
|
r2382 | password = '' | ||
|
r11226 | length.times {|i| password << chars[SecureRandom.random_number(chars.size)] } | ||
|
r2382 | self.password = password | ||
self.password_confirmation = password | ||||
self | ||||
end | ||||
|
r6387 | |||
|
r330 | def pref | ||
self.preference ||= UserPreference.new(:user => self) | ||||
end | ||||
|
r6387 | |||
|
r904 | def time_zone | ||
|
r2132 | @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) | ||
|
r904 | end | ||
|
r6387 | |||
|
r1183 | def wants_comments_in_reverse_order? | ||
self.pref[:comments_sorting] == 'desc' | ||||
end | ||||
|
r6387 | |||
|
r663 | # Return user's RSS key (a 40 chars long string), used to access feeds | ||
def rss_key | ||||
|
r9285 | if rss_token.nil? | ||
create_rss_token(:action => 'feeds') | ||||
end | ||||
rss_token.value | ||||
|
r336 | end | ||
|
r3103 | |||
# Return user's API key (a 40 chars long string), used to access the API | ||||
def api_key | ||||
|
r9285 | if api_token.nil? | ||
create_api_token(:action => 'api') | ||||
end | ||||
api_token.value | ||||
|
r3103 | end | ||
|
r6387 | |||
|
r842 | # 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 | ||||
|
r6387 | |||
|
r842 | def notified_project_ids=(ids) | ||
|
r11610 | @notified_projects_ids_changed = true | ||
@notified_projects_ids = ids | ||||
end | ||||
# Updates per project notifications (after_save callback) | ||||
def update_notified_project_ids | ||||
if @notified_projects_ids_changed | ||||
ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : []) | ||||
members.update_all(:mail_notification => false) | ||||
members.where(:project_id => ids).update_all(:mail_notification => true) if ids.any? | ||||
end | ||||
|
r842 | end | ||
|
r11610 | private :update_notified_project_ids | ||
|
r3694 | |||
|
r4110 | def valid_notification_options | ||
|
r4610 | self.class.valid_notification_options(self) | ||
end | ||||
# Only users that belong to more than 1 project can select projects for which they are notified | ||||
def self.valid_notification_options(user=nil) | ||||
|
r4110 | # Note that @user.membership.size would fail since AR ignores | ||
# :include association option when doing a count | ||||
|
r4610 | if user.nil? || user.memberships.length < 1 | ||
|
r4607 | MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} | ||
|
r4110 | else | ||
MAIL_NOTIFICATION_OPTIONS | ||||
end | ||||
end | ||||
|
r3694 | # Find a user account by matching the exact login and then a case-insensitive | ||
# version. Exact matches will be given priority. | ||||
|
r3693 | def self.find_by_login(login) | ||
|
r10992 | if login.present? | ||
login = login.to_s | ||||
# First look for an exact match | ||||
user = where(:login => login).all.detect {|u| u.login == login} | ||||
unless user | ||||
# Fail over to case-insensitive if none was found | ||||
user = where("LOWER(login) = ?", login.downcase).first | ||||
end | ||||
user | ||||
|
r8910 | end | ||
|
r3693 | end | ||
|
r336 | def self.find_by_rss_key(key) | ||
|
r11066 | Token.find_active_user('feeds', key) | ||
|
r336 | end | ||
|
r6387 | |||
|
r3103 | def self.find_by_api_key(key) | ||
|
r11066 | Token.find_active_user('api', key) | ||
|
r3103 | end | ||
|
r6387 | |||
|
r2120 | # Makes find_by_mail case-insensitive | ||
def self.find_by_mail(mail) | ||||
|
r10654 | where("LOWER(mail) = ?", mail.to_s.downcase).first | ||
|
r2120 | end | ||
|
r6387 | |||
|
r9245 | # Returns true if the default admin account can no longer be used | ||
def self.default_admin_account_changed? | ||||
!User.active.find_by_login("admin").try(:check_password?, "admin") | ||||
end | ||||
|
r663 | def to_s | ||
name | ||||
end | ||||
|
r6387 | |||
|
r10462 | CSS_CLASS_BY_STATUS = { | ||
STATUS_ANONYMOUS => 'anon', | ||||
STATUS_ACTIVE => 'active', | ||||
STATUS_REGISTERED => 'registered', | ||||
STATUS_LOCKED => 'locked' | ||||
} | ||||
def css_classes | ||||
"user #{CSS_CLASS_BY_STATUS[status]}" | ||||
end | ||||
|
r2898 | # 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 | ||||
|
r6387 | |||
|
r9543 | # Returns the day of +time+ according to user's time zone | ||
def time_to_date(time) | ||||
if time_zone.nil? | ||||
time.to_date | ||||
else | ||||
time.in_time_zone(time_zone).to_date | ||||
end | ||||
end | ||||
|
r663 | def logged? | ||
true | ||||
end | ||||
|
r6387 | |||
|
r1657 | def anonymous? | ||
!logged? | ||||
end | ||||
|
r6387 | |||
|
r11278 | # Returns user's membership for the given project | ||
# or nil if the user is not a member of project | ||||
def membership(project) | ||||
project_id = project.is_a?(Project) ? project.id : project | ||||
@membership_by_project_id ||= Hash.new {|h, project_id| | ||||
h[project_id] = memberships.where(:project_id => project_id).first | ||||
} | ||||
@membership_by_project_id[project_id] | ||||
end | ||||
|
r11735 | # Returns the user's bult-in role | ||
def builtin_role | ||||
|
r11737 | @builtin_role ||= Role.non_member | ||
|
r11735 | end | ||
|
r2627 | # Return user's roles for project | ||
def roles_for_project(project) | ||||
roles = [] | ||||
|
r663 | # No role on archived projects | ||
|
r9700 | return roles if project.nil? || project.archived? | ||
|
r11737 | if membership = membership(project) | ||
roles = membership.roles | ||||
|
r663 | else | ||
|
r11735 | roles << builtin_role | ||
|
r663 | end | ||
|
r2627 | roles | ||
|
r663 | end | ||
|
r6387 | |||
|
r663 | # Return true if the user is a member of project | ||
def member_of?(project) | ||||
|
r11375 | projects.to_a.include?(project) | ||
|
r663 | end | ||
|
r6387 | |||
|
r5020 | # Returns a hash of user's projects grouped by roles | ||
def projects_by_role | ||||
return @projects_by_role if @projects_by_role | ||||
|
r6387 | |||
|
r10059 | @projects_by_role = Hash.new([]) | ||
|
r5020 | memberships.each do |membership| | ||
|
r10059 | if membership.project | ||
membership.roles.each do |role| | ||||
@projects_by_role[role] = [] unless @projects_by_role.key?(role) | ||||
@projects_by_role[role] << membership.project | ||||
end | ||||
|
r5020 | end | ||
end | ||||
@projects_by_role.each do |role, projects| | ||||
projects.uniq! | ||||
end | ||||
|
r6387 | |||
|
r5020 | @projects_by_role | ||
end | ||||
|
r6387 | |||
|
r6186 | # Returns true if user is arg or belongs to arg | ||
def is_or_belongs_to?(arg) | ||||
if arg.is_a?(User) | ||||
self == arg | ||||
elsif arg.is_a?(Group) | ||||
arg.users.include?(self) | ||||
else | ||||
false | ||||
end | ||||
end | ||||
|
r6387 | |||
|
r4113 | # Return true if the user is allowed to do the specified action on a specific context | ||
# Action can be: | ||||
|
r663 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') | ||
# * a permission Symbol (eg. :edit_project) | ||||
|
r4113 | # Context can be: | ||
# * a project : returns true if user is allowed to do the specified action on this project | ||||
|
r5296 | # * an array of projects : returns true if user is allowed on every project | ||
|
r6387 | # * nil with options[:global] set : check if user has at least one role allowed for this action, | ||
|
r4113 | # or falls back to Non Member / Anonymous permissions depending if the user is logged | ||
|
r5296 | def allowed_to?(action, context, options={}, &block) | ||
|
r4120 | if context && context.is_a?(Project) | ||
return false unless context.allows_to?(action) | ||||
|
r1297 | # Admin users are authorized for anything else | ||
return true if admin? | ||||
|
r6387 | |||
|
r4120 | roles = roles_for_project(context) | ||
|
r2627 | return false unless roles | ||
|
r10403 | roles.any? {|role| | ||
|
r5296 | (context.is_public? || role.member?) && | ||
role.allowed_to?(action) && | ||||
(block_given? ? yield(role, self) : true) | ||||
} | ||||
|
r4120 | elsif context && context.is_a?(Array) | ||
|
r10403 | if context.empty? | ||
false | ||||
else | ||||
# Authorize if user is authorized on every element of the array | ||||
context.map {|project| allowed_to?(action, project, options, &block)}.reduce(:&) | ||||
|
r4113 | end | ||
|
r1297 | elsif options[:global] | ||
|
r2651 | # Admin users are always authorized | ||
return true if admin? | ||||
|
r6387 | |||
|
r1297 | # authorize if user has at least one role that has this permission | ||
|
r2627 | roles = memberships.collect {|m| m.roles}.flatten.uniq | ||
|
r5296 | roles << (self.logged? ? Role.non_member : Role.anonymous) | ||
|
r10403 | roles.any? {|role| | ||
|
r5296 | role.allowed_to?(action) && | ||
(block_given? ? yield(role, self) : true) | ||||
} | ||||
|
r1297 | else | ||
false | ||||
end | ||||
|
r663 | end | ||
|
r4050 | |||
# Is the user allowed to do the specified action on any project? | ||||
# See allowed_to? for the actions and valid options. | ||||
|
r5296 | def allowed_to_globally?(action, options, &block) | ||
allowed_to?(action, nil, options.reverse_merge(:global => true), &block) | ||||
|
r4050 | end | ||
|
r4378 | |||
|
r11759 | # Returns true if the user is allowed to delete the user's own account | ||
|
r9283 | def own_account_deletable? | ||
Setting.unsubscribe? && | ||||
|
r10654 | (!admin? || User.active.where("admin = ? AND id <> ?", true, id).exists?) | ||
|
r9283 | end | ||
|
r4378 | safe_attributes 'login', | ||
'firstname', | ||||
'lastname', | ||||
'mail', | ||||
'mail_notification', | ||||
|
r11610 | 'notified_project_ids', | ||
|
r4378 | 'language', | ||
'custom_field_values', | ||||
'custom_fields', | ||||
'identity_url' | ||||
|
r6387 | |||
|
r4378 | safe_attributes 'status', | ||
'auth_source_id', | ||||
|
r11226 | 'generate_password', | ||
|
r4378 | :if => lambda {|user, current_user| current_user.admin?} | ||
|
r6387 | |||
|
r4385 | safe_attributes 'group_ids', | ||
:if => lambda {|user, current_user| current_user.admin? && !user.new_record?} | ||||
|
r6387 | |||
|
r4104 | # Utility method to help check if a user should be notified about an | ||
# event. | ||||
# | ||||
# TODO: only supports Issue events currently | ||||
def notify_about?(object) | ||||
|
r11041 | if mail_notification == 'all' | ||
|
r4104 | true | ||
|
r11041 | elsif mail_notification.blank? || mail_notification == 'none' | ||
|
r4104 | false | ||
|
r11041 | else | ||
case object | ||||
when Issue | ||||
case mail_notification | ||||
when 'selected', 'only_my_events' | ||||
# user receives notifications for created/assigned issues on unselected projects | ||||
object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) | ||||
when 'only_assigned' | ||||
is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) | ||||
when 'only_owner' | ||||
object.author == self | ||||
end | ||||
when News | ||||
# always send to project members except when mail_notification is set to 'none' | ||||
|
r4104 | true | ||
end | ||||
end | ||||
end | ||||
|
r6387 | |||
|
r663 | def self.current=(user) | ||
|
r10684 | Thread.current[:current_user] = user | ||
|
r663 | end | ||
|
r6387 | |||
|
r663 | def self.current | ||
|
r10684 | Thread.current[:current_user] ||= User.anonymous | ||
|
r663 | end | ||
|
r6387 | |||
|
r2536 | # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only | ||
# one anonymous user per database. | ||||
|
r663 | def self.anonymous | ||
|
r10654 | anonymous_user = AnonymousUser.first | ||
|
r906 | 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 | ||||
|
r1429 | anonymous_user | ||
|
r663 | end | ||
|
r4816 | |||
# Salts all existing unsalted passwords | ||||
# It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) | ||||
# This method is used in the SaltPasswords migration and is to be kept as is | ||||
def self.salt_unsalted_passwords! | ||||
transaction do | ||||
|
r10654 | User.where("salt IS NULL OR salt = ''").find_each do |user| | ||
|
r4816 | next if user.hashed_password.blank? | ||
salt = User.generate_salt | ||||
hashed_password = User.hash_password("#{salt}#{user.hashed_password}") | ||||
|
r10655 | User.where(:id => user.id).update_all(:salt => salt, :hashed_password => hashed_password) | ||
|
r4816 | end | ||
end | ||||
end | ||||
|
r6387 | |||
|
r2586 | protected | ||
|
r6387 | |||
|
r7311 | def validate_password_length | ||
|
r11226 | return if password.blank? && generate_password? | ||
|
r2586 | # 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 | ||||
|
r6387 | |||
|
r2586 | private | ||
|
r6387 | |||
|
r11226 | def generate_password_if_needed | ||
if generate_password? && auth_source.nil? | ||||
length = [Setting.password_min_length.to_i + 2, 10].max | ||||
random_password(length) | ||||
end | ||||
end | ||||
|
r4606 | # Removes references that are not handled by associations | ||
# Things that are not deleted are reassociated with the anonymous user | ||||
def remove_references_before_destroy | ||||
return if self.id.nil? | ||||
|
r6387 | |||
|
r4606 | substitute = User.anonymous | ||
Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | ||||
Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | ||||
Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | ||||
Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] | ||||
Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | ||||
JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] | ||||
JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] | ||||
Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | ||||
News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | ||||
# Remove private queries and keep public ones | ||||
|
r8020 | ::Query.delete_all ['user_id = ? AND is_public = ?', id, false] | ||
::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | ||||
|
r4606 | TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] | ||
Token.delete_all ['user_id = ?', id] | ||||
Watcher.delete_all ['user_id = ?', id] | ||||
WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | ||||
WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] | ||||
end | ||||
|
r6387 | |||
|
r330 | # Return password digest | ||
def self.hash_password(clear_password) | ||||
Digest::SHA1.hexdigest(clear_password || "") | ||||
|
r5 | end | ||
|
r6387 | |||
|
r4816 | # Returns a 128bits random salt as a hex string (32 chars long) | ||
def self.generate_salt | ||||
|
r8951 | Redmine::Utils.random_hex(16) | ||
|
r4816 | end | ||
|
r6387 | |||
|
r2 | end | ||
|
r663 | |||
class AnonymousUser < User | ||||
|
r8173 | validate :validate_anonymous_uniqueness, :on => :create | ||
|
r8176 | |||
|
r8173 | def validate_anonymous_uniqueness | ||
|
r906 | # There should be only one AnonymousUser in the database | ||
|
r10701 | errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? | ||
|
r904 | end | ||
|
r6387 | |||
|
r1578 | def available_custom_fields | ||
[] | ||||
end | ||||
|
r6387 | |||
|
r906 | # Overrides a few properties | ||
def logged?; false end | ||||
def admin; false end | ||||
|
r2910 | def name(*args); I18n.t(:label_user_anonymous) end | ||
|
r906 | def mail; nil end | ||
def time_zone; nil end | ||||
def rss_key; nil end | ||||
|
r6387 | |||
|
r9734 | def pref | ||
UserPreference.new(:user => self) | ||||
end | ||||
|
r11737 | # Returns the user's bult-in role | ||
def builtin_role | ||||
@builtin_role ||= Role.anonymous | ||||
end | ||||
def membership(*args) | ||||
nil | ||||
end | ||||
def member_of?(*args) | ||||
|
r11375 | false | ||
end | ||||
|
r4606 | # Anonymous user can not be destroyed | ||
def destroy | ||||
false | ||||
end | ||||
|
r663 | end | ||