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