Class: User

Inherits:
KitIndexed show all
Includes:
ActionView::Helpers, BCrypt
Defined in:
app/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KitIndexed

do_indexing, indexed_columns, paginate

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'app/models/user.rb', line 86

def method_missing(meth, *args, &block)
  if meth =~ /^image_(.*)$/
    return attribute_image($1, args[0]) 
  end

  if UserAttribute.where(:code_name=>meth).first
    return attribute_value(meth)
  end
  super
end

Instance Attribute Details

#fave_pagesObject

Returns the value of attribute fave_pages.



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

def fave_pages
  @fave_pages
end

#loaded_attsObject

Returns the value of attribute loaded_atts.



72
73
74
# File 'app/models/user.rb', line 72

def loaded_atts
  @loaded_atts
end

#skip_passwordObject

Returns the value of attribute skip_password.



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

def skip_password
  @skip_password
end

Class Method Details

.authenticate(sid, email, password) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'app/models/user.rb', line 316

def self.authenticate(sid, email, password)
  logger.debug "Authenticating #{email}"
  u = User.sys(sid).where(:email=>email).first

  unlock_hours = Preference.get_cached(sid, "account_unlock_after_hours")
  if unlock_hours.not_blank?
    unlock_hours = unlock_hours.to_i
    if Time.now >= u.locked_at + unlock_hours.hours
      u.unlock_access!
    end
  end

  return nil unless u
  return nil unless u.password==password
  return nil unless u.not_banned?
  return nil if u.locked?
  return u
end


335
336
337
338
# File 'app/models/user.rb', line 335

def self.cookie_authenticate(sid, token)
  return nil unless token
  User.sys(sid).where(:remember_token=>token).where("remember_token is not null and remember_token <> ''").first
end

.load_forum_attributes(system_id, users) ⇒ Object

this is like our own version of eager loading, because we can’t use the system_id in normal eager loading



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/user.rb', line 54

def self.load_forum_attributes(system_id, users)
  ids = users.keys.join(',')
  return if ids.is_blank?

  attributes_to_load = Preference.get_cached(system_id, "user_attributes_to_load")
  return unless attributes_to_load

  atts = eval(attributes_to_load)

  users.each do |id, user|
    user.loaded_atts = {}
  end

  UserAttributeValue.where("user_attribute_id in (#{atts.keys.join(',')})").where("user_id in (#{ids})").each do |uav|
    users[uav.user_id].loaded_atts[atts[uav.user_attribute_id]] = uav
  end  
end

.record_failed_signin(sid, request, method = 'e') ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'app/models/user.rb', line 390

def self.(sid, request, method = 'e')
  user = nil
  email = request.params[:email] || request.params[:user][:email]
  if email
    user = User.sys(sid).where(:email=>email).first
    if user
      user.failed_attempts = (self.failed_attempts + 1) rescue 1
      if user.failed_attempts >= (Preference.get_cached(sid, "account_lock_after_failures") || "10").to_i
        user.lock_access!
      end
      user.save
    end
  end
  return user
end

.token_authenticate(sid, token) ⇒ Object



340
341
342
343
344
345
346
# File 'app/models/user.rb', line 340

def self.token_authenticate(sid, token)
  if Preference.get_cached(sid, "account_token_auth")=="true" 
    User.sys(sid).where("token is not null and token<>''").where(:token=>token).first rescue nil
  else
    nil
  end
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'app/models/user.rb', line 250

def active?
  self.not_banned?
end

#add_role(role, current_user_id = nil) ⇒ Object



254
255
256
257
258
259
260
261
# File 'app/models/user.rb', line 254

def add_role(role, current_user_id = nil)
  r = Role.where(:name=>role).sys(self.system.id).first
  return nil unless r
  Activity.add(self.system.id, "Adding role '#{role}' to user <a href='/admin/user/#{self.id}'>#{self.email}</a>", 0, "Users")
  self.user_notes << UserNote.new(:category=>"Role", :description=>"Added to role #{role}", :created_by_id=>current_user_id)
  self.roles << r 
  Rails.cache.delete(self.role_cache_key)
end

#admin?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'app/models/user.rb', line 205

def admin?
  role?("Admin") || role?("SuperAdmin")
end

#admin_status(set) ⇒ Object



284
285
286
# File 'app/models/user.rb', line 284

def admin_status(set)
  set ? self.add_role('Admin')  : self.remove_role('Admin')
end

#attribute_image(name, size) ⇒ Object



144
145
146
# File 'app/models/user.rb', line 144

def attribute_image(name, size)
  self.user_attribute_values.where(["user_attributes.name = ?", name]).joins(:user_attribute).first.asset.url(size) rescue nil
