Class: User

Inherits:
KitIndexed show all
Defined in:
app/models/user.rb

Defined Under Namespace

Classes: ConfirmationsController, PasswordsController, RegistrationsController, SessionsController, UnlocksController

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



80
81
82
83
84
85
86
87
88
89
# File 'app/models/user.rb', line 80

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.



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

def fave_pages
  @fave_pages
end

#loaded_attsObject

Returns the value of attribute loaded_atts.



66
67
68
# File 'app/models/user.rb', line 66

def loaded_atts
  @loaded_atts
end

Class Method Details

.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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/user.rb', line 48

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  super && self.not_banned?
end

#active_for_authentication?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/user.rb', line 106

def active_for_authentication?
  super && self.banned_at == nil
end

#add_role(role, current_user_id = nil) ⇒ Object



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

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)


187
188
189
# File 'app/models/user.rb', line 187

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

#admin_status(set) ⇒ Object



266
267
268
# File 'app/models/user.rb', line 266

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

#attribute_image(name, size) ⇒ Object



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

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



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

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



220
221
222
223
224
225
226
# File 'app/models/user.rb', line 220

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



274
275
276
277
278
279
280
281
282
# File 'app/models/user.rb', line 274

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



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

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

#designer?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'app/models/user.rb', line 195

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

#designer_status(set) ⇒ Object



262
263
264
# File 'app/models/user.rb', line 262

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

#editor?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'app/models/user.rb', line 199

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

#favourites_pages_commaObject



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

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

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



96
97
98
99
100
101
102
103
104
# File 'app/models/user.rb', line 96

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



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

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)


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

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


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

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



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

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



68
69
70
71
72
73
74
# File 'app/models/user.rb', line 68

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



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

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")
  super   
end

#mailchimp_connectionObject



294
295
296
# File 'app/models/user.rb', line 294

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

#moderator?Boolean

Returns:

  • (Boolean)


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

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

#moderator_status(set) ⇒ Object



258
259
260
# File 'app/models/user.rb', line 258

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

#not_banned?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'app/models/user.rb', line 228

def not_banned?
  self.banned_at == nil
end

#rankingObject

an elevating numerical representation of the various roles



204
205
206
207
208
209
210
211
# File 'app/models/user.rb', line 204

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

#remove_role(role, current_user_id = nil) ⇒ Object



245
246
247
248
249
250
251
252
# File 'app/models/user.rb', line 245

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)


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

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



254
255
256
# File 'app/models/user.rb', line 254

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

#sees_menu?Boolean

Returns:

  • (Boolean)


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

def sees_menu?
  admin? || editor?
end

#short_displayObject



290
291
292
# File 'app/models/user.rb', line 290

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

#superadmin?Boolean

Returns:

  • (Boolean)


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

def superadmin?
  role?("SuperAdmin")
end

#system_filter_sqlObject



76
77
78
# File 'app/models/user.rb', line 76

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

#unban!(current_user_id = nil) ⇒ Object



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

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

#welcome_messageObject



270
271
272
# File 'app/models/user.rb', line 270

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