##// END OF EJS Templates
Fixed that rss key is generated twice when user is not reloaded (#10668)....
Fixed that rss key is generated twice when user is not reloaded (#10668). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9419 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r9285:24c361eb0a12
r9285:24c361eb0a12
Show More
user.rb
657 lines | 21.6 KiB | text/x-ruby | RubyLexer
Jean-Philippe Lang
User groups branch merged....
r2755 # Redmine - project management software
Jean-Philippe Lang
Changed the way the visibility SQL statement is built....
r5020 # Copyright (C) 2006-2011 Jean-Philippe Lang
Jean-Philippe Lang
added svn:eol-style native property on /app files...
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.
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387 #
Jean-Philippe Lang
added svn:eol-style native property on /app files...
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.
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387 #
Jean-Philippe Lang
added svn:eol-style native property on /app files...
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"
Jean-Philippe Lang
User groups branch merged....
r2755 class User < Principal
Jean-Philippe Lang
Declare safe attributes for User and Projects models....
r4378 include Redmine::SafeAttributes
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
User's projects alphabetically sorted in the Projects drop down menu....
r535 # Account statuses
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 STATUS_ANONYMOUS = 0
Jean-Philippe Lang
User's projects alphabetically sorted in the Projects drop down menu....
r535 STATUS_ACTIVE = 1
STATUS_REGISTERED = 2
STATUS_LOCKED = 3
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Sort the issue list by author/assignee according to user display format (#9669)....
r7818 # Different ways of displaying/sorting users
Jean-Philippe Lang
User display format is now configurable in administration settings....
r1089 USER_FORMATS = {
Jean-Philippe Lang
Sort the issue list by author/assignee according to user display format (#9669)....
r7818 :firstname_lastname => {:string => '#{firstname} #{lastname}', :order => %w(firstname lastname id)},
:firstname => {:string => '#{firstname}', :order => %w(firstname id)},
:lastname_firstname => {:string => '#{lastname} #{firstname}', :order => %w(lastname firstname id)},
:lastname_coma_firstname => {:string => '#{lastname}, #{firstname}', :order => %w(lastname firstname id)},
:username => {:string => '#{login}', :order => %w(login id)},
Jean-Philippe Lang
User display format is now configurable in administration settings....
r1089 }
Jean-Philippe Lang
User's projects alphabetically sorted in the Projects drop down menu....
r535
Eric Davis
Converted User#mail_notification from a boolean to a string....
r4102 MAIL_NOTIFICATION_OPTIONS = [
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
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]
]
Eric Davis
Converted User#mail_notification from a boolean to a string....
r4102
Jean-Philippe Lang
User groups branch merged....
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)}
Jean-Philippe Lang
Maps repository users to Redmine users (#1383)....
r2004 has_many :changesets, :dependent => :nullify
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
Jean-Philippe Lang
When destroying a user, remove all references to that user (#7296)....
r4606 has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'"
has_one :api_token, :class_name => 'Token', :conditions => "action='api'"
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 belongs_to :auth_source
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Replaces User.find_active with a named scope....
r2077 # Active non-anonymous users scope
named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
Jean-Philippe Lang
Adds named scopes for users index....
r7961 named_scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Custom fields refactoring: most of code moved from controllers to models (using new module ActsAsCustomizable)....
r1578 acts_as_customizable
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 attr_accessor :password, :password_confirmation
attr_accessor :last_before_login_on
# Prevents unauthorized assignments
Jean-Philippe Lang
Extracts user groups assignment from controller....
r4385 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
Jean-Philippe Lang
Increase username length limit from 30 to 60 (#2719)....
r8658
LOGIN_LENGTH_LIMIT = 60
MAIL_LENGTH_LIMIT = 60
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
Eric Davis
Change User#login to be case-insensitive. #2473...
r3693 validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false
Jean-Philippe Lang
Makes email adress uniqueness case-insensitive (#2473)....
r2251 validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # Login must contain lettres, numbers, underscores only
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i
Jean-Philippe Lang
Increase username length limit from 30 to 60 (#2719)....
r8658 validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT
Jean-Philippe Lang
Added some attributes length validations....
r397 validates_length_of :firstname, :lastname, :maximum => 30
Jean-Philippe Lang
Do not show 2 validation errors when user mail is blank....
r6048 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_blank => true
Jean-Philippe Lang
Increase username length limit from 30 to 60 (#2719)....
r8658 validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 validates_confirmation_of :password, :allow_nil => true
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
r4380 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
Toshi MARUYAMA
Rails3: model: replace deprecated 'validate' method at User model...
r7311 validate :validate_password_length
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330
Toshi MARUYAMA
Rails3: replace deprecated 'before_create' method at User model....
r6723 before_create :set_mail_notification
Toshi MARUYAMA
Rails3: replace deprecated 'before_save' method at User model....
r6803 before_save :update_hashed_password
Jean-Philippe Lang
When destroying a user, remove all references to that user (#7296)....
r4606 before_destroy :remove_references_before_destroy
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Adds a Group filter on the admin users list (#7893)....
r5030 named_scope :in_group, lambda {|group|
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] }
}
Jean-Philippe Lang
Fixed: list of users for adding to a group may be empty if 100 first users have been added (#8029)....
r5164 named_scope :not_in_group, lambda {|group|
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] }
}
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Toshi MARUYAMA
Rails3: replace deprecated 'before_create' method at User model....
r6723 def set_mail_notification
Eric Davis
Allow admins to edit user's email notifications and preferences. #3503...
r4109 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
Jean-Philippe Lang
More flexible mail notifications settings at user level. A user has now 3 options:...
r842 true
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Toshi MARUYAMA
Rails3: replace deprecated 'before_save' method at User model....
r6803 def update_hashed_password
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # update hashed_password if password was set
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
r4816 if self.password && self.auth_source_id.blank?
salt_password(password)
end
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Sort users by their display names so that user dropdown lists are sorted alphabetically (#2015)....
r2008 def reload(*args)
@name = nil
Jean-Philippe Lang
Changed the way the visibility SQL statement is built....
r5020 @projects_by_role = nil
Jean-Philippe Lang
Sort users by their display names so that user dropdown lists are sorted alphabetically (#2015)....
r2008 super
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Strips user email (#5834)....
r3759 def mail=(arg)
write_attribute(:mail, arg.to_s.strip)
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Eric Davis
Normalize the identity_url when it's set....
r2392 def identity_url=(url)
Jean-Philippe Lang
Fixed: User#identity_url raises an error when invalid url is supplied (#2742)....
r2415 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
Eric Davis
Normalize the identity_url when it's set....
r2392 end
self.read_attribute(:identity_url)
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # Returns the user that matches provided login and password, or nil
def self.try_to_login(login, password)
Jean-Philippe Lang
Prevent LDAP authentication with empty password related problems....
r1217 # Make sure no one can sign in with an empty password
return nil if password.to_s.empty?
Jean-Philippe Lang
Fixes r9029....
r8910 user = find_by_login(login)
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 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
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
r4816 return nil unless user.check_password?(password)
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
else
# user is not yet registered, try to authenticate with available sources
attrs = AuthSource.authenticate(login, password)
if attrs
Jean-Philippe Lang
Makes AuthSource.authenticate return a hash instead of an array....
r3378 user = new(attrs)
Jean-Philippe Lang
Improved on-the-fly account creation. If some attributes are missing (eg. not present in the LDAP) or are invalid, the registration form is displayed so that the user is able to fill or fix these attributes....
r1661 user.login = login
user.language = Setting.default_language
if user.save
user.reload
Eric Davis
Allow AuthSources to control if they allow password changes....
r3631 logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387 end
Jean-Philippe Lang
Improved on-the-fly account creation. If some attributes are missing (eg. not present in the LDAP) or are invalid, the registration form is displayed so that the user is able to fill or fix these attributes....
r1661 user.update_attribute(:last_login_on, Time.now) if user && !user.new_record?
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 user
Jean-Philippe Lang
Better error message and AR errors in log for failed LDAP on-the-fly user creation (closes #932, #1042)....
r1330 rescue => text
raise text
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Fixed: When logging in via an autologin cookie the user's last_login_on should be updated (#2820)....
r2460 # Returns the user who matches the given autologin +key+ or nil
def self.try_to_autologin(key)
Jean-Philippe Lang
Do not autologin if more that one token is found (#3351)....
r2643 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
Jean-Philippe Lang
Fixed: When logging in via an autologin cookie the user's last_login_on should be updated (#2820)....
r2460 end
end
Jean-Philippe Lang
Sort the issue list by author/assignee according to user display format (#9669)....
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
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # Return user's full name for display
Jean-Philippe Lang
User display format is now configurable in administration settings....
r1089 def name(formatter = nil)
Jean-Philippe Lang
Sort the issue list by author/assignee according to user display format (#9669)....
r7818 f = self.class.name_formatter(formatter)
Jean-Philippe Lang
Fixes #2170: user display format in application settings broken by r2010....
r2029 if formatter
Jean-Philippe Lang
Sort the issue list by author/assignee according to user display format (#9669)....
r7818 eval('"' + f[:string] + '"')
Jean-Philippe Lang
Fixes #2170: user display format in application settings broken by r2010....
r2029 else
Jean-Philippe Lang
Sort the issue list by author/assignee according to user display format (#9669)....
r7818 @name ||= eval('"' + f[:string] + '"')
Jean-Philippe Lang
Fixes #2170: user display format in application settings broken by r2010....
r2029 end
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 def active?
self.status == STATUS_ACTIVE
end
def registered?
self.status == STATUS_REGISTERED
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 def locked?
self.status == STATUS_LOCKED
end
Eric Davis
Refactor: Add methods to User to edit the encapsulate the status field....
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
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
r4816 # Returns true if +clear_password+ is the correct user's password, otherwise false
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 def check_password?(clear_password)
Eric Davis
Allow AuthSources to control if they allow password changes....
r3631 if auth_source_id.present?
auth_source.authenticate(self.login, clear_password)
else
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
r4816 User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password
Eric Davis
Allow AuthSources to control if they allow password changes....
r3631 end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
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
Eric Davis
Allow AuthSources to control if they allow password changes....
r3631
# Does the backend storage allow this user to change their password?
def change_password_allowed?
Jean-Philippe Lang
Use validation callback....
r8173 return true if auth_source.nil?
Eric Davis
Allow AuthSources to control if they allow password changes....
r3631 return auth_source.allow_password_changes?
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Eric Davis
Hooked up on the fly OpenID user creation....
r2382
# 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
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 def pref
self.preference ||= UserPreference.new(:user => self)
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
* Added time zone support: users can select their time zone on their account view....
r904 def time_zone
Jean-Philippe Lang
Rails 2.1.2 deprecations (#2332)....
r2132 @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone])
Jean-Philippe Lang
* Added time zone support: users can select their time zone on their account view....
r904 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Add a user preference to choose how comments/replies are displayed: in chronological or reverse chronological order (#589, #776)....
r1183 def wants_comments_in_reverse_order?
self.pref[:comments_sorting] == 'desc'
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 # Return user's RSS key (a 40 chars long string), used to access feeds
def rss_key
Jean-Philippe Lang
Fixed that rss key is generated twice when user is not reloaded (#10668)....
r9285 if rss_token.nil?
create_rss_token(:action => 'feeds')
end
rss_token.value
Jean-Philippe Lang
added rss/atom feeds at project levels for:...
r336 end
Eric Davis
Added an API token for each User to use when making API requests. (#3920)...
r3103
# Return user's API key (a 40 chars long string), used to access the API
def api_key
Jean-Philippe Lang
Fixed that rss key is generated twice when user is not reloaded (#10668)....
r9285 if api_token.nil?
create_api_token(:action => 'api')
end
api_token.value
Eric Davis
Added an API token for each User to use when making API requests. (#3920)...
r3103 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
More flexible mail notifications settings at user level. A user has now 3 options:...
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
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
More flexible mail notifications settings at user level. A user has now 3 options:...
r842 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
Eric Davis
Refactor and documentation for User#find_by_login....
r3694
Eric Davis
Refactor: move method to model...
r4110 def valid_notification_options
Jean-Philippe Lang
Do not show "for only project I select" notification option on application settings form (#7294)....
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)
Eric Davis
Refactor: move method to model...
r4110 # Note that @user.membership.size would fail since AR ignores
# :include association option when doing a count
Jean-Philippe Lang
Do not show "for only project I select" notification option on application settings form (#7294)....
r4610 if user.nil? || user.memberships.length < 1
Jean-Philippe Lang
Fixed: "Notifiy for only project I select" is randomly displayed (#7294)....
r4607 MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'}
Eric Davis
Refactor: move method to model...
r4110 else
MAIL_NOTIFICATION_OPTIONS
end
end
Eric Davis
Refactor and documentation for User#find_by_login....
r3694 # Find a user account by matching the exact login and then a case-insensitive
# version. Exact matches will be given priority.
Eric Davis
Change User#login to be case-insensitive. #2473...
r3693 def self.find_by_login(login)
# First look for an exact match
Jean-Philippe Lang
Fixes r9029....
r8910 user = all(:conditions => {:login => login}).detect {|u| u.login == login}
unless user
# Fail over to case-insensitive if none was found
user = first(:conditions => ["LOWER(login) = ?", login.to_s.downcase])
end
user
Eric Davis
Change User#login to be case-insensitive. #2473...
r3693 end
Jean-Philippe Lang
added rss/atom feeds at project levels for:...
r336 def self.find_by_rss_key(key)
token = Token.find_by_value(key)
token && token.user.active? ? token.user : nil
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Eric Davis
Added an API token for each User to use when making API requests. (#3920)...
r3103 def self.find_by_api_key(key)
token = Token.find_by_action_and_value('api', key)
token && token.user.active? ? token.user : nil
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Makes User.find_by_mail case-insensitive (password reminder #2322, repo users mapping)....
r2120 # Makes find_by_mail case-insensitive
def self.find_by_mail(mail)
find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase])
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Fixed that "Default administrator account changed" is always true (#10622)....
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
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 def to_s
name
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Fixed: Pre-filled time tracking date ignores timezone (#4160)....
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
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 def logged?
true
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Adds links to the user page on various views....
r1657 def anonymous?
!logged?
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 # Return user's roles for project
def roles_for_project(project)
roles = []
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 # No role on archived projects
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 return roles unless project && project.active?
Jean-Philippe Lang
Slight optimization in User#role_for_project....
r915 if logged?
# Find project membership
membership = memberships.detect {|m| m.project_id == project.id}
if membership
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 roles = membership.roles
Jean-Philippe Lang
Slight optimization in User#role_for_project....
r915 else
@role_non_member ||= Role.non_member
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 roles << @role_non_member
Jean-Philippe Lang
Slight optimization in User#role_for_project....
r915 end
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 else
Jean-Philippe Lang
Slight optimization in User#role_for_project....
r915 @role_anonymous ||= Role.anonymous
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 roles << @role_anonymous
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 end
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 roles
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 # Return true if the user is a member of project
def member_of?(project)
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 !roles_for_project(project).detect {|role| role.member?}.nil?
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Changed the way the visibility SQL statement is built....
r5020 # Returns a hash of user's projects grouped by roles
def projects_by_role
return @projects_by_role if @projects_by_role
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Changed the way the visibility SQL statement is built....
r5020 @projects_by_role = Hash.new {|h,k| h[k]=[]}
memberships.each do |membership|
membership.roles.each do |role|
@projects_by_role[role] << membership.project if membership.project
end
end
@projects_by_role.each do |role, projects|
projects.uniq!
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Changed the way the visibility SQL statement is built....
r5020 @projects_by_role
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Ability to assign issues to groups (#2964)....
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
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Baptiste Barth
Added ability to specify multiple projects in User#allowed_to? (#5332)...
r4113 # Return true if the user is allowed to do the specified action on a specific context
# Action can be:
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
# * a permission Symbol (eg. :edit_project)
Jean-Baptiste Barth
Added ability to specify multiple projects in User#allowed_to? (#5332)...
r4113 # Context can be:
# * a project : returns true if user is allowed to do the specified action on this project
Jean-Philippe Lang
Adds an issues visibility level on roles (#7412)....
r5296 # * an array of projects : returns true if user is allowed on every project
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387 # * nil with options[:global] set : check if user has at least one role allowed for this action,
Jean-Baptiste Barth
Added ability to specify multiple projects in User#allowed_to? (#5332)...
r4113 # or falls back to Non Member / Anonymous permissions depending if the user is logged
Jean-Philippe Lang
Adds an issues visibility level on roles (#7412)....
r5296 def allowed_to?(action, context, options={}, &block)
Jean-Baptiste Barth
Code cleanup: renamed variables in User#allowed_to? with explicit names...
r4120 if context && context.is_a?(Project)
Jean-Philippe Lang
Global queries can be saved from the global issue list (follows r1311 and closes #897)....
r1297 # No action allowed on archived projects
Jean-Baptiste Barth
Code cleanup: renamed variables in User#allowed_to? with explicit names...
r4120 return false unless context.active?
Jean-Philippe Lang
Global queries can be saved from the global issue list (follows r1311 and closes #897)....
r1297 # No action allowed on disabled modules
Jean-Baptiste Barth
Code cleanup: renamed variables in User#allowed_to? with explicit names...
r4120 return false unless context.allows_to?(action)
Jean-Philippe Lang
Global queries can be saved from the global issue list (follows r1311 and closes #897)....
r1297 # Admin users are authorized for anything else
return true if admin?
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Baptiste Barth
Code cleanup: renamed variables in User#allowed_to? with explicit names...
r4120 roles = roles_for_project(context)
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 return false unless roles
Jean-Philippe Lang
Adds an issues visibility level on roles (#7412)....
r5296 roles.detect {|role|
(context.is_public? || role.member?) &&
role.allowed_to?(action) &&
(block_given? ? yield(role, self) : true)
}
Jean-Baptiste Barth
Code cleanup: renamed variables in User#allowed_to? with explicit names...
r4120 elsif context && context.is_a?(Array)
Jean-Baptiste Barth
Added ability to specify multiple projects in User#allowed_to? (#5332)...
r4113 # Authorize if user is authorized on every element of the array
Jean-Baptiste Barth
Code cleanup: renamed variables in User#allowed_to? with explicit names...
r4120 context.map do |project|
Jean-Philippe Lang
Adds an issues visibility level on roles (#7412)....
r5296 allowed_to?(action, project, options, &block)
Jean-Baptiste Barth
Code cleanup: renamed variables in User#allowed_to? with explicit names...
r4120 end.inject do |memo,allowed|
memo && allowed
Jean-Baptiste Barth
Added ability to specify multiple projects in User#allowed_to? (#5332)...
r4113 end
Jean-Philippe Lang
Global queries can be saved from the global issue list (follows r1311 and closes #897)....
r1297 elsif options[:global]
Jean-Philippe Lang
Ability to allow non-admin users to create projects (#1007)....
r2651 # Admin users are always authorized
return true if admin?
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Global queries can be saved from the global issue list (follows r1311 and closes #897)....
r1297 # authorize if user has at least one role that has this permission
Jean-Philippe Lang
Allows multiple roles on the same project (#706). Prerequisite for user groups feature....
r2627 roles = memberships.collect {|m| m.roles}.flatten.uniq
Jean-Philippe Lang
Adds an issues visibility level on roles (#7412)....
r5296 roles << (self.logged? ? Role.non_member : Role.anonymous)
roles.detect {|role|
role.allowed_to?(action) &&
(block_given? ? yield(role, self) : true)
}
Jean-Philippe Lang
Global queries can be saved from the global issue list (follows r1311 and closes #897)....
r1297 else
false
end
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 end
Eric Davis
Add User#allowed_to_globally? which wraps User#allowed_to?...
r4050
# Is the user allowed to do the specified action on any project?
# See allowed_to? for the actions and valid options.
Jean-Philippe Lang
Adds an issues visibility level on roles (#7412)....
r5296 def allowed_to_globally?(action, options, &block)
allowed_to?(action, nil, options.reverse_merge(:global => true), &block)
Eric Davis
Add User#allowed_to_globally? which wraps User#allowed_to?...
r4050 end
Jean-Philippe Lang
Declare safe attributes for User and Projects models....
r4378
Jean-Philippe Lang
Adds the ability for users to delete their own account (#10664). Can be disabled in application settings....
r9283 # Returns true if the user is allowed to delete his own account
def own_account_deletable?
Setting.unsubscribe? &&
(!admin? || User.active.first(:conditions => ["admin = ? AND id <> ?", true, id]).present?)
end
Jean-Philippe Lang
Declare safe attributes for User and Projects models....
r4378 safe_attributes 'login',
'firstname',
'lastname',
'mail',
'mail_notification',
'language',
'custom_field_values',
'custom_fields',
'identity_url'
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Declare safe attributes for User and Projects models....
r4378 safe_attributes 'status',
'auth_source_id',
:if => lambda {|user, current_user| current_user.admin?}
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Extracts user groups assignment from controller....
r4385 safe_attributes 'group_ids',
:if => lambda {|user, current_user| current_user.admin? && !user.new_record?}
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Eric Davis
Added User#notify_about? to check when a user should be notified about an event...
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)
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
r4380 case mail_notification
when 'all'
Eric Davis
Added User#notify_about? to check when a user should be notified about an event...
r4104 true
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
r4380 when 'selected'
Jean-Philippe Lang
Fixed: no email sent with 'Notifiy for any event on the selected projects only' (#7421)....
r4641 # user receives notifications for created/assigned issues on unselected projects
Jean-Philippe Lang
Notify previous assignee when assignee changes (#2694)....
r8575 if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was))
Jean-Philippe Lang
Fixed: no email sent with 'Notifiy for any event on the selected projects only' (#7421)....
r4641 true
else
false
end
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
r4380 when 'none'
Eric Davis
Added User#notify_about? to check when a user should be notified about an event...
r4104 false
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
r4380 when 'only_my_events'
Jean-Philippe Lang
Notify previous assignee when assignee changes (#2694)....
r8575 if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was))
Eric Davis
Added User#notify_about? to check when a user should be notified about an event...
r4104 true
else
false
end
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
r4380 when 'only_assigned'
Jean-Philippe Lang
Notify previous assignee when assignee changes (#2694)....
r8575 if object.is_a?(Issue) && (is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was))
Eric Davis
Added User#notify_about? to check when a user should be notified about an event...
r4104 true
else
false
end
Jean-Philippe Lang
Validates user's mail_notification and turn options into strings (the attribute type)....
r4380 when 'only_owner'
Eric Davis
Added User#notify_about? to check when a user should be notified about an event...
r4104 if object.is_a?(Issue) && object.author == self
true
else
false
end
else
false
end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 def self.current=(user)
@current_user = user
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 def self.current
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 @current_user ||= User.anonymous
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Eric Davis
Added some RDoc documentation for some models....
r2536 # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
# one anonymous user per database.
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 def self.anonymous
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 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
Jean-Philippe Lang
Fixed: non member or anonymous permissions change is effective only after an application restart (#1280)....
r1429 anonymous_user
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 end
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
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
User.find_each(:conditions => "salt IS NULL OR salt = ''") do |user|
next if user.hashed_password.blank?
salt = User.generate_salt
hashed_password = User.hash_password("#{salt}#{user.hashed_password}")
User.update_all("salt = '#{salt}', hashed_password = '#{hashed_password}'", ["id = ?", user.id] )
end
end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Makes minimum password length configurable....
r2586 protected
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Toshi MARUYAMA
Rails3: model: replace deprecated 'validate' method at User model...
r7311 def validate_password_length
Jean-Philippe Lang
Makes minimum password length configurable....
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
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Makes minimum password length configurable....
r2586 private
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
When destroying a user, remove all references to that user (#7296)....
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?
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
When destroying a user, remove all references to that user (#7296)....
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
Toshi MARUYAMA
Rails3: model: user: use ::Query instead of Query...
r8020 ::Query.delete_all ['user_id = ? AND is_public = ?', id, false]
::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id]
Jean-Philippe Lang
When destroying a user, remove all references to that user (#7296)....
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
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # Return password digest
def self.hash_password(clear_password)
Digest::SHA1.hexdigest(clear_password || "")
Jean-Philippe Lang
v0.2.0...
r5 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
r4816 # Returns a 128bits random salt as a hex string (32 chars long)
def self.generate_salt
Jean-Philippe Lang
Adds Redmine::Utils.random_hex for generating a random hex string....
r8951 Redmine::Utils.random_hex(16)
Jean-Philippe Lang
Adds random salt to user passwords (#7410)....
r4816 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Initial commit...
r2 end
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663
class AnonymousUser < User
Jean-Philippe Lang
Use validation callback....
r8173 validate :validate_anonymous_uniqueness, :on => :create
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb...
r8176
Jean-Philippe Lang
Use validation callback....
r8173 def validate_anonymous_uniqueness
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 # There should be only one AnonymousUser in the database
Toshi MARUYAMA
Rails3: model: replace deprecated errors.add_to_base at validate_on_create of User...
r7488 errors.add :base, 'An anonymous user already exists.' if AnonymousUser.find(:first)
Jean-Philippe Lang
* Added time zone support: users can select their time zone on their account view....
r904 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Custom fields refactoring: most of code moved from controllers to models (using new module ActsAsCustomizable)....
r1578 def available_custom_fields
[]
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 # Overrides a few properties
def logged?; false end
def admin; false end
Jean-Philippe Lang
Fixes ApplicationHelper#link_to_user...
r2910 def name(*args); I18n.t(:label_user_anonymous) end
Jean-Philippe Lang
Anonymous users can now be allowed to create, edit, comment issues, comment news and post messages in the forums....
r906 def mail; nil end
def time_zone; nil end
def rss_key; nil end
Toshi MARUYAMA
remove trailing white-spaces from app/models/user.rb....
r6387
Jean-Philippe Lang
When destroying a user, remove all references to that user (#7296)....
r4606 # Anonymous user can not be destroyed
def destroy
false
end
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 end