@@ -1,255 +1,256 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require "digest/sha1" |
|
19 | 19 | |
|
20 | 20 | class User < ActiveRecord::Base |
|
21 | 21 | # Account statuses |
|
22 | 22 | STATUS_ANONYMOUS = 0 |
|
23 | 23 | STATUS_ACTIVE = 1 |
|
24 | 24 | STATUS_REGISTERED = 2 |
|
25 | 25 | STATUS_LOCKED = 3 |
|
26 | 26 | |
|
27 | 27 | has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :order => "#{Project.table_name}.name", :dependent => :delete_all |
|
28 | 28 | has_many :projects, :through => :memberships |
|
29 | 29 | has_many :custom_values, :dependent => :delete_all, :as => :customized |
|
30 | 30 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
|
31 | 31 | has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' |
|
32 | 32 | has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'" |
|
33 | 33 | belongs_to :auth_source |
|
34 | 34 | |
|
35 | 35 | attr_accessor :password, :password_confirmation |
|
36 | 36 | attr_accessor :last_before_login_on |
|
37 | 37 | # Prevents unauthorized assignments |
|
38 | 38 | attr_protected :login, :admin, :password, :password_confirmation, :hashed_password |
|
39 | 39 | |
|
40 | 40 | validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } |
|
41 |
validates_uniqueness_of :login, : |
|
|
41 | validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? } | |
|
42 | validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? } | |
|
42 | 43 | # Login must contain lettres, numbers, underscores only |
|
43 | 44 | validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i |
|
44 | 45 | validates_length_of :login, :maximum => 30 |
|
45 | 46 | validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i |
|
46 | 47 | validates_length_of :firstname, :lastname, :maximum => 30 |
|
47 | 48 | validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true |
|
48 | 49 | validates_length_of :mail, :maximum => 60, :allow_nil => true |
|
49 | 50 | validates_length_of :password, :minimum => 4, :allow_nil => true |
|
50 | 51 | validates_confirmation_of :password, :allow_nil => true |
|
51 | 52 | validates_associated :custom_values, :on => :update |
|
52 | 53 | |
|
53 | 54 | def before_create |
|
54 | 55 | self.mail_notification = false |
|
55 | 56 | true |
|
56 | 57 | end |
|
57 | 58 | |
|
58 | 59 | def before_save |
|
59 | 60 | # update hashed_password if password was set |
|
60 | 61 | self.hashed_password = User.hash_password(self.password) if self.password |
|
61 | 62 | end |
|
62 | 63 | |
|
63 | 64 | def self.active |
|
64 | 65 | with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do |
|
65 | 66 | yield |
|
66 | 67 | end |
|
67 | 68 | end |
|
68 | 69 | |
|
69 | 70 | def self.find_active(*args) |
|
70 | 71 | active do |
|
71 | 72 | find(*args) |
|
72 | 73 | end |
|
73 | 74 | end |
|
74 | 75 | |
|
75 | 76 | # Returns the user that matches provided login and password, or nil |
|
76 | 77 | def self.try_to_login(login, password) |
|
77 | 78 | user = find(:first, :conditions => ["login=?", login]) |
|
78 | 79 | if user |
|
79 | 80 | # user is already in local database |
|
80 | 81 | return nil if !user.active? |
|
81 | 82 | if user.auth_source |
|
82 | 83 | # user has an external authentication method |
|
83 | 84 | return nil unless user.auth_source.authenticate(login, password) |
|
84 | 85 | else |
|
85 | 86 | # authentication with local password |
|
86 | 87 | return nil unless User.hash_password(password) == user.hashed_password |
|
87 | 88 | end |
|
88 | 89 | else |
|
89 | 90 | # user is not yet registered, try to authenticate with available sources |
|
90 | 91 | attrs = AuthSource.authenticate(login, password) |
|
91 | 92 | if attrs |
|
92 | 93 | onthefly = new(*attrs) |
|
93 | 94 | onthefly.login = login |
|
94 | 95 | onthefly.language = Setting.default_language |
|
95 | 96 | if onthefly.save |
|
96 | 97 | user = find(:first, :conditions => ["login=?", login]) |
|
97 | 98 | logger.info("User '#{user.login}' created on the fly.") if logger |
|
98 | 99 | end |
|
99 | 100 | end |
|
100 | 101 | end |
|
101 | 102 | user.update_attribute(:last_login_on, Time.now) if user |
|
102 | 103 | user |
|
103 | 104 | |
|
104 | 105 | rescue => text |
|
105 | 106 | raise text |
|
106 | 107 | end |
|
107 | 108 | |
|
108 | 109 | # Return user's full name for display |
|
109 | 110 | def name |
|
110 | 111 | "#{firstname} #{lastname}" |
|
111 | 112 | end |
|
112 | 113 | |
|
113 | 114 | def active? |
|
114 | 115 | self.status == STATUS_ACTIVE |
|
115 | 116 | end |
|
116 | 117 | |
|
117 | 118 | def registered? |
|
118 | 119 | self.status == STATUS_REGISTERED |
|
119 | 120 | end |
|
120 | 121 | |
|
121 | 122 | def locked? |
|
122 | 123 | self.status == STATUS_LOCKED |
|
123 | 124 | end |
|
124 | 125 | |
|
125 | 126 | def check_password?(clear_password) |
|
126 | 127 | User.hash_password(clear_password) == self.hashed_password |
|
127 | 128 | end |
|
128 | 129 | |
|
129 | 130 | def pref |
|
130 | 131 | self.preference ||= UserPreference.new(:user => self) |
|
131 | 132 | end |
|
132 | 133 | |
|
133 | 134 | def time_zone |
|
134 | 135 | self.pref.time_zone.nil? ? nil : TimeZone[self.pref.time_zone] |
|
135 | 136 | end |
|
136 | 137 | |
|
137 | 138 | # Return user's RSS key (a 40 chars long string), used to access feeds |
|
138 | 139 | def rss_key |
|
139 | 140 | token = self.rss_token || Token.create(:user => self, :action => 'feeds') |
|
140 | 141 | token.value |
|
141 | 142 | end |
|
142 | 143 | |
|
143 | 144 | # Return an array of project ids for which the user has explicitly turned mail notifications on |
|
144 | 145 | def notified_projects_ids |
|
145 | 146 | @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) |
|
146 | 147 | end |
|
147 | 148 | |
|
148 | 149 | def notified_project_ids=(ids) |
|
149 | 150 | Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) |
|
150 | 151 | Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? |
|
151 | 152 | @notified_projects_ids = nil |
|
152 | 153 | notified_projects_ids |
|
153 | 154 | end |
|
154 | 155 | |
|
155 | 156 | def self.find_by_rss_key(key) |
|
156 | 157 | token = Token.find_by_value(key) |
|
157 | 158 | token && token.user.active? ? token.user : nil |
|
158 | 159 | end |
|
159 | 160 | |
|
160 | 161 | def self.find_by_autologin_key(key) |
|
161 | 162 | token = Token.find_by_action_and_value('autologin', key) |
|
162 | 163 | token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil |
|
163 | 164 | end |
|
164 | 165 | |
|
165 | 166 | def <=>(user) |
|
166 | 167 | user.nil? ? -1 : (lastname == user.lastname ? firstname <=> user.firstname : lastname <=> user.lastname) |
|
167 | 168 | end |
|
168 | 169 | |
|
169 | 170 | def to_s |
|
170 | 171 | name |
|
171 | 172 | end |
|
172 | 173 | |
|
173 | 174 | def logged? |
|
174 | 175 | true |
|
175 | 176 | end |
|
176 | 177 | |
|
177 | 178 | # Return user's role for project |
|
178 | 179 | def role_for_project(project) |
|
179 | 180 | # No role on archived projects |
|
180 | 181 | return nil unless project && project.active? |
|
181 | 182 | if logged? |
|
182 | 183 | # Find project membership |
|
183 | 184 | membership = memberships.detect {|m| m.project_id == project.id} |
|
184 | 185 | if membership |
|
185 | 186 | membership.role |
|
186 | 187 | else |
|
187 | 188 | @role_non_member ||= Role.non_member |
|
188 | 189 | end |
|
189 | 190 | else |
|
190 | 191 | @role_anonymous ||= Role.anonymous |
|
191 | 192 | end |
|
192 | 193 | end |
|
193 | 194 | |
|
194 | 195 | # Return true if the user is a member of project |
|
195 | 196 | def member_of?(project) |
|
196 | 197 | role_for_project(project).member? |
|
197 | 198 | end |
|
198 | 199 | |
|
199 | 200 | # Return true if the user is allowed to do the specified action on project |
|
200 | 201 | # action can be: |
|
201 | 202 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
202 | 203 | # * a permission Symbol (eg. :edit_project) |
|
203 | 204 | def allowed_to?(action, project) |
|
204 | 205 | # No action allowed on archived projects |
|
205 | 206 | return false unless project.active? |
|
206 | 207 | # No action allowed on disabled modules |
|
207 | 208 | return false unless project.allows_to?(action) |
|
208 | 209 | # Admin users are authorized for anything else |
|
209 | 210 | return true if admin? |
|
210 | 211 | |
|
211 | 212 | role = role_for_project(project) |
|
212 | 213 | return false unless role |
|
213 | 214 | role.allowed_to?(action) && (project.is_public? || role.member?) |
|
214 | 215 | end |
|
215 | 216 | |
|
216 | 217 | def self.current=(user) |
|
217 | 218 | @current_user = user |
|
218 | 219 | end |
|
219 | 220 | |
|
220 | 221 | def self.current |
|
221 | 222 | @current_user ||= User.anonymous |
|
222 | 223 | end |
|
223 | 224 | |
|
224 | 225 | def self.anonymous |
|
225 | 226 | return @anonymous_user if @anonymous_user |
|
226 | 227 | anonymous_user = AnonymousUser.find(:first) |
|
227 | 228 | if anonymous_user.nil? |
|
228 | 229 | anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) |
|
229 | 230 | raise 'Unable to create the anonymous user.' if anonymous_user.new_record? |
|
230 | 231 | end |
|
231 | 232 | @anonymous_user = anonymous_user |
|
232 | 233 | end |
|
233 | 234 | |
|
234 | 235 | private |
|
235 | 236 | # Return password digest |
|
236 | 237 | def self.hash_password(clear_password) |
|
237 | 238 | Digest::SHA1.hexdigest(clear_password || "") |
|
238 | 239 | end |
|
239 | 240 | end |
|
240 | 241 | |
|
241 | 242 | class AnonymousUser < User |
|
242 | 243 | |
|
243 | 244 | def validate_on_create |
|
244 | 245 | # There should be only one AnonymousUser in the database |
|
245 | 246 | errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first) |
|
246 | 247 | end |
|
247 | 248 | |
|
248 | 249 | # Overrides a few properties |
|
249 | 250 | def logged?; false end |
|
250 | 251 | def admin; false end |
|
251 | 252 | def name; 'Anonymous' end |
|
252 | 253 | def mail; nil end |
|
253 | 254 | def time_zone; nil end |
|
254 | 255 | def rss_key; nil end |
|
255 | 256 | end |
General Comments 0
You need to be logged in to leave comments.
Login now