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 @ 440:6253d777aa12
History | View | Annotate | Download (15.7 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2009 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 |
|
| 22 |
# Account statuses
|
| 23 |
STATUS_ANONYMOUS = 0 |
| 24 |
STATUS_ACTIVE = 1 |
| 25 |
STATUS_REGISTERED = 2 |
| 26 |
STATUS_LOCKED = 3 |
| 27 |
|
| 28 |
USER_FORMATS = {
|
| 29 |
:firstname_lastname => '#{firstname} #{lastname}', |
| 30 |
:firstname => '#{firstname}', |
| 31 |
:lastname_firstname => '#{lastname} #{firstname}', |
| 32 |
:lastname_coma_firstname => '#{lastname}, #{firstname}', |
| 33 |
:username => '#{login}' |
| 34 |
} |
| 35 |
|
| 36 |
MAIL_NOTIFICATION_OPTIONS = [
|
| 37 |
[:all, :label_user_mail_option_all], |
| 38 |
[:selected, :label_user_mail_option_selected], |
| 39 |
[:none, :label_user_mail_option_none], |
| 40 |
[:only_my_events, :label_user_mail_option_only_my_events], |
| 41 |
[:only_assigned, :label_user_mail_option_only_assigned], |
| 42 |
[:only_owner, :label_user_mail_option_only_owner] |
| 43 |
] |
| 44 |
|
| 45 |
has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, |
| 46 |
:after_remove => Proc.new {|user, group| group.user_removed(user)} |
| 47 |
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
| 48 |
has_many :changesets, :dependent => :nullify |
| 49 |
has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' |
| 50 |
has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'" |
| 51 |
has_one :api_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='api'" |
| 52 |
belongs_to :auth_source
|
| 53 |
|
| 54 |
has_one :ssamr_user_detail, :dependent => :destroy, :class_name => 'SsamrUserDetail' |
| 55 |
accepts_nested_attributes_for :ssamr_user_detail
|
| 56 |
|
| 57 |
# Active non-anonymous users scope
|
| 58 |
named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}" |
| 59 |
|
| 60 |
acts_as_customizable |
| 61 |
|
| 62 |
attr_accessor :password, :password_confirmation |
| 63 |
attr_accessor :last_before_login_on
|
| 64 |
# Prevents unauthorized assignments
|
| 65 |
attr_protected :login, :admin, :password, :password_confirmation, :hashed_password, :group_ids |
| 66 |
|
| 67 |
validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } |
| 68 |
|
| 69 |
# TODO: is this validation correct validates_presence_of :ssamr_user_detail
|
| 70 |
|
| 71 |
validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false |
| 72 |
validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false |
| 73 |
# Login must contain lettres, numbers, underscores only
|
| 74 |
validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i |
| 75 |
validates_length_of :login, :maximum => 30 |
| 76 |
validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-\.]*$/i |
| 77 |
validates_length_of :firstname, :lastname, :maximum => 30 |
| 78 |
validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true |
| 79 |
validates_length_of :mail, :maximum => 60, :allow_nil => true |
| 80 |
validates_confirmation_of :password, :allow_nil => true |
| 81 |
|
| 82 |
validates_acceptance_of :terms_and_conditions, :on => :create, :message => :must_accept_terms_and_conditions |
| 83 |
|
| 84 |
def before_create |
| 85 |
self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? |
| 86 |
true
|
| 87 |
end
|
| 88 |
|
| 89 |
def before_save |
| 90 |
# update hashed_password if password was set
|
| 91 |
self.hashed_password = User.hash_password(self.password) if self.password && self.auth_source_id.blank? |
| 92 |
end
|
| 93 |
|
| 94 |
def reload(*args) |
| 95 |
@name = nil |
| 96 |
super
|
| 97 |
end
|
| 98 |
|
| 99 |
def mail=(arg) |
| 100 |
write_attribute(:mail, arg.to_s.strip)
|
| 101 |
end
|
| 102 |
|
| 103 |
def description=(arg) |
| 104 |
write_attribute(:description, arg.to_s.strip)
|
| 105 |
end
|
| 106 |
|
| 107 |
def identity_url=(url) |
| 108 |
if url.blank?
|
| 109 |
write_attribute(:identity_url, '') |
| 110 |
else
|
| 111 |
begin
|
| 112 |
write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) |
| 113 |
rescue OpenIdAuthentication::InvalidOpenId |
| 114 |
# Invlaid url, don't save
|
| 115 |
end
|
| 116 |
end
|
| 117 |
self.read_attribute(:identity_url) |
| 118 |
end
|
| 119 |
|
| 120 |
# Returns the user that matches provided login and password, or nil
|
| 121 |
def self.try_to_login(login, password) |
| 122 |
# Make sure no one can sign in with an empty password
|
| 123 |
return nil if password.to_s.empty? |
| 124 |
user = find_by_login(login) |
| 125 |
if user
|
| 126 |
# user is already in local database
|
| 127 |
return nil if !user.active? |
| 128 |
if user.auth_source
|
| 129 |
# user has an external authentication method
|
| 130 |
return nil unless user.auth_source.authenticate(login, password) |
| 131 |
else
|
| 132 |
# authentication with local password
|
| 133 |
return nil unless User.hash_password(password) == user.hashed_password |
| 134 |
end
|
| 135 |
else
|
| 136 |
# user is not yet registered, try to authenticate with available sources
|
| 137 |
attrs = AuthSource.authenticate(login, password)
|
| 138 |
if attrs
|
| 139 |
user = new(attrs) |
| 140 |
user.login = login |
| 141 |
user.language = Setting.default_language
|
| 142 |
if user.save
|
| 143 |
user.reload |
| 144 |
logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source |
| 145 |
end
|
| 146 |
end
|
| 147 |
end
|
| 148 |
user.update_attribute(:last_login_on, Time.now) if user && !user.new_record? |
| 149 |
user |
| 150 |
rescue => text
|
| 151 |
raise text |
| 152 |
end
|
| 153 |
|
| 154 |
# Returns the user who matches the given autologin +key+ or nil
|
| 155 |
def self.try_to_autologin(key) |
| 156 |
tokens = Token.find_all_by_action_and_value('autologin', key) |
| 157 |
# Make sure there's only 1 token that matches the key
|
| 158 |
if tokens.size == 1 |
| 159 |
token = tokens.first |
| 160 |
if (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active? |
| 161 |
token.user.update_attribute(:last_login_on, Time.now) |
| 162 |
token.user |
| 163 |
end
|
| 164 |
end
|
| 165 |
end
|
| 166 |
|
| 167 |
# Return user's full name for display
|
| 168 |
def name(formatter = nil) |
| 169 |
if formatter
|
| 170 |
eval('"' + (USER_FORMATS[formatter] || USER_FORMATS[:firstname_lastname]) + '"') |
| 171 |
else
|
| 172 |
@name ||= eval('"' + (USER_FORMATS[Setting.user_format] || USER_FORMATS[:firstname_lastname]) + '"') |
| 173 |
end
|
| 174 |
end
|
| 175 |
|
| 176 |
def active? |
| 177 |
self.status == STATUS_ACTIVE |
| 178 |
end
|
| 179 |
|
| 180 |
def registered? |
| 181 |
self.status == STATUS_REGISTERED |
| 182 |
end
|
| 183 |
|
| 184 |
def locked? |
| 185 |
self.status == STATUS_LOCKED |
| 186 |
end
|
| 187 |
|
| 188 |
def activate |
| 189 |
self.status = STATUS_ACTIVE |
| 190 |
end
|
| 191 |
|
| 192 |
def register |
| 193 |
self.status = STATUS_REGISTERED |
| 194 |
end
|
| 195 |
|
| 196 |
def lock |
| 197 |
self.status = STATUS_LOCKED |
| 198 |
end
|
| 199 |
|
| 200 |
def activate! |
| 201 |
update_attribute(:status, STATUS_ACTIVE) |
| 202 |
end
|
| 203 |
|
| 204 |
def register! |
| 205 |
update_attribute(:status, STATUS_REGISTERED) |
| 206 |
end
|
| 207 |
|
| 208 |
def lock! |
| 209 |
update_attribute(:status, STATUS_LOCKED) |
| 210 |
end
|
| 211 |
|
| 212 |
def check_password?(clear_password) |
| 213 |
if auth_source_id.present?
|
| 214 |
auth_source.authenticate(self.login, clear_password)
|
| 215 |
else
|
| 216 |
User.hash_password(clear_password) == self.hashed_password |
| 217 |
end
|
| 218 |
end
|
| 219 |
|
| 220 |
# Does the backend storage allow this user to change their password?
|
| 221 |
def change_password_allowed? |
| 222 |
return true if auth_source_id.blank? |
| 223 |
return auth_source.allow_password_changes?
|
| 224 |
end
|
| 225 |
|
| 226 |
# Generate and set a random password. Useful for automated user creation
|
| 227 |
# Based on Token#generate_token_value
|
| 228 |
#
|
| 229 |
def random_password |
| 230 |
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a |
| 231 |
password = ''
|
| 232 |
40.times { |i| password << chars[rand(chars.size-1)] } |
| 233 |
self.password = password
|
| 234 |
self.password_confirmation = password
|
| 235 |
self
|
| 236 |
end
|
| 237 |
|
| 238 |
def pref |
| 239 |
self.preference ||= UserPreference.new(:user => self) |
| 240 |
end
|
| 241 |
|
| 242 |
def time_zone |
| 243 |
@time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) |
| 244 |
end
|
| 245 |
|
| 246 |
def wants_comments_in_reverse_order? |
| 247 |
self.pref[:comments_sorting] == 'desc' |
| 248 |
end
|
| 249 |
|
| 250 |
# Return user's RSS key (a 40 chars long string), used to access feeds
|
| 251 |
def rss_key |
| 252 |
token = self.rss_token || Token.create(:user => self, :action => 'feeds') |
| 253 |
token.value |
| 254 |
end
|
| 255 |
|
| 256 |
# Return user's API key (a 40 chars long string), used to access the API
|
| 257 |
def api_key |
| 258 |
token = self.api_token || self.create_api_token(:action => 'api') |
| 259 |
token.value |
| 260 |
end
|
| 261 |
|
| 262 |
# Return an array of project ids for which the user has explicitly turned mail notifications on
|
| 263 |
def notified_projects_ids |
| 264 |
@notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) |
| 265 |
end
|
| 266 |
|
| 267 |
def notified_project_ids=(ids) |
| 268 |
Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) |
| 269 |
Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? |
| 270 |
@notified_projects_ids = nil |
| 271 |
notified_projects_ids |
| 272 |
end
|
| 273 |
|
| 274 |
# Only users that belong to more than 1 project can select projects for which they are notified
|
| 275 |
def valid_notification_options |
| 276 |
# Note that @user.membership.size would fail since AR ignores
|
| 277 |
# :include association option when doing a count
|
| 278 |
if memberships.length < 1 |
| 279 |
MAIL_NOTIFICATION_OPTIONS.delete_if {|option| option.first == :selected} |
| 280 |
else
|
| 281 |
MAIL_NOTIFICATION_OPTIONS
|
| 282 |
end
|
| 283 |
end
|
| 284 |
|
| 285 |
# Find a user account by matching the exact login and then a case-insensitive
|
| 286 |
# version. Exact matches will be given priority.
|
| 287 |
def self.find_by_login(login) |
| 288 |
# force string comparison to be case sensitive on MySQL
|
| 289 |
type_cast = (ActiveRecord::Base.connection.adapter_name == 'MySQL') ? 'BINARY' : '' |
| 290 |
|
| 291 |
# First look for an exact match
|
| 292 |
user = first(:conditions => ["#{type_cast} login = ?", login]) |
| 293 |
# Fail over to case-insensitive if none was found
|
| 294 |
user ||= first(:conditions => ["#{type_cast} LOWER(login) = ?", login.to_s.downcase]) |
| 295 |
end
|
| 296 |
|
| 297 |
def self.find_by_rss_key(key) |
| 298 |
token = Token.find_by_value(key)
|
| 299 |
token && token.user.active? ? token.user : nil
|
| 300 |
end
|
| 301 |
|
| 302 |
def self.find_by_api_key(key) |
| 303 |
token = Token.find_by_action_and_value('api', key) |
| 304 |
token && token.user.active? ? token.user : nil
|
| 305 |
end
|
| 306 |
|
| 307 |
# Makes find_by_mail case-insensitive
|
| 308 |
def self.find_by_mail(mail) |
| 309 |
find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase]) |
| 310 |
end
|
| 311 |
|
| 312 |
def to_s |
| 313 |
name |
| 314 |
end
|
| 315 |
|
| 316 |
# Returns the current day according to user's time zone
|
| 317 |
def today |
| 318 |
if time_zone.nil?
|
| 319 |
Date.today
|
| 320 |
else
|
| 321 |
Time.now.in_time_zone(time_zone).to_date
|
| 322 |
end
|
| 323 |
end
|
| 324 |
|
| 325 |
def logged? |
| 326 |
true
|
| 327 |
end
|
| 328 |
|
| 329 |
def anonymous? |
| 330 |
!logged? |
| 331 |
end
|
| 332 |
|
| 333 |
# Return user's roles for project
|
| 334 |
def roles_for_project(project) |
| 335 |
roles = [] |
| 336 |
# No role on archived projects
|
| 337 |
return roles unless project && project.active? |
| 338 |
if logged?
|
| 339 |
# Find project membership
|
| 340 |
membership = memberships.detect {|m| m.project_id == project.id}
|
| 341 |
if membership
|
| 342 |
roles = membership.roles |
| 343 |
else
|
| 344 |
@role_non_member ||= Role.non_member |
| 345 |
roles << @role_non_member
|
| 346 |
end
|
| 347 |
else
|
| 348 |
@role_anonymous ||= Role.anonymous |
| 349 |
roles << @role_anonymous
|
| 350 |
end
|
| 351 |
roles |
| 352 |
end
|
| 353 |
|
| 354 |
# Return true if the user is a member of project
|
| 355 |
def member_of?(project) |
| 356 |
!roles_for_project(project).detect {|role| role.member?}.nil?
|
| 357 |
end
|
| 358 |
|
| 359 |
# Return true if the user is allowed to do the specified action on a specific context
|
| 360 |
# Action can be:
|
| 361 |
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
|
| 362 |
# * a permission Symbol (eg. :edit_project)
|
| 363 |
# Context can be:
|
| 364 |
# * a project : returns true if user is allowed to do the specified action on this project
|
| 365 |
# * a group of projects : returns true if user is allowed on every project
|
| 366 |
# * nil with options[:global] set : check if user has at least one role allowed for this action,
|
| 367 |
# or falls back to Non Member / Anonymous permissions depending if the user is logged
|
| 368 |
def allowed_to?(action, context, options={}) |
| 369 |
if context && context.is_a?(Project) |
| 370 |
# No action allowed on archived projects
|
| 371 |
return false unless context.active? |
| 372 |
# No action allowed on disabled modules
|
| 373 |
return false unless context.allows_to?(action) |
| 374 |
# Admin users are authorized for anything else
|
| 375 |
return true if admin? |
| 376 |
|
| 377 |
roles = roles_for_project(context) |
| 378 |
return false unless roles |
| 379 |
roles.detect {|role| (context.is_public? || role.member?) && role.allowed_to?(action)}
|
| 380 |
|
| 381 |
elsif context && context.is_a?(Array) |
| 382 |
# Authorize if user is authorized on every element of the array
|
| 383 |
context.map do |project|
|
| 384 |
allowed_to?(action,project,options) |
| 385 |
end.inject do |memo,allowed| |
| 386 |
memo && allowed |
| 387 |
end
|
| 388 |
elsif options[:global] |
| 389 |
# Admin users are always authorized
|
| 390 |
return true if admin? |
| 391 |
|
| 392 |
# authorize if user has at least one role that has this permission
|
| 393 |
roles = memberships.collect {|m| m.roles}.flatten.uniq
|
| 394 |
roles.detect {|r| r.allowed_to?(action)} || (self.logged? ? Role.non_member.allowed_to?(action) : Role.anonymous.allowed_to?(action))
|
| 395 |
else
|
| 396 |
false
|
| 397 |
end
|
| 398 |
end
|
| 399 |
|
| 400 |
# Is the user allowed to do the specified action on any project?
|
| 401 |
# See allowed_to? for the actions and valid options.
|
| 402 |
def allowed_to_globally?(action, options) |
| 403 |
allowed_to?(action, nil, options.reverse_merge(:global => true)) |
| 404 |
end
|
| 405 |
|
| 406 |
# Utility method to help check if a user should be notified about an
|
| 407 |
# event.
|
| 408 |
#
|
| 409 |
# TODO: only supports Issue events currently
|
| 410 |
def notify_about?(object) |
| 411 |
case mail_notification.to_sym
|
| 412 |
when :all |
| 413 |
true
|
| 414 |
when :selected |
| 415 |
# Handled by the Project
|
| 416 |
when :none |
| 417 |
false
|
| 418 |
when :only_my_events |
| 419 |
if object.is_a?(Issue) && (object.author == self || object.assigned_to == self) |
| 420 |
true
|
| 421 |
else
|
| 422 |
false
|
| 423 |
end
|
| 424 |
when :only_assigned |
| 425 |
if object.is_a?(Issue) && object.assigned_to == self |
| 426 |
true
|
| 427 |
else
|
| 428 |
false
|
| 429 |
end
|
| 430 |
when :only_owner |
| 431 |
if object.is_a?(Issue) && object.author == self |
| 432 |
true
|
| 433 |
else
|
| 434 |
false
|
| 435 |
end
|
| 436 |
else
|
| 437 |
false
|
| 438 |
end
|
| 439 |
end
|
| 440 |
|
| 441 |
def self.current=(user) |
| 442 |
@current_user = user
|
| 443 |
end
|
| 444 |
|
| 445 |
def self.current |
| 446 |
@current_user ||= User.anonymous |
| 447 |
end
|
| 448 |
|
| 449 |
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
|
| 450 |
# one anonymous user per database.
|
| 451 |
def self.anonymous |
| 452 |
anonymous_user = AnonymousUser.find(:first) |
| 453 |
if anonymous_user.nil?
|
| 454 |
anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) |
| 455 |
raise 'Unable to create the anonymous user.' if anonymous_user.new_record? |
| 456 |
end
|
| 457 |
anonymous_user |
| 458 |
end
|
| 459 |
|
| 460 |
protected |
| 461 |
|
| 462 |
def validate |
| 463 |
# Password length validation based on setting
|
| 464 |
if !password.nil? && password.size < Setting.password_min_length.to_i |
| 465 |
errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) |
| 466 |
end
|
| 467 |
end
|
| 468 |
|
| 469 |
private |
| 470 |
|
| 471 |
# Return password digest
|
| 472 |
def self.hash_password(clear_password) |
| 473 |
Digest::SHA1.hexdigest(clear_password || "") |
| 474 |
end
|
| 475 |
end
|
| 476 |
|
| 477 |
class AnonymousUser < User |
| 478 |
|
| 479 |
def validate_on_create |
| 480 |
# There should be only one AnonymousUser in the database
|
| 481 |
errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first) |
| 482 |
end
|
| 483 |
|
| 484 |
def available_custom_fields |
| 485 |
[] |
| 486 |
end
|
| 487 |
|
| 488 |
# Overrides a few properties
|
| 489 |
def logged?; false end |
| 490 |
def admin; false end |
| 491 |
def name(*args); I18n.t(:label_user_anonymous) end |
| 492 |
def mail; nil end |
| 493 |
def time_zone; nil end |
| 494 |
def rss_key; nil end |
| 495 |
end
|