Class: User

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

Overview

Copyright (c) 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or http://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(254) 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) google :string(32) encrypted_password :string(255) default(""), not null password_salt :string(255) default(""), not null last_sign_in_at :datetime current_sign_in_at :datetime last_sign_in_ip :string(255) current_sign_in_ip :string(255) sign_in_count :integer default(0), not null deleted_at :datetime created_at :datetime updated_at :datetime admin :boolean default(FALSE), not null suspended_at :datetime unconfirmed_email :string(254) default(""), not null reset_password_token :string(255) reset_password_sent_at :datetime remember_token :string(255) remember_created_at :datetime authentication_token :string(255) confirmation_token :string(255) confirmed_at :datetime confirmation_sent_at :datetime

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.can_signup?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'app/models/users/user.rb', line 209

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

.find_for_database_authentication(warden_conditions) ⇒ Object

Overrides Devise sign-in to use either username or email (case-insensitive)



215
216
217
218
219
220
# File 'app/models/users/user.rb', line 215

def find_for_database_authentication(warden_conditions)
  conditions = warden_conditions.dup
  if  = conditions.delete(:email)
    where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { value: .downcase }]).first
  end
end

Instance Method Details

#abilityObject

Returns permissions ability object.



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

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

#active_for_authentication?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'app/models/users/user.rb', line 129

def active_for_authentication?
  super && confirmed? && !awaits_approval? && !suspended?
end

#awaits_approval?Boolean


Returns:

  • (Boolean)


125
126
127
# File 'app/models/users/user.rb', line 125

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

#destroyable?(current_user) ⇒ Boolean

Returns true if this user is allowed to be destroyed.

Returns:

  • (Boolean)


185
186
187
# File 'app/models/users/user.rb', line 185

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

#emailable?Boolean

Send emails to active users only

Returns:

  • (Boolean)


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

def emailable?
  confirmed? && !awaits_approval? && !suspended? && email.present?
end

#full_nameObject




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

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)


197
198
199
200
201
202
203
204
# File 'app/models/users/user.rb', line 197

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

#inactive_messageObject



133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/users/user.rb', line 133

def inactive_message
  if !confirmed?
    super
  elsif awaits_approval?
    I18n.t(:msg_account_not_approved)
  elsif suspended?
    I18n.t(:msg_invalig_login)
  else
    super
  end
end

#nameObject




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

def name
  first_name.blank? ? username : first_name
end

#password_required?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'app/models/users/user.rb', line 173

def password_required?
  !persisted? || !password.nil? || !password_confirmation.nil?
end

#preferenceObject Also known as: pref




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

def preference
  @preference ||= preferences.build
end

#set_individual_localeObject

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



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

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

#suspend_if_needs_approvalObject

Suspend newly created user if signup requires an approval.



191
192
193
# File 'app/models/users/user.rb', line 191

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

#suspended?Boolean


Returns:

  • (Boolean)


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

def suspended?
  suspended_at != nil
end

#to_json(_options = nil) ⇒ Object

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



165
166
167
# File 'app/models/users/user.rb', line 165

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

#to_xml(_options = nil) ⇒ Object



169
170
171
# File 'app/models/users/user.rb', line 169

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