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