##// END OF EJS Templates
User's projects alphabetically sorted in the Projects drop down menu....
Jean-Philippe Lang -
r535:9fe0dd051dc2
parent child
Show More
@@ -1,159 +1,159
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 has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :dependent => :delete_all
21 # Account statuses
22 STATUS_ACTIVE = 1
23 STATUS_REGISTERED = 2
24 STATUS_LOCKED = 3
25
26 has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :order => "#{Project.table_name}.name", :dependent => :delete_all
22 27 has_many :projects, :through => :memberships
23 28 has_many :custom_values, :dependent => :delete_all, :as => :customized
24 29 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
25 30 has_one :rss_key, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
26 31 belongs_to :auth_source
27 32
28 33 attr_accessor :password, :password_confirmation
29 34 attr_accessor :last_before_login_on
30 35 # Prevents unauthorized assignments
31 36 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
32 37
33 38 validates_presence_of :login, :firstname, :lastname, :mail
34 39 validates_uniqueness_of :login, :mail
35 40 # Login must contain lettres, numbers, underscores only
36 41 validates_format_of :login, :with => /^[a-z0-9_\-@\.]+$/i
37 42 validates_length_of :login, :maximum => 30
38 43 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i
39 44 validates_length_of :firstname, :lastname, :maximum => 30
40 45 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
41 46 validates_length_of :mail, :maximum => 60
42 47 # Password length between 4 and 12
43 48 validates_length_of :password, :in => 4..12, :allow_nil => true
44 49 validates_confirmation_of :password, :allow_nil => true
45 50 validates_associated :custom_values, :on => :update
46 51
47 # Account statuses
48 STATUS_ACTIVE = 1
49 STATUS_REGISTERED = 2
50 STATUS_LOCKED = 3
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 128 member = memberships.detect {|m| m.project_id == project.id}
129 129 member ? member.role : nil
130 130 end
131 131
132 132 def pref
133 133 self.preference ||= UserPreference.new(:user => self)
134 134 end
135 135
136 136 def get_or_create_rss_key
137 137 self.rss_key || Token.create(:user => self, :action => 'feeds')
138 138 end
139 139
140 140 def self.find_by_rss_key(key)
141 141 token = Token.find_by_value(key)
142 142 token && token.user.active? ? token.user : nil
143 143 end
144 144
145 145 def self.find_by_autologin_key(key)
146 146 token = Token.find_by_action_and_value('autologin', key)
147 147 token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil
148 148 end
149 149
150 150 def <=>(user)
151 151 lastname == user.lastname ? firstname <=> user.firstname : lastname <=> user.lastname
152 152 end
153 153
154 154 private
155 155 # Return password digest
156 156 def self.hash_password(clear_password)
157 157 Digest::SHA1.hexdigest(clear_password || "")
158 158 end
159 159 end
General Comments 0
You need to be logged in to leave comments. Login now