##// 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 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require "digest/sha1"
18 require "digest/sha1"
19
19
20 class User < ActiveRecord::Base
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 has_many :projects, :through => :memberships
27 has_many :projects, :through => :memberships
23 has_many :custom_values, :dependent => :delete_all, :as => :customized
28 has_many :custom_values, :dependent => :delete_all, :as => :customized
24 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
29 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
25 has_one :rss_key, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
30 has_one :rss_key, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
26 belongs_to :auth_source
31 belongs_to :auth_source
27
32
28 attr_accessor :password, :password_confirmation
33 attr_accessor :password, :password_confirmation
29 attr_accessor :last_before_login_on
34 attr_accessor :last_before_login_on
30 # Prevents unauthorized assignments
35 # Prevents unauthorized assignments
31 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
36 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
32
37
33 validates_presence_of :login, :firstname, :lastname, :mail
38 validates_presence_of :login, :firstname, :lastname, :mail
34 validates_uniqueness_of :login, :mail
39 validates_uniqueness_of :login, :mail
35 # Login must contain lettres, numbers, underscores only
40 # Login must contain lettres, numbers, underscores only
36 validates_format_of :login, :with => /^[a-z0-9_\-@\.]+$/i
41 validates_format_of :login, :with => /^[a-z0-9_\-@\.]+$/i
37 validates_length_of :login, :maximum => 30
42 validates_length_of :login, :maximum => 30
38 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i
43 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i
39 validates_length_of :firstname, :lastname, :maximum => 30
44 validates_length_of :firstname, :lastname, :maximum => 30
40 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
45 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
41 validates_length_of :mail, :maximum => 60
46 validates_length_of :mail, :maximum => 60
42 # Password length between 4 and 12
47 # Password length between 4 and 12
43 validates_length_of :password, :in => 4..12, :allow_nil => true
48 validates_length_of :password, :in => 4..12, :allow_nil => true
44 validates_confirmation_of :password, :allow_nil => true
49 validates_confirmation_of :password, :allow_nil => true
45 validates_associated :custom_values, :on => :update
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 def before_save
52 def before_save
53 # update hashed_password if password was set
53 # update hashed_password if password was set
54 self.hashed_password = User.hash_password(self.password) if self.password
54 self.hashed_password = User.hash_password(self.password) if self.password
55 end
55 end
56
56
57 def self.active
57 def self.active
58 with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do
58 with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do
59 yield
59 yield
60 end
60 end
61 end
61 end
62
62
63 def self.find_active(*args)
63 def self.find_active(*args)
64 active do
64 active do
65 find(*args)
65 find(*args)
66 end
66 end
67 end
67 end
68
68
69 # Returns the user that matches provided login and password, or nil
69 # Returns the user that matches provided login and password, or nil
70 def self.try_to_login(login, password)
70 def self.try_to_login(login, password)
71 user = find(:first, :conditions => ["login=?", login])
71 user = find(:first, :conditions => ["login=?", login])
72 if user
72 if user
73 # user is already in local database
73 # user is already in local database
74 return nil if !user.active?
74 return nil if !user.active?
75 if user.auth_source
75 if user.auth_source
76 # user has an external authentication method
76 # user has an external authentication method
77 return nil unless user.auth_source.authenticate(login, password)
77 return nil unless user.auth_source.authenticate(login, password)
78 else
78 else
79 # authentication with local password
79 # authentication with local password
80 return nil unless User.hash_password(password) == user.hashed_password
80 return nil unless User.hash_password(password) == user.hashed_password
81 end
81 end
82 else
82 else
83 # user is not yet registered, try to authenticate with available sources
83 # user is not yet registered, try to authenticate with available sources
84 attrs = AuthSource.authenticate(login, password)
84 attrs = AuthSource.authenticate(login, password)
85 if attrs
85 if attrs
86 onthefly = new(*attrs)
86 onthefly = new(*attrs)
87 onthefly.login = login
87 onthefly.login = login
88 onthefly.language = Setting.default_language
88 onthefly.language = Setting.default_language
89 if onthefly.save
89 if onthefly.save
90 user = find(:first, :conditions => ["login=?", login])
90 user = find(:first, :conditions => ["login=?", login])
91 logger.info("User '#{user.login}' created on the fly.") if logger
91 logger.info("User '#{user.login}' created on the fly.") if logger
92 end
92 end
93 end
93 end
94 end
94 end
95 user.update_attribute(:last_login_on, Time.now) if user
95 user.update_attribute(:last_login_on, Time.now) if user
96 user
96 user
97
97
98 rescue => text
98 rescue => text
99 raise text
99 raise text
100 end
100 end
101
101
102 # Return user's full name for display
102 # Return user's full name for display
103 def display_name
103 def display_name
104 firstname + " " + lastname
104 firstname + " " + lastname
105 end
105 end
106
106
107 def name
107 def name
108 display_name
108 display_name
109 end
109 end
110
110
111 def active?
111 def active?
112 self.status == STATUS_ACTIVE
112 self.status == STATUS_ACTIVE
113 end
113 end
114
114
115 def registered?
115 def registered?
116 self.status == STATUS_REGISTERED
116 self.status == STATUS_REGISTERED
117 end
117 end
118
118
119 def locked?
119 def locked?
120 self.status == STATUS_LOCKED
120 self.status == STATUS_LOCKED
121 end
121 end
122
122
123 def check_password?(clear_password)
123 def check_password?(clear_password)
124 User.hash_password(clear_password) == self.hashed_password
124 User.hash_password(clear_password) == self.hashed_password
125 end
125 end
126
126
127 def role_for_project(project)
127 def role_for_project(project)
128 member = memberships.detect {|m| m.project_id == project.id}
128 member = memberships.detect {|m| m.project_id == project.id}
129 member ? member.role : nil
129 member ? member.role : nil
130 end
130 end
131
131
132 def pref
132 def pref
133 self.preference ||= UserPreference.new(:user => self)
133 self.preference ||= UserPreference.new(:user => self)
134 end
134 end
135
135
136 def get_or_create_rss_key
136 def get_or_create_rss_key
137 self.rss_key || Token.create(:user => self, :action => 'feeds')
137 self.rss_key || Token.create(:user => self, :action => 'feeds')
138 end
138 end
139
139
140 def self.find_by_rss_key(key)
140 def self.find_by_rss_key(key)
141 token = Token.find_by_value(key)
141 token = Token.find_by_value(key)
142 token && token.user.active? ? token.user : nil
142 token && token.user.active? ? token.user : nil
143 end
143 end
144
144
145 def self.find_by_autologin_key(key)
145 def self.find_by_autologin_key(key)
146 token = Token.find_by_action_and_value('autologin', key)
146 token = Token.find_by_action_and_value('autologin', key)
147 token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil
147 token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user.active? ? token.user : nil
148 end
148 end
149
149
150 def <=>(user)
150 def <=>(user)
151 lastname == user.lastname ? firstname <=> user.firstname : lastname <=> user.lastname
151 lastname == user.lastname ? firstname <=> user.firstname : lastname <=> user.lastname
152 end
152 end
153
153
154 private
154 private
155 # Return password digest
155 # Return password digest
156 def self.hash_password(clear_password)
156 def self.hash_password(clear_password)
157 Digest::SHA1.hexdigest(clear_password || "")
157 Digest::SHA1.hexdigest(clear_password || "")
158 end
158 end
159 end
159 end
General Comments 0
You need to be logged in to leave comments. Login now