##// END OF EJS Templates
Fixed User role_for_project method...
Jean-Philippe Lang -
r413:4ed89dc2d372
parent child
Show More
@@ -1,149 +1,150
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 has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :dependent => :delete_all
22 22 has_many :projects, :through => :memberships
23 23 has_many :custom_values, :dependent => :delete_all, :as => :customized
24 24 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
25 25 has_one :rss_key, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
26 26 belongs_to :auth_source
27 27
28 28 attr_accessor :password, :password_confirmation
29 29 attr_accessor :last_before_login_on
30 30 # Prevents unauthorized assignments
31 31 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
32 32
33 33 validates_presence_of :login, :firstname, :lastname, :mail
34 34 validates_uniqueness_of :login, :mail
35 35 # Login must contain lettres, numbers, underscores only
36 36 validates_format_of :login, :with => /^[a-z0-9_\-@\.]+$/i
37 37 validates_length_of :login, :maximum => 30
38 38 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i
39 39 validates_length_of :firstname, :lastname, :maximum => 30
40 40 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
41 41 validates_length_of :mail, :maximum => 60
42 42 # Password length between 4 and 12
43 43 validates_length_of :password, :in => 4..12, :allow_nil => true
44 44 validates_confirmation_of :password, :allow_nil => true
45 45 validates_associated :custom_values, :on => :update
46 46
47 47 # Account statuses
48 48 STATUS_ACTIVE = 1
49 49 STATUS_REGISTERED = 2
50 50 STATUS_LOCKED = 3
51 51
52 52 def before_save
53 53 # update hashed_password if password was set
54 54 self.hashed_password = User.hash_password(self.password) if self.password
55 55 end
56 56
57 57 def self.active
58 58 with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do
59 59 yield
60 60 end
61 61 end
62 62
63 63 def self.find_active(*args)
64 64 active do
65 65 find(*args)
66 66 end
67 67 end
68 68
69 69 # Returns the user that matches provided login and password, or nil
70 70 def self.try_to_login(login, password)
71 71 user = find(:first, :conditions => ["login=?", login])
72 72 if user
73 73 # user is already in local database
74 74 return nil if !user.active?
75 75 if user.auth_source
76 76 # user has an external authentication method
77 77 return nil unless user.auth_source.authenticate(login, password)
78 78 else
79 79 # authentication with local password
80 80 return nil unless User.hash_password(password) == user.hashed_password
81 81 end
82 82 else
83 83 # user is not yet registered, try to authenticate with available sources
84 84 attrs = AuthSource.authenticate(login, password)
85 85 if attrs
86 86 onthefly = new(*attrs)
87 87 onthefly.login = login
88 88 onthefly.language = Setting.default_language
89 89 if onthefly.save
90 90 user = find(:first, :conditions => ["login=?", login])
91 91 logger.info("User '#{user.login}' created on the fly.") if logger
92 92 end
93 93 end
94 94 end
95 95 user.update_attribute(:last_login_on, Time.now) if user
96 96 user
97 97
98 98 rescue => text
99 99 raise text
100 100 end
101 101
102 102 # Return user's full name for display
103 103 def display_name
104 104 firstname + " " + lastname
105 105 end
106 106
107 107 def name
108 108 display_name
109 109 end
110 110
111 111 def active?
112 112 self.status == STATUS_ACTIVE
113 113 end
114 114
115 115 def registered?
116 116 self.status == STATUS_REGISTERED
117 117 end
118 118
119 119 def locked?
120 120 self.status == STATUS_LOCKED
121 121 end
122 122
123 123 def check_password?(clear_password)
124 124 User.hash_password(clear_password) == self.hashed_password
125 125 end
126 126
127 127 def role_for_project(project)
128 memberships.detect {|m| m.project_id == project.id}
128 member = memberships.detect {|m| m.project_id == project.id}
129 member ? member.role : nil
129 130 end
130 131
131 132 def pref
132 133 self.preference ||= UserPreference.new(:user => self)
133 134 end
134 135
135 136 def get_or_create_rss_key
136 137 self.rss_key || Token.create(:user => self, :action => 'feeds')
137 138 end
138 139
139 140 def self.find_by_rss_key(key)
140 141 token = Token.find_by_value(key)
141 142 token && token.user.active? ? token.user : nil
142 143 end
143 144
144 145 private
145 146 # Return password digest
146 147 def self.hash_password(clear_password)
147 148 Digest::SHA1.hexdigest(clear_password || "")
148 149 end
149 150 end
General Comments 0
You need to be logged in to leave comments. Login now