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