@@ -1,253 +1,255 | |||
|
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 | 41 | validates_uniqueness_of :login, :mail |
|
42 | 42 | # Login must contain lettres, numbers, underscores only |
|
43 | 43 | validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i |
|
44 | 44 | validates_length_of :login, :maximum => 30 |
|
45 | 45 | validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i |
|
46 | 46 | validates_length_of :firstname, :lastname, :maximum => 30 |
|
47 | 47 | validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true |
|
48 | 48 | validates_length_of :mail, :maximum => 60, :allow_nil => true |
|
49 | 49 | validates_length_of :password, :minimum => 4, :allow_nil => true |
|
50 | 50 | validates_confirmation_of :password, :allow_nil => true |
|
51 | 51 | validates_associated :custom_values, :on => :update |
|
52 | 52 | |
|
53 | 53 | def before_create |
|
54 | 54 | self.mail_notification = false |
|
55 | 55 | true |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def before_save |
|
59 | 59 | # update hashed_password if password was set |
|
60 | 60 | self.hashed_password = User.hash_password(self.password) if self.password |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | def self.active |
|
64 | 64 | with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do |
|
65 | 65 | yield |
|
66 | 66 | end |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def self.find_active(*args) |
|
70 | 70 | active do |
|
71 | 71 | find(*args) |
|
72 | 72 | end |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | # Returns the user that matches provided login and password, or nil |
|
76 | 76 | def self.try_to_login(login, password) |
|
77 | 77 | user = find(:first, :conditions => ["login=?", login]) |
|
78 | 78 | if user |
|
79 | 79 | # user is already in local database |
|
80 | 80 | return nil if !user.active? |
|
81 | 81 | if user.auth_source |
|
82 | 82 | # user has an external authentication method |
|
83 | 83 | return nil unless user.auth_source.authenticate(login, password) |
|
84 | 84 | else |
|
85 | 85 | # authentication with local password |
|
86 | 86 | return nil unless User.hash_password(password) == user.hashed_password |
|
87 | 87 | end |
|
88 | 88 | else |
|
89 | 89 | # user is not yet registered, try to authenticate with available sources |
|
90 | 90 | attrs = AuthSource.authenticate(login, password) |
|
91 | 91 | if attrs |
|
92 | 92 | onthefly = new(*attrs) |
|
93 | 93 | onthefly.login = login |
|
94 | 94 | onthefly.language = Setting.default_language |
|
95 | 95 | if onthefly.save |
|
96 | 96 | user = find(:first, :conditions => ["login=?", login]) |
|
97 | 97 | logger.info("User '#{user.login}' created on the fly.") if logger |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | user.update_attribute(:last_login_on, Time.now) if user |
|
102 | 102 | user |
|
103 | 103 | |
|
104 | 104 | rescue => text |
|
105 | 105 | raise text |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | # Return user's full name for display |
|
109 | 109 | def name |
|
110 | 110 | "#{firstname} #{lastname}" |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | def active? |
|
114 | 114 | self.status == STATUS_ACTIVE |
|
115 | 115 | end |
|
116 | 116 | |
|
117 | 117 | def registered? |
|
118 | 118 | self.status == STATUS_REGISTERED |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | def locked? |
|
122 | 122 | self.status == STATUS_LOCKED |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def check_password?(clear_password) |
|
126 | 126 | User.hash_password(clear_password) == self.hashed_password |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | def pref |
|
130 | 130 | self.preference ||= UserPreference.new(:user => self) |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | def time_zone |
|
134 | 134 | self.pref.time_zone.nil? ? nil : TimeZone[self.pref.time_zone] |
|
135 | 135 | end |
|
136 | 136 | |
|
137 | 137 | # Return user's RSS key (a 40 chars long string), used to access feeds |
|
138 | 138 | def rss_key |
|
139 | 139 | token = self.rss_token || Token.create(:user => self, :action => 'feeds') |
|
140 | 140 | token.value |
|
141 | 141 | end |
|
142 | 142 | |
|
143 | 143 | # Return an array of project ids for which the user has explicitly turned mail notifications on |
|
144 | 144 | def notified_projects_ids |
|
145 | 145 | @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def notified_project_ids=(ids) |
|
149 | 149 | Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) |
|
150 | 150 | Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? |
|
151 | 151 | @notified_projects_ids = nil |
|
152 | 152 | notified_projects_ids |
|
153 | 153 | end |
|
154 | 154 | |
|
155 | 155 | def self.find_by_rss_key(key) |
|
156 | 156 | token = Token.find_by_value(key) |
|
157 | 157 | token && token.user.active? ? token.user : nil |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | def self.find_by_autologin_key(key) |
|
161 | 161 | token = Token.find_by_action_and_value('autologin', key) |
|
162 | 162 | token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil |
|
163 | 163 | end |
|
164 | 164 | |
|
165 | 165 | def <=>(user) |
|
166 | 166 | lastname == user.lastname ? firstname <=> user.firstname : lastname <=> user.lastname |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | 169 | def to_s |
|
170 | 170 | name |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | def logged? |
|
174 | 174 | true |
|
175 | 175 | end |
|
176 | 176 | |
|
177 | 177 | # Return user's role for project |
|
178 | 178 | def role_for_project(project) |
|
179 | 179 | # No role on archived projects |
|
180 | 180 | return nil unless project && project.active? |
|
181 | # Find project membership | |
|
182 | membership = memberships.detect {|m| m.project_id == project.id} | |
|
183 | if membership | |
|
184 |
membership |
|
|
185 | elsif logged? | |
|
186 | Role.non_member | |
|
181 | if logged? | |
|
182 | # Find project membership | |
|
183 | membership = memberships.detect {|m| m.project_id == project.id} | |
|
184 | if membership | |
|
185 | membership.role | |
|
186 | else | |
|
187 | @role_non_member ||= Role.non_member | |
|
188 | end | |
|
187 | 189 | else |
|
188 | Role.anonymous | |
|
190 | @role_anonymous ||= Role.anonymous | |
|
189 | 191 | end |
|
190 | 192 | end |
|
191 | 193 | |
|
192 | 194 | # Return true if the user is a member of project |
|
193 | 195 | def member_of?(project) |
|
194 | 196 | role_for_project(project).member? |
|
195 | 197 | end |
|
196 | 198 | |
|
197 | 199 | # Return true if the user is allowed to do the specified action on project |
|
198 | 200 | # action can be: |
|
199 | 201 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
200 | 202 | # * a permission Symbol (eg. :edit_project) |
|
201 | 203 | def allowed_to?(action, project) |
|
202 | 204 | # No action allowed on archived projects |
|
203 | 205 | return false unless project.active? |
|
204 | 206 | # No action allowed on disabled modules |
|
205 | 207 | return false unless project.allows_to?(action) |
|
206 | 208 | # Admin users are authorized for anything else |
|
207 | 209 | return true if admin? |
|
208 | 210 | |
|
209 | 211 | role = role_for_project(project) |
|
210 | 212 | return false unless role |
|
211 | 213 | role.allowed_to?(action) && (project.is_public? || role.member?) |
|
212 | 214 | end |
|
213 | 215 | |
|
214 | 216 | def self.current=(user) |
|
215 | 217 | @current_user = user |
|
216 | 218 | end |
|
217 | 219 | |
|
218 | 220 | def self.current |
|
219 | 221 | @current_user ||= User.anonymous |
|
220 | 222 | end |
|
221 | 223 | |
|
222 | 224 | def self.anonymous |
|
223 | 225 | return @anonymous_user if @anonymous_user |
|
224 | 226 | anonymous_user = AnonymousUser.find(:first) |
|
225 | 227 | if anonymous_user.nil? |
|
226 | 228 | anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) |
|
227 | 229 | raise 'Unable to create the anonymous user.' if anonymous_user.new_record? |
|
228 | 230 | end |
|
229 | 231 | @anonymous_user = anonymous_user |
|
230 | 232 | end |
|
231 | 233 | |
|
232 | 234 | private |
|
233 | 235 | # Return password digest |
|
234 | 236 | def self.hash_password(clear_password) |
|
235 | 237 | Digest::SHA1.hexdigest(clear_password || "") |
|
236 | 238 | end |
|
237 | 239 | end |
|
238 | 240 | |
|
239 | 241 | class AnonymousUser < User |
|
240 | 242 | |
|
241 | 243 | def validate_on_create |
|
242 | 244 | # There should be only one AnonymousUser in the database |
|
243 | 245 | errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first) |
|
244 | 246 | end |
|
245 | 247 | |
|
246 | 248 | # Overrides a few properties |
|
247 | 249 | def logged?; false end |
|
248 | 250 | def admin; false end |
|
249 | 251 | def name; 'Anonymous' end |
|
250 | 252 | def mail; nil end |
|
251 | 253 | def time_zone; nil end |
|
252 | 254 | def rss_key; nil end |
|
253 | 255 | end |
General Comments 0
You need to be logged in to leave comments.
Login now