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