end

#attribute_value(name) ⇒ Object



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

def attribute_value(name)
  self.user_attribute_values.where(["user_attributes.name = ?", name]).joins(:user_attribute).first.value rescue nil
end

#ban!(current_user_id = nil) ⇒ Object



238
239
240
241
242
243
244
# File 'app/models/user.rb', line 238

def ban!(current_user_id = nil)
  return if self.email == Preference.get_cached(self.system_id,'master_user_email')
  self.banned_at = Time.now
  self.save
  Activity.add(self.system.id, "Banning user <a href='/admin/user/#{self.id}'>#{self.email}</a>", 0, "Users")
  self.user_notes << UserNote.new(:category=>"Ban", :description=>"Banned", :created_by_id=>current_user_id)
end

#check_spam_pointsObject



292
293
294
295
296
297
298
299
300
# File 'app/models/user.rb', line 292

def check_spam_points
  max = (Preference.getCached(self.system_id, 'spam_points_to_ban_user') || "10").to_i
  return unless self.spam_points
  if self.spam_points >= max
    self.banned_at = Time.now
    Activity.add(self.system_id, "Banning user <a href='/admin/user/#{self.id}'>#{self.email}</a> due to accumulated spam-type activity", 0, "Users")
    self.user_notes << UserNote.new(:category=>"Ban", :description=>"Accumulation of spam points exceeding #{max}", :created_by_id=>0)
  end
end

#concatenated_groupsObject



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

def concatenated_groups
  self.groups.map {|g| g.name}.join(", ")
end

#designer?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'app/models/user.rb', line 213

def designer?
  role?("Designer") || role?("SuperAdmin")
end

#designer_status(set) ⇒ Object



280
281
282
# File 'app/models/user.rb', line 280

def designer_status(set)
  set ? self.add_role('Designer') : self.remove_role('Designer')
end

#dont_rememberObject



359
360
361
362
363
# File 'app/models/user.rb', line 359

def dont_remember
  self.remember_created_at = nil
  self.remember_token = nil
  self.save
end

#editor?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'app/models/user.rb', line 217

def editor?
  role?("Editor") || role?("SuperAdmin") || role?("Admin") || role?("Designer")
end

#favourites_pages_commaObject



182
183
184
185
# File 'app/models/user.rb', line 182

def favourites_pages_comma
  load_favourite_pages
  fave_pages.keys.join(',')
end

#forum_rating(cant_be_negative = true, min_votes = 0) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'app/models/user.rb', line 102

def forum_rating(cant_be_negative = true, min_votes = 0)
  return nil if self.forum_votes < min_votes

  r = self.forum_votes.to_f / self.forum_points.to_f * 100

  r = 0 if r < 0 && cant_be_negative

  "%d" % r rescue nil
end

#heard_from!Object



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

def heard_from!
  User.connection.execute("update users set last_heard_from=now() where id = #{self.id}")
end

#is_favourite_page?(page_url) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
# File 'app/models/user.rb', line 177

def is_favourite_page?(page_url)
  load_favourite_pages
  return self.fave_pages[page_url.id]==1
end


302
303
304
305
306
# File 'app/models/user.rb', line 302

def links
  return self.user_links.order("id") if self.user_links.size>0

  return UserLink.where("user_id is null").order("id").all
end

#load_favourite_pagesObject



168
169
170
171
172
173
174
175
# File 'app/models/user.rb', line 168

def load_favourite_pages
  unless self.fave_pages
    self.fave_pages = Hash.new
    pages.each do |pu|
      self.fave_pages[pu.id] = 1
    end
  end
end

#loaded_attributes(name) ⇒ Object



74
75
76
77
78
79
80
# File 'app/models/user.rb', line 74

def loaded_attributes(name)
  unless loaded_atts
    User.load_forum_attributes(self.system_id, { self.id => self } )
  end 
  
  return loaded_atts ? loaded_atts[name] : nil
end

#lock_access!Object



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

def lock_access!
  Activity.add(self.system.id, "Locking user <a href='/admin/user/#{self.id}'>#{self.email}</a> after #{self.failed_attempts} failed attempts", 0, "Users")
  self.locked_at = Time.now
  self.save
end

#locked?Boolean

Returns:

  • (Boolean)


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

def locked?
  self.locked_at != nil
end

#mailchimp_connectionObject



312
313
314
# File 'app/models/user.rb', line 312

def mailchimp_connection
  Gibbon.new(Preference.get_cached(self.system_id,'mailchimp_api_key'))
end

#moderator?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'app/models/user.rb', line 201

def moderator?
  role?("Forum Moderator") 
end

#moderator_status(set) ⇒ Object



