To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / user.rb @ 1361:7c0909052511
History | View | Annotate | Download (22.8 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
| 3 |
#
|
| 4 |
# This program is free software; you can redistribute it and/or
|
| 5 |
# modify it under the terms of the GNU General Public License
|
| 6 |
# as published by the Free Software Foundation; either version 2
|
| 7 |
# of the License, or (at your option) any later version.
|
| 8 |
#
|
| 9 |
# This program is distributed in the hope that it will be useful,
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
# GNU General Public License for more details.
|
| 13 |
#
|
| 14 |
# You should have received a copy of the GNU General Public License
|
| 15 |
# along with this program; if not, write to the Free Software
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 17 |
|
| 18 |
require "digest/sha1"
|
| 19 |
|
| 20 |
class User < Principal |
| 21 |
include Redmine::SafeAttributes |
| 22 |
|
| 23 |
# Account statuses
|
| 24 |
STATUS_ANONYMOUS = 0 |
| 25 |
STATUS_ACTIVE = 1 |
| 26 |
STATUS_REGISTERED = 2 |
| 27 |
STATUS_LOCKED = 3 |
| 28 |
|
| 29 |
# Different ways of displaying/sorting users
|
| 30 |
USER_FORMATS = {
|
| 31 |
:firstname_lastname => {
|
| 32 |
:string => '#{firstname} #{lastname}', |
| 33 |
:order => %w(firstname lastname id), |
| 34 |
:setting_order => 1 |
| 35 |
}, |
| 36 |
:firstname_lastinitial => {
|
| 37 |
:string => '#{firstname} #{lastname.to_s.chars.first}.', |
| 38 |
:order => %w(firstname lastname id), |
| 39 |
:setting_order => 2 |
| 40 |
}, |
| 41 |
:firstname => {
|
| 42 |
:string => '#{firstname}', |
| 43 |
:order => %w(firstname id), |
| 44 |
:setting_order => 3 |
| 45 |
}, |
| 46 |
:lastname_firstname => {
|
| 47 |
:string => '#{lastname} #{firstname}', |
| 48 |
:order => %w(lastname firstname id), |
| 49 |
:setting_order => 4 |
| 50 |
}, |
| 51 |
:lastname_coma_firstname => {
|
| 52 |
:string => '#{lastname}, #{firstname}', |
| 53 |
:order => %w(lastname firstname id), |
| 54 |
:setting_order => 5 |
| 55 |
}, |
| 56 |
:lastname => {
|
| 57 |
:string => '#{lastname}', |
| 58 |
:order => %w(lastname id), |
| 59 |
:setting_order => 6 |
| 60 |
}, |
| 61 |
:username => {
|
| 62 |
:string => '#{login}', |
| 63 |
:order => %w(login id), |
| 64 |
:setting_order => 7 |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
MAIL_NOTIFICATION_OPTIONS = [
|
| 69 |
['all', :label_user_mail_option_all], |
| 70 |
['selected', :label_user_mail_option_selected], |
| 71 |
['only_my_events', :label_user_mail_option_only_my_events], |
| 72 |
['only_assigned', :label_user_mail_option_only_assigned], |
| 73 |
['only_owner', :label_user_mail_option_only_owner], |
| 74 |
['none', :label_user_mail_option_none] |
| 75 |
] |
| 76 |
|
| 77 |
has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, |
| 78 |
:after_remove => Proc.new {|user, group| group.user_removed(user)} |
| 79 |
has_many :changesets, :dependent => :nullify |
| 80 |
has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' |
| 81 |
has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" |
| 82 |
has_one :api_token, :class_name => 'Token', :conditions => "action='api'" |
| 83 |
belongs_to :auth_source
|
| 84 |
|
| 85 |
scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}" |
| 86 |
scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} } |
| 87 |
|
| 88 |
has_one :ssamr_user_detail, :dependent => :destroy, :class_name => 'SsamrUserDetail' |
| 89 |
accepts_nested_attributes_for :ssamr_user_detail
|
| 90 |
|
| 91 |
has_one :author
|
| 92 |
|
| 93 |
acts_as_customizable |
| 94 |
|
| 95 |
attr_accessor :password, :password_confirmation |
| 96 |
attr_accessor :last_before_login_on
|
| 97 |
# Prevents unauthorized assignments
|
| 98 |
attr_protected :login, :admin, :password, :password_confirmation, :hashed_password |
| 99 |
|
| 100 |
LOGIN_LENGTH_LIMIT = 60 |
| 101 |
MAIL_LENGTH_LIMIT = 60 |
| 102 |
|
| 103 |
validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } |
| 104 |
validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, :case_sensitive => false |
| 105 |
validates_uniqueness_of :mail, :if => Proc.new { |user| user.mail_changed? && user.mail.present? }, :case_sensitive => false |
| 106 |
|
| 107 |
# Login must contain lettres, numbers, underscores only
|
| 108 |
validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i |
| 109 |
validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT |
| 110 |
validates_length_of :firstname, :lastname, :maximum => 30 |
| 111 |
validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_blank => true |
| 112 |
validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true |
| 113 |
validates_confirmation_of :password, :allow_nil => true |
| 114 |
validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true |
| 115 |
validate :validate_password_length
|
| 116 |
|
| 117 |
before_create :set_mail_notification
|
| 118 |
before_save :update_hashed_password
|
| 119 |
before_destroy :remove_references_before_destroy
|
| 120 |
|
| 121 |
validates_acceptance_of :terms_and_conditions, :on => :create, :message => :must_accept_terms_and_conditions |
| 122 |
|
| 123 |
scope :in_group, lambda {|group|
|
| 124 |
group_id = group.is_a?(Group) ? group.id : group.to_i
|
| 125 |
where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id)
|
| 126 |
} |
| 127 |
scope :not_in_group, lambda {|group|
|
| 128 |
group_id = group.is_a?(Group) ? group.id : group.to_i
|
| 129 |
where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id)
|
| 130 |
} |
| 131 |
|
| 132 |
def set_mail_notification |
| 133 |
self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? |
| 134 |
true
|
| 135 |
end
|
| 136 |
|
| 137 |
def update_hashed_password |
| 138 |
# update hashed_password if password was set
|
| 139 |
if self.password && self.auth_source_id.blank? |
| 140 |
salt_password(password) |
| 141 |
end
|
| 142 |
end
|
| 143 |
|
| 144 |
def reload(*args) |
| 145 |
@name = nil |
| 146 |
@projects_by_role = nil |
| 147 |
super
|
| 148 |
end
|
| 149 |
|
| 150 |
def mail=(arg) |
| 151 |
write_attribute(:mail, arg.to_s.strip)
|
| 152 |
end
|
| 153 |
|
| 154 |
def description=(arg) |
| 155 |
write_attribute(:description, arg.to_s.strip)
|
| 156 |
end
|
| 157 |
|
| 158 |
def identity_url=(url) |
| 159 |
if url.blank?
|
| 160 |
write_attribute(:identity_url, '') |
| 161 |
else
|
| 162 |
begin
|
| 163 |
write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) |
| 164 |
rescue OpenIdAuthentication::InvalidOpenId |
| 165 |
# Invlaid url, don't save
|
| 166 |
end
|
| 167 |
end
|
| 168 |
self.read_attribute(:identity_url) |
| 169 |
end
|
| 170 |
|
| 171 |
# Returns the user that matches provided login and password, or nil
|
| 172 |
def self.try_to_login(login, password) |
| 173 |
login = login.to_s |
| 174 |
password = password.to_s |
| 175 |
|
| 176 |
# Make sure no one can sign in with an empty password
|
| 177 |
return nil if password.empty? |
| 178 |
user = find_by_login(login) |
| 179 |
if user
|
| 180 |
# user is already in local database
|
| 181 |
return nil if !user.active? |
| 182 |
if user.auth_source
|
| 183 |
# user has an external authentication method
|
| 184 |
return nil unless user.auth_source.authenticate(login, password) |
| 185 |
else
|
| 186 |
# authentication with local password
|
| 187 |
return nil unless user.check_password?(password) |
| 188 |
end
|
| 189 |
else
|
| 190 |
# user is not yet registered, try to authenticate with available sources
|
| 191 |
attrs = AuthSource.authenticate(login, password)
|
| 192 |
if attrs
|
| 193 |
user = new(attrs) |
| 194 |
user.login = login |
| 195 |
user.language = Setting.default_language
|
| 196 |
if user.save
|
| 197 |
user.reload |
| 198 |
logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source |
| 199 |
end
|
| 200 |
end
|
| 201 |
end
|
| 202 |
user.update_attribute(:last_login_on, Time.now) if user && !user.new_record? |
| 203 |
user |
| 204 |
rescue => text
|
| 205 |
raise text |
| 206 |
end
|
| 207 |
|
| 208 |
# Returns the user who matches the given autologin +key+ or nil
|
| 209 |
def self.try_to_autologin(key) |
| 210 |
tokens = Token.find_all_by_action_and_value('autologin', key.to_s) |
| 211 |
# Make sure there's only 1 token that matches the key
|
| 212 |
if tokens.size == 1 |
| 213 |
token = tokens.first |
| 214 |
if (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active? |
| 215 |
token.user.update_attribute(:last_login_on, Time.now) |
| 216 |
token.user |
| 217 |
end
|
| 218 |
end
|
| 219 |
end
|
| 220 |
|
| 221 |
def self.name_formatter(formatter = nil) |
| 222 |
USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] |
| 223 |
end
|
| 224 |
|
| 225 |
# Returns an array of fields names than can be used to make an order statement for users
|
| 226 |
# according to how user names are displayed
|
| 227 |
# Examples:
|
| 228 |
#
|
| 229 |
# User.fields_for_order_statement => ['users.login', 'users.id']
|
| 230 |
# User.fields_for_order_statement('authors') => ['authors.login', 'authors.id']
|
| 231 |
def self.fields_for_order_statement(table=nil) |
| 232 |
table ||= table_name |
| 233 |
name_formatter[:order].map {|field| "#{table}.#{field}"} |
| 234 |
end
|
| 235 |
|
| 236 |
# Return user's full name for display
|
| 237 |
def name(formatter = nil) |
| 238 |
f = self.class.name_formatter(formatter)
|
| 239 |
if formatter
|
| 240 |
eval('"' + f[:string] + '"') |
| 241 |
else
|
| 242 |
@name ||= eval('"' + f[:string] + '"') |
| 243 |
end
|
| 244 |
end
|
| 245 |
|
| 246 |
def active? |
| 247 |
self.status == STATUS_ACTIVE |
| 248 |
end
|
| 249 |
|
| 250 |
def registered? |
| 251 |
self.status == STATUS_REGISTERED |
| 252 |
end
|
| 253 |
|
| 254 |
def locked? |
| 255 |
self.status == STATUS_LOCKED |
| 256 |
end
|
| 257 |
|
| 258 |
def activate |
| 259 |
self.status = STATUS_ACTIVE |
| 260 |
end
|
| 261 |
|
| 262 |
def register |
| 263 |
self.status = STATUS_REGISTERED |
| 264 |
end
|
| 265 |
|
| 266 |
def lock |
| 267 |
self.status = STATUS_LOCKED |
| 268 |
end
|
| 269 |
|
| 270 |
def activate! |
| 271 |
update_attribute(:status, STATUS_ACTIVE) |
| 272 |
end
|
| 273 |
|
| 274 |
def register! |
| 275 |
update_attribute(:status, STATUS_REGISTERED) |
| 276 |
end
|
| 277 |
|
| 278 |
def lock! |
| 279 |
update_attribute(:status, STATUS_LOCKED) |
| 280 |
end
|
| 281 |
|
| 282 |
# Returns true if +clear_password+ is the correct user's password, otherwise false
|
| 283 |
def check_password?(clear_password) |
| 284 |
if auth_source_id.present?
|
| 285 |
auth_source.authenticate(self.login, clear_password)
|
| 286 |
else
|
| 287 |
User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password |
| 288 |
end
|
| 289 |
end
|
| 290 |
|
| 291 |
# Generates a random salt and computes hashed_password for +clear_password+
|
| 292 |
# The hashed password is stored in the following form: SHA1(salt + SHA1(password))
|
| 293 |
def salt_password(clear_password) |
| 294 |
self.salt = User.generate_salt |
| 295 |
self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}") |
| 296 |
end
|
| 297 |
|
| 298 |
# Does the backend storage allow this user to change their password?
|
| 299 |
def change_password_allowed? |
| 300 |
return true if auth_source.nil? |
| 301 |
return auth_source.allow_password_changes?
|
| 302 |
end
|
| 303 |
|
| 304 |
# Generate and set a random password. Useful for automated user creation
|
| 305 |
# Based on Token#generate_token_value
|
| 306 |
#
|
| 307 |
def random_password |
| 308 |
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a |
| 309 |
password = ''
|
| 310 |
40.times { |i| password << chars[rand(chars.size-1)] } |
| 311 |
self.password = password
|
| 312 |
self.password_confirmation = password
|
| 313 |
self
|
| 314 |
end
|
| 315 |
|
| 316 |
def pref |
| 317 |
self.preference ||= UserPreference.new(:user => self) |
| 318 |
end
|
| 319 |
|
| 320 |
def time_zone |
| 321 |
@time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) |
| 322 |
end
|
| 323 |
|
| 324 |
def wants_comments_in_reverse_order? |
| 325 |
self.pref[:comments_sorting] == 'desc' |
| 326 |
end
|
| 327 |
|
| 328 |
# Return user's RSS key (a 40 chars long string), used to access feeds
|
| 329 |
def rss_key |
| 330 |
if rss_token.nil?
|
| 331 |
create_rss_token(:action => 'feeds') |
| 332 |
end
|
| 333 |
rss_token.value |
| 334 |
end
|
| 335 |
|
| 336 |
# Return user's API key (a 40 chars long string), used to access the API
|
| 337 |
def api_key |
| 338 |
if api_token.nil?
|
| 339 |
create_api_token(:action => 'api') |
| 340 |
end
|
| 341 |
api_token.value |
| 342 |
end
|
| 343 |
|
| 344 |
# Return an array of project ids for which the user has explicitly turned mail notifications on
|
| 345 |
def notified_projects_ids |
| 346 |
@notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) |
| 347 |
end
|
| 348 |
|
| 349 |
def notified_project_ids=(ids) |
| 350 |
Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) |
| 351 |
Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? |
| 352 |
@notified_projects_ids = nil |
| 353 |
notified_projects_ids |
| 354 |
end
|
| 355 |
|
| 356 |
def valid_notification_options |
| 357 |
self.class.valid_notification_options(self) |
| 358 |
end
|
| 359 |
|
| 360 |
# Only users that belong to more than 1 project can select projects for which they are notified
|
| 361 |
def self.valid_notification_options(user=nil) |
| 362 |
# Note that @user.membership.size would fail since AR ignores
|
| 363 |
# :include association option when doing a count
|
| 364 |
if user.nil? || user.memberships.length < 1 |
| 365 |
MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} |
| 366 |
else
|
| 367 |
MAIL_NOTIFICATION_OPTIONS
|
| 368 |
end
|
| 369 |
end
|
| 370 |
|
| 371 |
# Find a user account by matching the exact login and then a case-insensitive
|
| 372 |
# version. Exact matches will be given priority.
|
| 373 |
def self.find_by_login(login) |
| 374 |
# First look for an exact match
|
| 375 |
user = where(:login => login).all.detect {|u| u.login == login}
|
| 376 |
unless user
|
| 377 |
# Fail over to case-insensitive if none was found
|
| 378 |
user = where("LOWER(login) = ?", login.to_s.downcase).first
|
| 379 |
end
|
| 380 |
user |
| 381 |
end
|
| 382 |
|
| 383 |
def self.find_by_rss_key(key) |
| 384 |
token = Token.find_by_action_and_value('feeds', key.to_s) |
| 385 |
token && token.user.active? ? token.user : nil
|
| 386 |
end
|
| 387 |
|
| 388 |
def self.find_by_api_key(key) |
| 389 |
token = Token.find_by_action_and_value('api', key.to_s) |
| 390 |
token && token.user.active? ? token.user : nil
|
| 391 |
end
|
| 392 |
|
| 393 |
# Makes find_by_mail case-insensitive
|
| 394 |
def self.find_by_mail(mail) |
| 395 |
where("LOWER(mail) = ?", mail.to_s.downcase).first
|
| 396 |
end
|
| 397 |
|
| 398 |
# Returns true if the default admin account can no longer be used
|
| 399 |
def self.default_admin_account_changed? |
| 400 |
!User.active.find_by_login("admin").try(:check_password?, "admin") |
| 401 |
end
|
| 402 |
|
| 403 |
def to_s |
| 404 |
name |
| 405 |
end
|
| 406 |
|
| 407 |
CSS_CLASS_BY_STATUS = {
|
| 408 |
STATUS_ANONYMOUS => 'anon', |
| 409 |
STATUS_ACTIVE => 'active', |
| 410 |
STATUS_REGISTERED => 'registered', |
| 411 |
STATUS_LOCKED => 'locked' |
| 412 |
} |
| 413 |
|
| 414 |
def css_classes |
| 415 |
"user #{CSS_CLASS_BY_STATUS[status]}"
|
| 416 |
end
|
| 417 |
|
| 418 |
# Returns the current day according to user's time zone
|
| 419 |
def today |
| 420 |
if time_zone.nil?
|
| 421 |
Date.today
|
| 422 |
else
|
| 423 |
Time.now.in_time_zone(time_zone).to_date
|
| 424 |
end
|
| 425 |
end
|
| 426 |
|
| 427 |
# Returns the day of +time+ according to user's time zone
|
| 428 |
def time_to_date(time) |
| 429 |
if time_zone.nil?
|
| 430 |
time.to_date |
| 431 |
else
|
| 432 |
time.in_time_zone(time_zone).to_date |
| 433 |
end
|
| 434 |
end
|
| 435 |
|
| 436 |
def logged? |
| 437 |
true
|
| 438 |
end
|
| 439 |
|
| 440 |
def anonymous? |
| 441 |
!logged? |
| 442 |
end
|
| 443 |
|
| 444 |
# Return user's roles for project
|
| 445 |
def roles_for_project(project) |
| 446 |
roles = [] |
| 447 |
# No role on archived projects
|
| 448 |
return roles if project.nil? || project.archived? |
| 449 |
if logged?
|
| 450 |
# Find project membership
|
| 451 |
membership = memberships.detect {|m| m.project_id == project.id}
|
| 452 |
if membership
|
| 453 |
roles = membership.roles |
| 454 |
else
|
| 455 |
@role_non_member ||= Role.non_member |
| 456 |
roles << @role_non_member
|
| 457 |
end
|
| 458 |
else
|
| 459 |
@role_anonymous ||= Role.anonymous |
| 460 |
roles << @role_anonymous
|
| 461 |
end
|
| 462 |
roles |
| 463 |
end
|
| 464 |
|
| 465 |
# Return true if the user is a member of project
|
| 466 |
def member_of?(project) |
| 467 |
!roles_for_project(project).detect {|role| role.member?}.nil?
|
| 468 |
end
|
| 469 |
|
| 470 |
# Returns a hash of user's projects grouped by roles
|
| 471 |
def projects_by_role |
| 472 |
return @projects_by_role if @projects_by_role |
| 473 |
|
| 474 |
@projects_by_role = Hash.new([]) |
| 475 |
memberships.each do |membership|
|
| 476 |
if membership.project
|
| 477 |
membership.roles.each do |role|
|
| 478 |
@projects_by_role[role] = [] unless @projects_by_role.key?(role) |
| 479 |
@projects_by_role[role] << membership.project
|
| 480 |
end
|
| 481 |
end
|
| 482 |
end
|
| 483 |
@projects_by_role.each do |role, projects| |
| 484 |
projects.uniq! |
| 485 |
end
|
| 486 |
|
| 487 |
@projects_by_role
|
| 488 |
end
|
| 489 |
|
| 490 |
# Returns true if user is arg or belongs to arg
|
| 491 |
def is_or_belongs_to?(arg) |
| 492 |
if arg.is_a?(User) |
| 493 |
self == arg
|
| 494 |
elsif arg.is_a?(Group) |
| 495 |
arg.users.include?(self)
|
| 496 |
else
|
| 497 |
false
|
| 498 |
end
|
| 499 |
end
|
| 500 |
|
| 501 |
# Return true if the user is allowed to do the specified action on a specific context
|
| 502 |
# Action can be:
|
| 503 |
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
|
| 504 |
# * a permission Symbol (eg. :edit_project)
|
| 505 |
# Context can be:
|
| 506 |
# * a project : returns true if user is allowed to do the specified action on this project
|
| 507 |
# * an array of projects : returns true if user is allowed on every project
|
| 508 |
# * nil with options[:global] set : check if user has at least one role allowed for this action,
|
| 509 |
# or falls back to Non Member / Anonymous permissions depending if the user is logged
|
| 510 |
def allowed_to?(action, context, options={}, &block) |
| 511 |
if context && context.is_a?(Project) |
| 512 |
return false unless context.allows_to?(action) |
| 513 |
# Admin users are authorized for anything else
|
| 514 |
return true if admin? |
| 515 |
|
| 516 |
roles = roles_for_project(context) |
| 517 |
return false unless roles |
| 518 |
roles.any? {|role|
|
| 519 |
(context.is_public? || role.member?) && |
| 520 |
role.allowed_to?(action) && |
| 521 |
(block_given? ? yield(role, self) : true) |
| 522 |
} |
| 523 |
elsif context && context.is_a?(Array) |
| 524 |
if context.empty?
|
| 525 |
false
|
| 526 |
else
|
| 527 |
# Authorize if user is authorized on every element of the array
|
| 528 |
context.map {|project| allowed_to?(action, project, options, &block)}.reduce(:&)
|
| 529 |
end
|
| 530 |
elsif options[:global] |
| 531 |
# Admin users are always authorized
|
| 532 |
return true if admin? |
| 533 |
|
| 534 |
# authorize if user has at least one role that has this permission
|
| 535 |
roles = memberships.collect {|m| m.roles}.flatten.uniq
|
| 536 |
roles << (self.logged? ? Role.non_member : Role.anonymous) |
| 537 |
roles.any? {|role|
|
| 538 |
role.allowed_to?(action) && |
| 539 |
(block_given? ? yield(role, self) : true) |
| 540 |
} |
| 541 |
else
|
| 542 |
false
|
| 543 |
end
|
| 544 |
end
|
| 545 |
|
| 546 |
# Is the user allowed to do the specified action on any project?
|
| 547 |
# See allowed_to? for the actions and valid options.
|
| 548 |
def allowed_to_globally?(action, options, &block) |
| 549 |
allowed_to?(action, nil, options.reverse_merge(:global => true), &block) |
| 550 |
end
|
| 551 |
|
| 552 |
# Returns true if the user is allowed to delete his own account
|
| 553 |
def own_account_deletable? |
| 554 |
Setting.unsubscribe? &&
|
| 555 |
(!admin? || User.active.where("admin = ? AND id <> ?", true, id).exists?) |
| 556 |
end
|
| 557 |
|
| 558 |
safe_attributes 'login',
|
| 559 |
'firstname',
|
| 560 |
'lastname',
|
| 561 |
'mail',
|
| 562 |
'mail_notification',
|
| 563 |
'language',
|
| 564 |
'custom_field_values',
|
| 565 |
'custom_fields',
|
| 566 |
'identity_url'
|
| 567 |
|
| 568 |
safe_attributes 'status',
|
| 569 |
'auth_source_id',
|
| 570 |
:if => lambda {|user, current_user| current_user.admin?}
|
| 571 |
|
| 572 |
safe_attributes 'group_ids',
|
| 573 |
:if => lambda {|user, current_user| current_user.admin? && !user.new_record?}
|
| 574 |
|
| 575 |
# Utility method to help check if a user should be notified about an
|
| 576 |
# event.
|
| 577 |
#
|
| 578 |
# TODO: only supports Issue events currently
|
| 579 |
def notify_about?(object) |
| 580 |
case mail_notification
|
| 581 |
when 'all' |
| 582 |
true
|
| 583 |
when 'selected' |
| 584 |
# user receives notifications for created/assigned issues on unselected projects
|
| 585 |
if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was)) |
| 586 |
true
|
| 587 |
else
|
| 588 |
false
|
| 589 |
end
|
| 590 |
when 'none' |
| 591 |
false
|
| 592 |
when 'only_my_events' |
| 593 |
if object.is_a?(Issue) && (object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was)) |
| 594 |
true
|
| 595 |
else
|
| 596 |
false
|
| 597 |
end
|
| 598 |
when 'only_assigned' |
| 599 |
if object.is_a?(Issue) && (is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was)) |
| 600 |
true
|
| 601 |
else
|
| 602 |
false
|
| 603 |
end
|
| 604 |
when 'only_owner' |
| 605 |
if object.is_a?(Issue) && object.author == self |
| 606 |
true
|
| 607 |
else
|
| 608 |
false
|
| 609 |
end
|
| 610 |
else
|
| 611 |
false
|
| 612 |
end
|
| 613 |
end
|
| 614 |
|
| 615 |
def self.current=(user) |
| 616 |
@current_user = user
|
| 617 |
end
|
| 618 |
|
| 619 |
def self.current |
| 620 |
@current_user ||= User.anonymous |
| 621 |
end
|
| 622 |
|
| 623 |
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
|
| 624 |
# one anonymous user per database.
|
| 625 |
def self.anonymous |
| 626 |
anonymous_user = AnonymousUser.first
|
| 627 |
if anonymous_user.nil?
|
| 628 |
anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) |
| 629 |
raise 'Unable to create the anonymous user.' if anonymous_user.new_record? |
| 630 |
end
|
| 631 |
anonymous_user |
| 632 |
end
|
| 633 |
|
| 634 |
# Salts all existing unsalted passwords
|
| 635 |
# It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password))
|
| 636 |
# This method is used in the SaltPasswords migration and is to be kept as is
|
| 637 |
def self.salt_unsalted_passwords! |
| 638 |
transaction do
|
| 639 |
User.where("salt IS NULL OR salt = ''").find_each do |user| |
| 640 |
next if user.hashed_password.blank? |
| 641 |
salt = User.generate_salt
|
| 642 |
hashed_password = User.hash_password("#{salt}#{user.hashed_password}") |
| 643 |
User.where(:id => user.id).update_all(:salt => salt, :hashed_password => hashed_password) |
| 644 |
end
|
| 645 |
end
|
| 646 |
end
|
| 647 |
|
| 648 |
protected |
| 649 |
|
| 650 |
def validate_password_length |
| 651 |
# Password length validation based on setting
|
| 652 |
if !password.nil? && password.size < Setting.password_min_length.to_i |
| 653 |
errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) |
| 654 |
end
|
| 655 |
end
|
| 656 |
|
| 657 |
private |
| 658 |
|
| 659 |
# Removes references that are not handled by associations
|
| 660 |
# Things that are not deleted are reassociated with the anonymous user
|
| 661 |
def remove_references_before_destroy |
| 662 |
return if self.id.nil? |
| 663 |
|
| 664 |
substitute = User.anonymous
|
| 665 |
Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
| 666 |
Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
| 667 |
Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
| 668 |
Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] |
| 669 |
Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
| 670 |
JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] |
| 671 |
JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] |
| 672 |
Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
| 673 |
News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
| 674 |
# Remove private queries and keep public ones
|
| 675 |
::Query.delete_all ['user_id = ? AND is_public = ?', id, false] |
| 676 |
::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
| 677 |
TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
| 678 |
Token.delete_all ['user_id = ?', id] |
| 679 |
Watcher.delete_all ['user_id = ?', id] |
| 680 |
WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
| 681 |
WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
| 682 |
end
|
| 683 |
|
| 684 |
# Return password digest
|
| 685 |
def self.hash_password(clear_password) |
| 686 |
Digest::SHA1.hexdigest(clear_password || "") |
| 687 |
end
|
| 688 |
|
| 689 |
# Returns a 128bits random salt as a hex string (32 chars long)
|
| 690 |
def self.generate_salt |
| 691 |
Redmine::Utils.random_hex(16) |
| 692 |
end
|
| 693 |
|
| 694 |
end
|
| 695 |
|
| 696 |
class AnonymousUser < User |
| 697 |
validate :validate_anonymous_uniqueness, :on => :create |
| 698 |
|
| 699 |
def validate_anonymous_uniqueness |
| 700 |
# There should be only one AnonymousUser in the database
|
| 701 |
errors.add :base, 'An anonymous user already exists.' if AnonymousUser.find(:first) |
| 702 |
end
|
| 703 |
|
| 704 |
def available_custom_fields |
| 705 |
[] |
| 706 |
end
|
| 707 |
|
| 708 |
# Overrides a few properties
|
| 709 |
def logged?; false end |
| 710 |
def admin; false end |
| 711 |
def name(*args); I18n.t(:label_user_anonymous) end |
| 712 |
def mail; nil end |
| 713 |
def time_zone; nil end |
| 714 |
def rss_key; nil end |
| 715 |
|
| 716 |
def pref |
| 717 |
UserPreference.new(:user => self) |
| 718 |
end
|
| 719 |
|
| 720 |
# Anonymous user can not be destroyed
|
| 721 |
def destroy |
| 722 |
false
|
| 723 |
end
|
| 724 |
end
|