-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
user.rb
168 lines (145 loc) · 6.39 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Fat Free CRM
# Copyright (C) 2008-2011 by Michael Dvorkin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------
# == Schema Information
# Schema version: 27
#
# Table name: users
#
# id :integer(4) not null, primary key
# username :string(32) default(""), not null
# email :string(64) default(""), not null
# first_name :string(32)
# last_name :string(32)
# title :string(64)
# company :string(64)
# alt_email :string(64)
# phone :string(32)
# mobile :string(32)
# aim :string(32)
# yahoo :string(32)
# google :string(32)
# skype :string(32)
# password_hash :string(255) default(""), not null
# password_salt :string(255) default(""), not null
# persistence_token :string(255) default(""), not null
# perishable_token :string(255) default(""), not null
# last_request_at :datetime
# last_login_at :datetime
# current_login_at :datetime
# last_login_ip :string(255)
# current_login_ip :string(255)
# login_count :integer(4) default(0), not null
# deleted_at :datetime
# created_at :datetime
# updated_at :datetime
# admin :boolean(1) not null
# suspended_at :datetime
#
class User < ActiveRecord::Base
attr_protected :admin, :suspended_at
before_create :check_if_needs_approval
before_destroy :check_if_current_user, :check_if_has_related_assets
has_one :avatar, :as => :entity, :dependent => :destroy # Personal avatar.
has_many :avatars # As owner who uploaded it, ex. Contact avatar.
has_many :comments, :as => :commentable # As owner who crated a comment.
has_many :accounts
has_many :campaigns
has_many :leads
has_many :contacts
has_many :opportunities
has_many :activities, :dependent => :destroy
has_many :permissions, :dependent => :destroy
has_many :preferences, :dependent => :destroy
is_paranoid
# For some reason this does not play nice with is_paranoid when set as default scope
scope :by_id, order('id DESC')
scope :except, lambda { |user| where('id != ?', user.id) }
scope :by_name, order('first_name, last_name, email')
scope :search, lambda { |query|
query = query.gsub(/[^\w\s\-\.'\p{L}]/u, '').strip
where('upper(username) LIKE upper(:s) OR upper(first_name) LIKE upper(:s) OR upper(last_name) LIKE upper(:s)', :s => "#{query}%")
}
acts_as_authentic do |c|
c.session_class = Authentication
c.validates_uniqueness_of_login_field_options = { :message => :username_taken }
c.validates_uniqueness_of_email_field_options = { :message => :email_in_use }
c.validates_length_of_password_field_options = { :minimum => 0, :allow_blank => true, :if => :require_password? }
c.ignore_blank_passwords = true
end
# Store current user in the class so we could access it from the activity
# observer without extra authentication query.
cattr_accessor :current_user
validates_presence_of :username, :message => :missing_username
validates_presence_of :email, :message => :missing_email
#----------------------------------------------------------------------------
def name
self.first_name.blank? ? self.username : self.first_name
end
#----------------------------------------------------------------------------
def full_name
self.first_name.blank? && self.last_name.blank? ? self.email : "#{self.first_name} #{self.last_name}".strip
end
#----------------------------------------------------------------------------
def suspended?
self.suspended_at != nil
end
#----------------------------------------------------------------------------
def awaits_approval?
self.suspended? && self.login_count == 0 && Setting.user_signup == :needs_approval
end
#----------------------------------------------------------------------------
def preference
Preference.new(:user => self)
end
alias :pref :preference
#----------------------------------------------------------------------------
def deliver_password_reset_instructions!
reset_perishable_token!
Notifier.password_reset_instructions(self).deliver
end
# Override global I18n.locale if the user has individual local preference.
#----------------------------------------------------------------------------
def set_individual_locale
I18n.locale = self.preference[:locale] if self.preference[:locale]
end
# Generate the value of single access token if it hasn't been set already.
#----------------------------------------------------------------------------
def set_single_access_token
self.single_access_token ||= update_attribute(:single_access_token, Authlogic::Random.friendly_token)
end
private
# Suspend newly created user if signup requires an approval.
#----------------------------------------------------------------------------
def check_if_needs_approval
self.suspended_at = Time.now if Setting.user_signup == :needs_approval && !self.admin
end
# Prevent current user from deleting herself.
#----------------------------------------------------------------------------
def check_if_current_user
User.current_user.nil? || User.current_user != self
end
# Prevent deleting a user unless she has no artifacts left.
#----------------------------------------------------------------------------
def check_if_has_related_assets
artifacts = %w(Account Campaign Lead Contact Opportunity Comment).inject(0) do |sum, asset|
klass = asset.constantize
sum += klass.assigned_to(self).count if asset != "Comment"
sum += klass.created_by(self).count
end
artifacts == 0
end
end