276
277
278
# File 'app/models/user.rb', line 276

def moderator_status(set) 
  set ? self.add_role('Forum Moderator')  : self.remove_role('Forum Moderator')
end

#not_banned?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'app/models/user.rb', line 246

def not_banned?
  self.banned_at == nil
end

#passwordObject



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

def password
  @password ||= Password.new(self.encrypted_password)
end

#password=(new_password) ⇒ Object



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

def password=(new_password)
  @password = new_password
  self.encrypted_password = Password.create(new_password)
end

#rankingObject

an elevating numerical representation of the various roles



222
223
224
225
226
227
228
229
# File 'app/models/user.rb', line 222

def ranking
  return 10 if self.superadmin?
  return 8 if self.designer?
  return 7 if self.admin?
  return 5 if self.editor?
  return 3 if self.moderator?
  return 1
end

#record_signin(sid, request, method = 'e') ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'app/models/user.rb', line 365

def (sid, request, method = 'e')
  lh = UserLogin.new
  lh.user_id = self.id
  lh.ip = request.remote_ip
  lh.method = method
  lh.system_id = sid
  lh.save
  self. = (self. + 1) rescue 1
  self. = self.
  self. = Time.now
  self. = self.
  self. = request.remote_ip
  self.failed_attempts = 0
  self.skip_password = true

  if method=='c' || request.params[:remember_me]
    self.remember_created_at = Time.now
    self.remember_token ||= Digest::MD5.hexdigest(self.email + Time.now.to_s + rand(100000).to_s) 
  else
    self.remember_created_at = nil
    self.remember_token = nil
  end
  self.save
end

#remove_role(role, current_user_id = nil) ⇒ Object



263
264
265
266
267
268
269
270
# File 'app/models/user.rb', line 263

def remove_role(role, current_user_id = nil)
  r = Role.where(:name=>role).sys(self.system.id).first
  return nil unless r
  Activity.add(self.system.id, "Removing role '#{role}' to user <a href='/admin/user/#{self.id}'>#{self.email}</a>", 0, "Users")
  self.user_notes << UserNote.new(:category=>"Role", :description=>"Removed from role #{role}", :created_by_id=>current_user_id)
  self.roles.delete(r)
  Rails.cache.delete(self.role_cache_key)
end

#role?(role) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
# File 'app/models/user.rb', line 187

def role?(role)
  users_roles = Rails.cache.fetch(self.role_cache_key, :expires_in => 10.minutes) do
    ur = {}
    self.roles.map {|role| ur[role.name.camelize.downcase] = true}
    ur
  end

  users_roles[role.camelize.downcase] ? true : false
end

#role_cache_keyObject



272
273
274
# File 'app/models/user.rb', line 272

def role_cache_key
  "user_#{self.id}_roles"
end

#sees_menu?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'app/models/user.rb', line 197

def sees_menu?
  admin? || editor?
end

#short_displayObject



308
309
310
# File 'app/models/user.rb', line 308

def short_display
  self.email.split('@')[0]
end

#status_displayObject



348
349
350
351
352
353
354
355
356
# File 'app/models/user.rb', line 348

def status_display
  status = []
  roles = self.roles.collect {|r| r.name }.join(',  ')
  status << "[#{roles}]" if roles.not_blank?
  status << "Banned" if self.banned_at
  status << "Locked" if self.locked_at
  status << "Failures: #{self.failed_attempts}" if self.failed_attempts>0
  status.join(", ")
end

#superadmin?Boolean

Returns:

  • (Boolean)


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

def superadmin?
  role?("SuperAdmin")
end

#system_filter_sqlObject



82
83
84
# File 'app/models/user.rb', line 82

def system_filter_sql 
  "system_id = #{self.system_id}"
end

#unban!(current_user_id = nil) ⇒ Object



231
232
233
234
235
236
# File 'app/models/user.rb', line 231

def unban!(current_user_id = nil)
  self.banned_at = nil
  self.save
  Activity.add(self.system.id, "UnBanning user <a href='/admin/user/#{self.id}'>#{self.email}</a>", 0, "Users")
  self.user_notes << UserNote.new(:category=>"Ban", :description=>"Unbanned", :created_by_id=>current_user_id)
end

#unlock_access!Object



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

def unlock_access!
  Activity.add(self.system.id, "UnLocking user <a href='/admin/user/#{self.id}'>#{self.email}</a>", 0, "Users")
  self.locked_at = nil
  self.save
end

#welcome_messageObject



288
289
290
# File 'app/models/user.rb', line 288

def welcome_message
  Notification.welcome_message(self, self.system_id).deliver unless Preference.get(self.system_id, "no_signup_confirmation")=='true'
end