Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require "digest/sha1" Chris@909: Chris@909: class User < Principal Chris@909: include Redmine::SafeAttributes Chris@909: Chris@909: # Account statuses Chris@909: STATUS_ANONYMOUS = 0 Chris@909: STATUS_ACTIVE = 1 Chris@909: STATUS_REGISTERED = 2 Chris@909: STATUS_LOCKED = 3 Chris@909: Chris@909: # Different ways of displaying/sorting users Chris@909: USER_FORMATS = { Chris@909: :firstname_lastname => {:string => '#{firstname} #{lastname}', :order => %w(firstname lastname id)}, Chris@909: :firstname => {:string => '#{firstname}', :order => %w(firstname id)}, Chris@909: :lastname_firstname => {:string => '#{lastname} #{firstname}', :order => %w(lastname firstname id)}, Chris@909: :lastname_coma_firstname => {:string => '#{lastname}, #{firstname}', :order => %w(lastname firstname id)}, Chris@909: :username => {:string => '#{login}', :order => %w(login id)}, Chris@909: } Chris@909: Chris@909: MAIL_NOTIFICATION_OPTIONS = [ Chris@909: ['all', :label_user_mail_option_all], Chris@909: ['selected', :label_user_mail_option_selected], Chris@909: ['only_my_events', :label_user_mail_option_only_my_events], Chris@909: ['only_assigned', :label_user_mail_option_only_assigned], Chris@909: ['only_owner', :label_user_mail_option_only_owner], Chris@909: ['none', :label_user_mail_option_none] Chris@909: ] Chris@909: Chris@909: has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, Chris@909: :after_remove => Proc.new {|user, group| group.user_removed(user)} Chris@909: has_many :changesets, :dependent => :nullify Chris@909: has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' Chris@909: has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" Chris@909: has_one :api_token, :class_name => 'Token', :conditions => "action='api'" Chris@909: belongs_to :auth_source Chris@909: Chris@909: # Active non-anonymous users scope Chris@909: named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}" Chris@909: Chris@909: acts_as_customizable Chris@909: Chris@909: attr_accessor :password, :password_confirmation Chris@909: attr_accessor :last_before_login_on Chris@909: # Prevents unauthorized assignments Chris@909: attr_protected :login, :admin, :password, :password_confirmation, :hashed_password Chris@909: Chris@909: validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } Chris@909: validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false Chris@909: validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false Chris@909: # Login must contain lettres, numbers, underscores only Chris@909: validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i Chris@909: validates_length_of :login, :maximum => 30 Chris@909: validates_length_of :firstname, :lastname, :maximum => 30 Chris@909: validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_blank => true Chris@909: validates_length_of :mail, :maximum => 60, :allow_nil => true Chris@909: validates_confirmation_of :password, :allow_nil => true Chris@909: validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true Chris@909: validate :validate_password_length Chris@909: Chris@909: before_create :set_mail_notification Chris@909: before_save :update_hashed_password Chris@909: before_destroy :remove_references_before_destroy Chris@909: Chris@909: named_scope :in_group, lambda {|group| Chris@909: group_id = group.is_a?(Group) ? group.id : group.to_i Chris@909: { :conditions => ["#{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] } Chris@909: } Chris@909: named_scope :not_in_group, lambda {|group| Chris@909: group_id = group.is_a?(Group) ? group.id : group.to_i Chris@909: { :conditions => ["#{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] } Chris@909: } Chris@909: Chris@909: def set_mail_notification Chris@909: self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? Chris@909: true Chris@909: end Chris@909: Chris@909: def update_hashed_password Chris@909: # update hashed_password if password was set Chris@909: if self.password && self.auth_source_id.blank? Chris@909: salt_password(password) Chris@909: end Chris@909: end Chris@909: Chris@909: def reload(*args) Chris@909: @name = nil Chris@909: @projects_by_role = nil Chris@909: super Chris@909: end Chris@909: Chris@909: def mail=(arg) Chris@909: write_attribute(:mail, arg.to_s.strip) Chris@909: end Chris@909: Chris@909: def identity_url=(url) Chris@909: if url.blank? Chris@909: write_attribute(:identity_url, '') Chris@909: else Chris@909: begin Chris@909: write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) Chris@909: rescue OpenIdAuthentication::InvalidOpenId Chris@909: # Invlaid url, don't save Chris@909: end Chris@909: end Chris@909: self.read_attribute(:identity_url) Chris@909: end Chris@909: Chris@909: # Returns the user that matches provided login and password, or nil Chris@909: def self.try_to_login(login, password) Chris@909: # Make sure no one can sign in with an empty password Chris@909: return nil if password.to_s.empty? Chris@909: user = find_by_login(login) Chris@909: if user Chris@909: # user is already in local database Chris@909: return nil if !user.active? Chris@909: if user.auth_source Chris@909: # user has an external authentication method Chris@909: return nil unless user.auth_source.authenticate(login, password) Chris@909: else Chris@909: # authentication with local password Chris@909: return nil unless user.check_password?(password) Chris@909: end Chris@909: else Chris@909: # user is not yet registered, try to authenticate with available sources Chris@909: attrs = AuthSource.authenticate(login, password) Chris@909: if attrs Chris@909: user = new(attrs) Chris@909: user.login = login Chris@909: user.language = Setting.default_language Chris@909: if user.save Chris@909: user.reload Chris@909: logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source Chris@909: end Chris@909: end Chris@909: end Chris@909: user.update_attribute(:last_login_on, Time.now) if user && !user.new_record? Chris@909: user Chris@909: rescue => text Chris@909: raise text Chris@909: end Chris@909: Chris@909: # Returns the user who matches the given autologin +key+ or nil Chris@909: def self.try_to_autologin(key) Chris@909: tokens = Token.find_all_by_action_and_value('autologin', key) Chris@909: # Make sure there's only 1 token that matches the key Chris@909: if tokens.size == 1 Chris@909: token = tokens.first Chris@909: if (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active? Chris@909: token.user.update_attribute(:last_login_on, Time.now) Chris@909: token.user Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def self.name_formatter(formatter = nil) Chris@909: USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] Chris@909: end Chris@909: Chris@909: # Returns an array of fields names than can be used to make an order statement for users Chris@909: # according to how user names are displayed Chris@909: # Examples: Chris@909: # Chris@909: # User.fields_for_order_statement => ['users.login', 'users.id'] Chris@909: # User.fields_for_order_statement('authors') => ['authors.login', 'authors.id'] Chris@909: def self.fields_for_order_statement(table=nil) Chris@909: table ||= table_name Chris@909: name_formatter[:order].map {|field| "#{table}.#{field}"} Chris@909: end Chris@909: Chris@909: # Return user's full name for display Chris@909: def name(formatter = nil) Chris@909: f = self.class.name_formatter(formatter) Chris@909: if formatter Chris@909: eval('"' + f[:string] + '"') Chris@909: else Chris@909: @name ||= eval('"' + f[:string] + '"') Chris@909: end Chris@909: end Chris@909: Chris@909: def active? Chris@909: self.status == STATUS_ACTIVE Chris@909: end Chris@909: Chris@909: def registered? Chris@909: self.status == STATUS_REGISTERED Chris@909: end Chris@909: Chris@909: def locked? Chris@909: self.status == STATUS_LOCKED Chris@909: end Chris@909: Chris@909: def activate Chris@909: self.status = STATUS_ACTIVE Chris@909: end Chris@909: Chris@909: def register Chris@909: self.status = STATUS_REGISTERED Chris@909: end Chris@909: Chris@909: def lock Chris@909: self.status = STATUS_LOCKED Chris@909: end Chris@909: Chris@909: def activate! Chris@909: update_attribute(:status, STATUS_ACTIVE) Chris@909: end Chris@909: Chris@909: def register! Chris@909: update_attribute(:status, STATUS_REGISTERED) Chris@909: end Chris@909: Chris@909: def lock! Chris@909: update_attribute(:status, STATUS_LOCKED) Chris@909: end Chris@909: Chris@909: # Returns true if +clear_password+ is the correct user's password, otherwise false Chris@909: def check_password?(clear_password) Chris@909: if auth_source_id.present? Chris@909: auth_source.authenticate(self.login, clear_password) Chris@909: else Chris@909: User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password Chris@909: end Chris@909: end Chris@909: Chris@909: # Generates a random salt and computes hashed_password for +clear_password+ Chris@909: # The hashed password is stored in the following form: SHA1(salt + SHA1(password)) Chris@909: def salt_password(clear_password) Chris@909: self.salt = User.generate_salt Chris@909: self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}") Chris@909: end Chris@909: Chris@909: # Does the backend storage allow this user to change their password? Chris@909: def change_password_allowed? Chris@909: return true if auth_source_id.blank? Chris@909: return auth_source.allow_password_changes? Chris@909: end Chris@909: Chris@909: # Generate and set a random password. Useful for automated user creation Chris@909: # Based on Token#generate_token_value Chris@909: # Chris@909: def random_password Chris@909: chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a Chris@909: password = '' Chris@909: 40.times { |i| password << chars[rand(chars.size-1)] } Chris@909: self.password = password Chris@909: self.password_confirmation = password Chris@909: self Chris@909: end Chris@909: Chris@909: def pref Chris@909: self.preference ||= UserPreference.new(:user => self) Chris@909: end Chris@909: Chris@909: def time_zone Chris@909: @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) Chris@909: end Chris@909: Chris@909: def wants_comments_in_reverse_order? Chris@909: self.pref[:comments_sorting] == 'desc' Chris@909: end Chris@909: Chris@909: # Return user's RSS key (a 40 chars long string), used to access feeds Chris@909: def rss_key Chris@909: token = self.rss_token || Token.create(:user => self, :action => 'feeds') Chris@909: token.value Chris@909: end Chris@909: Chris@909: # Return user's API key (a 40 chars long string), used to access the API Chris@909: def api_key Chris@909: token = self.api_token || self.create_api_token(:action => 'api') Chris@909: token.value Chris@909: end Chris@909: Chris@909: # Return an array of project ids for which the user has explicitly turned mail notifications on Chris@909: def notified_projects_ids Chris@909: @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) Chris@909: end Chris@909: Chris@909: def notified_project_ids=(ids) Chris@909: Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) Chris@909: Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? Chris@909: @notified_projects_ids = nil Chris@909: notified_projects_ids Chris@909: end Chris@909: Chris@909: def valid_notification_options Chris@909: self.class.valid_notification_options(self) Chris@909: end Chris@909: Chris@909: # Only users that belong to more than 1 project can select projects for which they are notified Chris@909: def self.valid_notification_options(user=nil) Chris@909: # Note that @user.membership.size would fail since AR ignores Chris@909: # :include association option when doing a count Chris@909: if user.nil? || user.memberships.length < 1 Chris@909: MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} Chris@909: else Chris@909: MAIL_NOTIFICATION_OPTIONS Chris@909: end Chris@909: end Chris@909: Chris@909: # Find a user account by matching the exact login and then a case-insensitive Chris@909: # version. Exact matches will be given priority. Chris@909: def self.find_by_login(login) Chris@909: # force string comparison to be case sensitive on MySQL Chris@909: type_cast = (ActiveRecord::Base.connection.adapter_name == 'MySQL') ? 'BINARY' : '' Chris@909: Chris@909: # First look for an exact match Chris@909: user = first(:conditions => ["#{type_cast} login = ?", login]) Chris@909: # Fail over to case-insensitive if none was found Chris@909: user ||= first(:conditions => ["#{type_cast} LOWER(login) = ?", login.to_s.downcase]) Chris@909: end Chris@909: Chris@909: def self.find_by_rss_key(key) Chris@909: token = Token.find_by_value(key) Chris@909: token && token.user.active? ? token.user : nil Chris@909: end Chris@909: Chris@909: def self.find_by_api_key(key) Chris@909: token = Token.find_by_action_and_value('api', key) Chris@909: token && token.user.active? ? token.user : nil Chris@909: end Chris@909: Chris@909: # Makes find_by_mail case-insensitive Chris@909: def self.find_by_mail(mail) Chris@909: find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase]) Chris@909: end Chris@909: Chris@909: def to_s Chris@909: name Chris@909: end Chris@909: Chris@909: # Returns the current day according to user's time zone Chris@909: def today Chris@909: if time_zone.nil? Chris@909: Date.today Chris@909: else Chris@909: Time.now.in_time_zone(time_zone).to_date Chris@909: end Chris@909: end Chris@909: Chris@909: def logged? Chris@909: true Chris@909: end Chris@909: Chris@909: def anonymous? Chris@909: !logged? Chris@909: end Chris@909: Chris@909: # Return user's roles for project Chris@909: def roles_for_project(project) Chris@909: roles = [] Chris@909: # No role on archived projects Chris@909: return roles unless project && project.active? Chris@909: if logged? Chris@909: # Find project membership Chris@909: membership = memberships.detect {|m| m.project_id == project.id} Chris@909: if membership Chris@909: roles = membership.roles Chris@909: else Chris@909: @role_non_member ||= Role.non_member Chris@909: roles << @role_non_member Chris@909: end Chris@909: else Chris@909: @role_anonymous ||= Role.anonymous Chris@909: roles << @role_anonymous Chris@909: end Chris@909: roles Chris@909: end Chris@909: Chris@909: # Return true if the user is a member of project Chris@909: def member_of?(project) Chris@909: !roles_for_project(project).detect {|role| role.member?}.nil? Chris@909: end Chris@909: Chris@909: # Returns a hash of user's projects grouped by roles Chris@909: def projects_by_role Chris@909: return @projects_by_role if @projects_by_role Chris@909: Chris@909: @projects_by_role = Hash.new {|h,k| h[k]=[]} Chris@909: memberships.each do |membership| Chris@909: membership.roles.each do |role| Chris@909: @projects_by_role[role] << membership.project if membership.project Chris@909: end Chris@909: end Chris@909: @projects_by_role.each do |role, projects| Chris@909: projects.uniq! Chris@909: end Chris@909: Chris@909: @projects_by_role Chris@909: end Chris@909: Chris@909: # Returns true if user is arg or belongs to arg Chris@909: def is_or_belongs_to?(arg) Chris@909: if arg.is_a?(User) Chris@909: self == arg Chris@909: elsif arg.is_a?(Group) Chris@909: arg.users.include?(self) Chris@909: else Chris@909: false Chris@909: end Chris@909: end Chris@909: Chris@909: # Return true if the user is allowed to do the specified action on a specific context Chris@909: # Action can be: Chris@909: # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') Chris@909: # * a permission Symbol (eg. :edit_project) Chris@909: # Context can be: Chris@909: # * a project : returns true if user is allowed to do the specified action on this project Chris@909: # * an array of projects : returns true if user is allowed on every project Chris@909: # * nil with options[:global] set : check if user has at least one role allowed for this action, Chris@909: # or falls back to Non Member / Anonymous permissions depending if the user is logged Chris@909: def allowed_to?(action, context, options={}, &block) Chris@909: if context && context.is_a?(Project) Chris@909: # No action allowed on archived projects Chris@909: return false unless context.active? Chris@909: # No action allowed on disabled modules Chris@909: return false unless context.allows_to?(action) Chris@909: # Admin users are authorized for anything else Chris@909: return true if admin? Chris@909: Chris@909: roles = roles_for_project(context) Chris@909: return false unless roles Chris@909: roles.detect {|role| Chris@909: (context.is_public? || role.member?) && Chris@909: role.allowed_to?(action) && Chris@909: (block_given? ? yield(role, self) : true) Chris@909: } Chris@909: elsif context && context.is_a?(Array) Chris@909: # Authorize if user is authorized on every element of the array Chris@909: context.map do |project| Chris@909: allowed_to?(action, project, options, &block) Chris@909: end.inject do |memo,allowed| Chris@909: memo && allowed Chris@909: end Chris@909: elsif options[:global] Chris@909: # Admin users are always authorized Chris@909: return true if admin? Chris@909: Chris@909: # authorize if user has at least one role that has this permission Chris@909: roles = memberships.collect {|m| m.roles}.flatten.uniq Chris@909: roles << (self.logged? ? Role.non_member : Role.anonymous) Chris@909: roles.detect {|role| Chris@909: role.allowed_to?(action) && Chris@909: (block_given? ? yield(role, self) : true) Chris@909: } Chris@909: else Chris@909: false Chris@909: end Chris@909: end Chris@909: Chris@909: # Is the user allowed to do the specified action on any project? Chris@909: # See allowed_to? for the actions and valid options. Chris@909: def allowed_to_globally?(action, options, &block) Chris@909: allowed_to?(action, nil, options.reverse_merge(:global => true), &block) Chris@909: end Chris@909: Chris@909: safe_attributes 'login', Chris@909: 'firstname', Chris@909: 'lastname', Chris@909: 'mail', Chris@909: 'mail_notification', Chris@909: 'language', Chris@909: 'custom_field_values', Chris@909: 'custom_fields', Chris@909: 'identity_url' Chris@909: Chris@909: safe_attributes 'status', Chris@909: 'auth_source_id', Chris@909: :if => lambda {|user, current_user| current_user.admin?} Chris@909: Chris@909: safe_attributes 'group_ids', Chris@909: :if => lambda {|user, current_user| current_user.admin? && !user.new_record?} Chris@909: Chris@909: # Utility method to help check if a user should be notified about an Chris@909: # event. Chris@909: # Chris@909: # TODO: only supports Issue events currently Chris@909: def notify_about?(object) Chris@909: case mail_notification Chris@909: when 'all' Chris@909: true Chris@909: when 'selected' Chris@909: # user receives notifications for created/assigned issues on unselected projects Chris@909: if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to)) Chris@909: true Chris@909: else Chris@909: false Chris@909: end Chris@909: when 'none' Chris@909: false Chris@909: when 'only_my_events' Chris@909: if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to)) Chris@909: true Chris@909: else Chris@909: false Chris@909: end Chris@909: when 'only_assigned' Chris@909: if object.is_a?(Issue) && is_or_belongs_to?(object.assigned_to) Chris@909: true Chris@909: else Chris@909: false Chris@909: end Chris@909: when 'only_owner' Chris@909: if object.is_a?(Issue) && object.author == self Chris@909: true Chris@909: else Chris@909: false Chris@909: end Chris@909: else Chris@909: false Chris@909: end Chris@909: end Chris@909: Chris@909: def self.current=(user) Chris@909: @current_user = user Chris@909: end Chris@909: Chris@909: def self.current Chris@909: @current_user ||= User.anonymous Chris@909: end Chris@909: Chris@909: # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only Chris@909: # one anonymous user per database. Chris@909: def self.anonymous Chris@909: anonymous_user = AnonymousUser.find(:first) Chris@909: if anonymous_user.nil? Chris@909: anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) Chris@909: raise 'Unable to create the anonymous user.' if anonymous_user.new_record? Chris@909: end Chris@909: anonymous_user Chris@909: end Chris@909: Chris@909: # Salts all existing unsalted passwords Chris@909: # It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) Chris@909: # This method is used in the SaltPasswords migration and is to be kept as is Chris@909: def self.salt_unsalted_passwords! Chris@909: transaction do Chris@909: User.find_each(:conditions => "salt IS NULL OR salt = ''") do |user| Chris@909: next if user.hashed_password.blank? Chris@909: salt = User.generate_salt Chris@909: hashed_password = User.hash_password("#{salt}#{user.hashed_password}") Chris@909: User.update_all("salt = '#{salt}', hashed_password = '#{hashed_password}'", ["id = ?", user.id] ) Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: protected Chris@909: Chris@909: def validate_password_length Chris@909: # Password length validation based on setting Chris@909: if !password.nil? && password.size < Setting.password_min_length.to_i Chris@909: errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) Chris@909: end Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: # Removes references that are not handled by associations Chris@909: # Things that are not deleted are reassociated with the anonymous user Chris@909: def remove_references_before_destroy Chris@909: return if self.id.nil? Chris@909: Chris@909: substitute = User.anonymous Chris@909: Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Chris@909: Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Chris@909: Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Chris@909: Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] Chris@909: Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] Chris@909: JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] Chris@909: JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] Chris@909: Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Chris@909: News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Chris@909: # Remove private queries and keep public ones Chris@909: Query.delete_all ['user_id = ? AND is_public = ?', id, false] Chris@909: Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] Chris@909: TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] Chris@909: Token.delete_all ['user_id = ?', id] Chris@909: Watcher.delete_all ['user_id = ?', id] Chris@909: WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Chris@909: WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Chris@909: end Chris@909: Chris@909: # Return password digest Chris@909: def self.hash_password(clear_password) Chris@909: Digest::SHA1.hexdigest(clear_password || "") Chris@909: end Chris@909: Chris@909: # Returns a 128bits random salt as a hex string (32 chars long) Chris@909: def self.generate_salt Chris@909: ActiveSupport::SecureRandom.hex(16) Chris@909: end Chris@909: Chris@909: end Chris@909: Chris@909: class AnonymousUser < User Chris@909: Chris@909: def validate_on_create Chris@909: # There should be only one AnonymousUser in the database Chris@909: errors.add :base, 'An anonymous user already exists.' if AnonymousUser.find(:first) Chris@909: end Chris@909: Chris@909: def available_custom_fields Chris@909: [] Chris@909: end Chris@909: Chris@909: # Overrides a few properties Chris@909: def logged?; false end Chris@909: def admin; false end Chris@909: def name(*args); I18n.t(:label_user_anonymous) end Chris@909: def mail; nil end Chris@909: def time_zone; nil end Chris@909: def rss_key; nil end Chris@909: Chris@909: # Anonymous user can not be destroyed Chris@909: def destroy Chris@909: false Chris@909: end Chris@909: end