remove trailing white-spaces from app/models/user.rb.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6507 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2011-08-21 01:57:25 +00:00
parent 33745d1972
commit e86f9711b9
1 changed files with 60 additions and 60 deletions

View File

@ -5,12 +5,12 @@
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -19,13 +19,13 @@ require "digest/sha1"
class User < Principal class User < Principal
include Redmine::SafeAttributes include Redmine::SafeAttributes
# Account statuses # Account statuses
STATUS_ANONYMOUS = 0 STATUS_ANONYMOUS = 0
STATUS_ACTIVE = 1 STATUS_ACTIVE = 1
STATUS_REGISTERED = 2 STATUS_REGISTERED = 2
STATUS_LOCKED = 3 STATUS_LOCKED = 3
USER_FORMATS = { USER_FORMATS = {
:firstname_lastname => '#{firstname} #{lastname}', :firstname_lastname => '#{firstname} #{lastname}',
:firstname => '#{firstname}', :firstname => '#{firstname}',
@ -50,12 +50,12 @@ class User < Principal
has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'"
has_one :api_token, :class_name => 'Token', :conditions => "action='api'" has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
belongs_to :auth_source belongs_to :auth_source
# Active non-anonymous users scope # Active non-anonymous users scope
named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}" named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
acts_as_customizable acts_as_customizable
attr_accessor :password, :password_confirmation attr_accessor :password, :password_confirmation
attr_accessor :last_before_login_on attr_accessor :last_before_login_on
# Prevents unauthorized assignments # Prevents unauthorized assignments
@ -74,7 +74,7 @@ class User < Principal
validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
before_destroy :remove_references_before_destroy before_destroy :remove_references_before_destroy
named_scope :in_group, lambda {|group| named_scope :in_group, lambda {|group|
group_id = group.is_a?(Group) ? group.id : group.to_i group_id = group.is_a?(Group) ? group.id : group.to_i
{ :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] } { :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] }
@ -83,29 +83,29 @@ class User < Principal
group_id = group.is_a?(Group) ? group.id : group.to_i group_id = group.is_a?(Group) ? group.id : group.to_i
{ :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] } { :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] }
} }
def before_create def before_create
self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
true true
end end
def before_save def before_save
# update hashed_password if password was set # update hashed_password if password was set
if self.password && self.auth_source_id.blank? if self.password && self.auth_source_id.blank?
salt_password(password) salt_password(password)
end end
end end
def reload(*args) def reload(*args)
@name = nil @name = nil
@projects_by_role = nil @projects_by_role = nil
super super
end end
def mail=(arg) def mail=(arg)
write_attribute(:mail, arg.to_s.strip) write_attribute(:mail, arg.to_s.strip)
end end
def identity_url=(url) def identity_url=(url)
if url.blank? if url.blank?
write_attribute(:identity_url, '') write_attribute(:identity_url, '')
@ -118,7 +118,7 @@ class User < Principal
end end
self.read_attribute(:identity_url) self.read_attribute(:identity_url)
end end
# Returns the user that matches provided login and password, or nil # Returns the user that matches provided login and password, or nil
def self.try_to_login(login, password) def self.try_to_login(login, password)
# Make sure no one can sign in with an empty password # Make sure no one can sign in with an empty password
@ -146,13 +146,13 @@ class User < Principal
logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
end end
end end
end end
user.update_attribute(:last_login_on, Time.now) if user && !user.new_record? user.update_attribute(:last_login_on, Time.now) if user && !user.new_record?
user user
rescue => text rescue => text
raise text raise text
end end
# Returns the user who matches the given autologin +key+ or nil # Returns the user who matches the given autologin +key+ or nil
def self.try_to_autologin(key) def self.try_to_autologin(key)
tokens = Token.find_all_by_action_and_value('autologin', key) tokens = Token.find_all_by_action_and_value('autologin', key)
@ -174,7 +174,7 @@ class User < Principal
@name ||= eval('"' + (USER_FORMATS[Setting.user_format] || USER_FORMATS[:firstname_lastname]) + '"') @name ||= eval('"' + (USER_FORMATS[Setting.user_format] || USER_FORMATS[:firstname_lastname]) + '"')
end end
end end
def active? def active?
self.status == STATUS_ACTIVE self.status == STATUS_ACTIVE
end end
@ -182,7 +182,7 @@ class User < Principal
def registered? def registered?
self.status == STATUS_REGISTERED self.status == STATUS_REGISTERED
end end
def locked? def locked?
self.status == STATUS_LOCKED self.status == STATUS_LOCKED
end end
@ -219,7 +219,7 @@ class User < Principal
User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password
end end
end end
# Generates a random salt and computes hashed_password for +clear_password+ # Generates a random salt and computes hashed_password for +clear_password+
# The hashed password is stored in the following form: SHA1(salt + SHA1(password)) # The hashed password is stored in the following form: SHA1(salt + SHA1(password))
def salt_password(clear_password) def salt_password(clear_password)
@ -244,19 +244,19 @@ class User < Principal
self.password_confirmation = password self.password_confirmation = password
self self
end end
def pref def pref
self.preference ||= UserPreference.new(:user => self) self.preference ||= UserPreference.new(:user => self)
end end
def time_zone def time_zone
@time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone])
end end
def wants_comments_in_reverse_order? def wants_comments_in_reverse_order?
self.pref[:comments_sorting] == 'desc' self.pref[:comments_sorting] == 'desc'
end end
# Return user's RSS key (a 40 chars long string), used to access feeds # Return user's RSS key (a 40 chars long string), used to access feeds
def rss_key def rss_key
token = self.rss_token || Token.create(:user => self, :action => 'feeds') token = self.rss_token || Token.create(:user => self, :action => 'feeds')
@ -268,12 +268,12 @@ class User < Principal
token = self.api_token || self.create_api_token(:action => 'api') token = self.api_token || self.create_api_token(:action => 'api')
token.value token.value
end end
# Return an array of project ids for which the user has explicitly turned mail notifications on # Return an array of project ids for which the user has explicitly turned mail notifications on
def notified_projects_ids def notified_projects_ids
@notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id)
end end
def notified_project_ids=(ids) def notified_project_ids=(ids)
Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) 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? Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
@ -301,7 +301,7 @@ class User < Principal
def self.find_by_login(login) def self.find_by_login(login)
# force string comparison to be case sensitive on MySQL # force string comparison to be case sensitive on MySQL
type_cast = (ActiveRecord::Base.connection.adapter_name == 'MySQL') ? 'BINARY' : '' type_cast = (ActiveRecord::Base.connection.adapter_name == 'MySQL') ? 'BINARY' : ''
# First look for an exact match # First look for an exact match
user = first(:conditions => ["#{type_cast} login = ?", login]) user = first(:conditions => ["#{type_cast} login = ?", login])
# Fail over to case-insensitive if none was found # Fail over to case-insensitive if none was found
@ -312,21 +312,21 @@ class User < Principal
token = Token.find_by_value(key) token = Token.find_by_value(key)
token && token.user.active? ? token.user : nil token && token.user.active? ? token.user : nil
end end
def self.find_by_api_key(key) def self.find_by_api_key(key)
token = Token.find_by_action_and_value('api', key) token = Token.find_by_action_and_value('api', key)
token && token.user.active? ? token.user : nil token && token.user.active? ? token.user : nil
end end
# Makes find_by_mail case-insensitive # Makes find_by_mail case-insensitive
def self.find_by_mail(mail) def self.find_by_mail(mail)
find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase]) find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase])
end end
def to_s def to_s
name name
end end
# Returns the current day according to user's time zone # Returns the current day according to user's time zone
def today def today
if time_zone.nil? if time_zone.nil?
@ -335,15 +335,15 @@ class User < Principal
Time.now.in_time_zone(time_zone).to_date Time.now.in_time_zone(time_zone).to_date
end end
end end
def logged? def logged?
true true
end end
def anonymous? def anonymous?
!logged? !logged?
end end
# Return user's roles for project # Return user's roles for project
def roles_for_project(project) def roles_for_project(project)
roles = [] roles = []
@ -364,16 +364,16 @@ class User < Principal
end end
roles roles
end end
# Return true if the user is a member of project # Return true if the user is a member of project
def member_of?(project) def member_of?(project)
!roles_for_project(project).detect {|role| role.member?}.nil? !roles_for_project(project).detect {|role| role.member?}.nil?
end end
# Returns a hash of user's projects grouped by roles # Returns a hash of user's projects grouped by roles
def projects_by_role def projects_by_role
return @projects_by_role if @projects_by_role return @projects_by_role if @projects_by_role
@projects_by_role = Hash.new {|h,k| h[k]=[]} @projects_by_role = Hash.new {|h,k| h[k]=[]}
memberships.each do |membership| memberships.each do |membership|
membership.roles.each do |role| membership.roles.each do |role|
@ -383,10 +383,10 @@ class User < Principal
@projects_by_role.each do |role, projects| @projects_by_role.each do |role, projects|
projects.uniq! projects.uniq!
end end
@projects_by_role @projects_by_role
end end
# Returns true if user is arg or belongs to arg # Returns true if user is arg or belongs to arg
def is_or_belongs_to?(arg) def is_or_belongs_to?(arg)
if arg.is_a?(User) if arg.is_a?(User)
@ -397,7 +397,7 @@ class User < Principal
false false
end end
end end
# Return true if the user is allowed to do the specified action on a specific context # Return true if the user is allowed to do the specified action on a specific context
# Action can be: # Action can be:
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
@ -405,7 +405,7 @@ class User < Principal
# Context can be: # Context can be:
# * a project : returns true if user is allowed to do the specified action on this project # * a project : returns true if user is allowed to do the specified action on this project
# * an array of projects : returns true if user is allowed on every project # * an array of projects : returns true if user is allowed on every project
# * nil with options[:global] set : check if user has at least one role allowed for this action, # * nil with options[:global] set : check if user has at least one role allowed for this action,
# or falls back to Non Member / Anonymous permissions depending if the user is logged # or falls back to Non Member / Anonymous permissions depending if the user is logged
def allowed_to?(action, context, options={}, &block) def allowed_to?(action, context, options={}, &block)
if context && context.is_a?(Project) if context && context.is_a?(Project)
@ -415,7 +415,7 @@ class User < Principal
return false unless context.allows_to?(action) return false unless context.allows_to?(action)
# Admin users are authorized for anything else # Admin users are authorized for anything else
return true if admin? return true if admin?
roles = roles_for_project(context) roles = roles_for_project(context)
return false unless roles return false unless roles
roles.detect {|role| roles.detect {|role|
@ -433,7 +433,7 @@ class User < Principal
elsif options[:global] elsif options[:global]
# Admin users are always authorized # Admin users are always authorized
return true if admin? return true if admin?
# authorize if user has at least one role that has this permission # authorize if user has at least one role that has this permission
roles = memberships.collect {|m| m.roles}.flatten.uniq roles = memberships.collect {|m| m.roles}.flatten.uniq
roles << (self.logged? ? Role.non_member : Role.anonymous) roles << (self.logged? ? Role.non_member : Role.anonymous)
@ -461,14 +461,14 @@ class User < Principal
'custom_field_values', 'custom_field_values',
'custom_fields', 'custom_fields',
'identity_url' 'identity_url'
safe_attributes 'status', safe_attributes 'status',
'auth_source_id', 'auth_source_id',
:if => lambda {|user, current_user| current_user.admin?} :if => lambda {|user, current_user| current_user.admin?}
safe_attributes 'group_ids', safe_attributes 'group_ids',
:if => lambda {|user, current_user| current_user.admin? && !user.new_record?} :if => lambda {|user, current_user| current_user.admin? && !user.new_record?}
# Utility method to help check if a user should be notified about an # Utility method to help check if a user should be notified about an
# event. # event.
# #
@ -508,15 +508,15 @@ class User < Principal
false false
end end
end end
def self.current=(user) def self.current=(user)
@current_user = user @current_user = user
end end
def self.current def self.current
@current_user ||= User.anonymous @current_user ||= User.anonymous
end end
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
# one anonymous user per database. # one anonymous user per database.
def self.anonymous def self.anonymous
@ -541,23 +541,23 @@ class User < Principal
end end
end end
end end
protected protected
def validate def validate
# Password length validation based on setting # Password length validation based on setting
if !password.nil? && password.size < Setting.password_min_length.to_i if !password.nil? && password.size < Setting.password_min_length.to_i
errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) errors.add(:password, :too_short, :count => Setting.password_min_length.to_i)
end end
end end
private private
# Removes references that are not handled by associations # Removes references that are not handled by associations
# Things that are not deleted are reassociated with the anonymous user # Things that are not deleted are reassociated with the anonymous user
def remove_references_before_destroy def remove_references_before_destroy
return if self.id.nil? return if self.id.nil?
substitute = User.anonymous substitute = User.anonymous
Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
@ -577,30 +577,30 @@ class User < Principal
WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id]
end end
# Return password digest # Return password digest
def self.hash_password(clear_password) def self.hash_password(clear_password)
Digest::SHA1.hexdigest(clear_password || "") Digest::SHA1.hexdigest(clear_password || "")
end end
# Returns a 128bits random salt as a hex string (32 chars long) # Returns a 128bits random salt as a hex string (32 chars long)
def self.generate_salt def self.generate_salt
ActiveSupport::SecureRandom.hex(16) ActiveSupport::SecureRandom.hex(16)
end end
end end
class AnonymousUser < User class AnonymousUser < User
def validate_on_create def validate_on_create
# There should be only one AnonymousUser in the database # There should be only one AnonymousUser in the database
errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first) errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first)
end end
def available_custom_fields def available_custom_fields
[] []
end end
# Overrides a few properties # Overrides a few properties
def logged?; false end def logged?; false end
def admin; false end def admin; false end
@ -608,7 +608,7 @@ class AnonymousUser < User
def mail; nil end def mail; nil end
def time_zone; nil end def time_zone; nil end
def rss_key; nil end def rss_key; nil end
# Anonymous user can not be destroyed # Anonymous user can not be destroyed
def destroy def destroy
false false