Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/users/user.rb

Overview

Copyright © 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php


Schema Information

Table name: users

id                  :integer         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_login_at       :datetime
current_login_at    :datetime
last_login_ip       :string(255)
current_login_ip    :string(255)
login_count         :integer         default(0), not null
deleted_at          :datetime
created_at          :datetime
updated_at          :datetime
admin               :boolean         default(FALSE), not null
suspended_at        :datetime
single_access_token :string(255)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.can_signup?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'app/models/users/user.rb', line 179

def can_signup?
  %i[allowed needs_approval].include? Setting.
end

Instance Method Details

#abilityObject

Returns permissions ability object.




147
148
149
# File 'app/models/users/user.rb', line 147

def ability
  @ability ||= Ability.new(self)
end

#awaits_approval?Boolean


Returns:

  • (Boolean)


109
110
111
# File 'app/models/users/user.rb', line 109

def awaits_approval?
  suspended? &&  == 0 && Setting. == :needs_approval
end

#deliver_password_reset_instructions!Object




120
121
122
123
# File 'app/models/users/user.rb', line 120

def deliver_password_reset_instructions!
  reset_perishable_token!
  UserMailer.password_reset_instructions(self).deliver_now
end

#destroyable?(current_user) ⇒ Boolean

Returns true if this user is allowed to be destroyed.


Returns:

  • (Boolean)


153
154
155
# File 'app/models/users/user.rb', line 153

def destroyable?(current_user)
  current_user != self && !has_related_assets?
end

#full_nameObject




99
100
101
# File 'app/models/users/user.rb', line 99

def full_name
  first_name.blank? && last_name.blank? ? email : "#{first_name} #{last_name}".strip
end

Prevent deleting a user unless she has no artifacts left.


Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
# File 'app/models/users/user.rb', line 165

def has_related_assets?
  sum = %w[Account Campaign Lead Contact Opportunity Comment Task].detect do |asset|
    klass = asset.constantize

    asset != "Comment" && klass.assigned_to(self).exists? || klass.created_by(self).exists?
  end
  !sum.nil?
end

#nameObject




94
95
96
# File 'app/models/users/user.rb', line 94

def name
  first_name.blank? ? username : first_name
end

#preferenceObject Also known as: pref




114
115
116
# File 'app/models/users/user.rb', line 114

def preference
  @preference ||= preferences.build
end

#set_individual_localeObject

Override global I18n.locale if the user has individual local preference.




127
128
129
# File 'app/models/users/user.rb', line 127

def set_individual_locale
  I18n.locale = preference[:locale] if preference[:locale]
end

#set_single_access_tokenObject

Generate the value of single access token if it hasn’t been set already.




133
134
135
# File 'app/models/users/user.rb', line 133

def set_single_access_token
  self.single_access_token ||= update_attribute(:single_access_token, Authlogic::Random.friendly_token)
end

#suspend_if_needs_approvalObject

Suspend newly created user if signup requires an approval.




159
160
161
# File 'app/models/users/user.rb', line 159

def suspend_if_needs_approval
  self.suspended_at = Time.now if Setting. == :needs_approval && !admin
end

#suspended?Boolean


Returns:

  • (Boolean)


104
105
106
# File 'app/models/users/user.rb', line 104

def suspended?
  suspended_at != nil
end

#to_json(_options = nil) ⇒ Object



137
138
139
# File 'app/models/users/user.rb', line 137

def to_json(_options = nil)
  [name].to_json
end

#to_xml(_options = nil) ⇒ Object



141
142
143
# File 'app/models/users/user.rb', line 141

def to_xml(_options = nil)
  [name].to_xml
end