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