Module: MuckUsers::Models::MuckUser

Extended by:
ActiveSupport::Concern
Defined in:
lib/muck-users/models/user.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#accepts_terms_of_service?Boolean

Returns:

  • (Boolean)


225
226
227
228
229
# File 'lib/muck-users/models/user.rb', line 225

def accepts_terms_of_service?
  if !self.terms_of_service
    self.errors[:base] << I18n.translate('muck.users.terms_of_service_required')
  end
end

#activate!Object



199
200
201
202
# File 'lib/muck-users/models/user.rb', line 199

def activate!
  self.activated_at = Time.now.utc
  self.save!
end

#active?Boolean

Authlogic automatically executes the following methods

Returns:

  • (Boolean)


184
185
186
# File 'lib/muck-users/models/user.rb', line 184

def active?
  !activated_at.blank?
end

#add_exclude_fields(options = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/muck-users/models/user.rb', line 162

def add_exclude_fields(options = {})
  options ||= {}
  options[:except] ||= []
  options[:except] << :crypted_password << :salt << :remember_token << :remember_token_expires_at << :activation_code
  options[:except] << :activated_at << :password_reset_code << :enabled << :terms_of_service << :can_send_messages << :identity_url
  options[:except] << :tmp_password << :protected_profile << :public_profile << :disabled_at << :current_login_ip << :access_code_id
  options[:except] << :failed_login_count << :last_login_ip
  options[:except] << :password_salt << :perishable_token << :persistence_token << :single_access_token
  options
end

#add_to_role(rolename) ⇒ Object

Add the user to a new role



147
148
149
150
151
# File 'lib/muck-users/models/user.rb', line 147

def add_to_role(rolename)
  @role_names = nil
  role = Role.find_or_create_by_rolename(rolename)
  self.roles << role if !self.roles.include?(role) # Make sure that the user can only be put into a role once
end

#admin?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/muck-users/models/user.rb', line 153

def admin?
  self.has_role?('administrator')
end

#any_role?(*test_rolenames) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
# File 'lib/muck-users/models/user.rb', line 138

def any_role?(*test_rolenames)
  test_rolenames = [test_rolenames] unless test_rolenames.is_a?(Array)
  test_rolenames.flatten!
  @role_names = self.roles.map(&:rolename) if @role_names.blank?
  return false if @role_names.blank?
  (@role_names & test_rolenames).length > 0
end

#as_json(options = {}) ⇒ Object



178
179
180
181
# File 'lib/muck-users/models/user.rb', line 178

def as_json(options = {})
  options = add_exclude_fields(options)
  super(options)
end

#can_edit?(user) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
# File 'lib/muck-users/models/user.rb', line 157

def can_edit?(user)
  return false if user.nil?
  self.id == user.id || user.admin?
end

#deactivate!Object



204
205
206
207
# File 'lib/muck-users/models/user.rb', line 204

def deactivate!
  self.activated_at = nil
  self.save!
end

#deliver_activation_confirmation!Object



94
95
96
97
# File 'lib/muck-users/models/user.rb', line 94

def deliver_activation_confirmation!
  reset_perishable_token!
  UserMailer.activation_confirmation(self).deliver
end

#deliver_activation_instructions!Object



99
100
101
102
# File 'lib/muck-users/models/user.rb', line 99

def deliver_activation_instructions!
  reset_perishable_token!
  UserMailer.activation_instructions(self).deliver
end

#deliver_password_reset_instructions!Object



104
105
106
107
108
109
110
111
# File 'lib/muck-users/models/user.rb', line 104

def deliver_password_reset_instructions!
  if self.active?
    reset_perishable_token!  
    UserMailer.password_reset_instructions(self).deliver
  else
    UserMailer.password_not_active_instructions(self).deliver
  end
end

#deliver_username_request!Object



113
114
115
# File 'lib/muck-users/models/user.rb', line 113

def deliver_username_request!
  UserMailer.username_request(self).deliver
end

#deliver_welcome_emailObject



90
91
92
# File 'lib/muck-users/models/user.rb', line 90

def deliver_welcome_email
  UserMailer.welcome_notification(self).deliver if MuckUsers.configuration.send_welcome
end

#display_nameObject



221
222
223
# File 'lib/muck-users/models/user.rb', line 221

def display_name
  CGI::escapeHTML(self.)
end

#full_nameObject



213
214
215
216
217
218
219
# File 'lib/muck-users/models/user.rb', line 213

def full_name
  if self.first_name.blank? && self.last_name.blank?
    self.display_name rescue 'Deleted user'
  else
    ((CGI::escapeHTML(self.first_name) || '') + ' ' + (CGI::escapeHTML(self.last_name) || '')).strip
  end
end

#generate_passwordObject

Build a random password



118
119
120
# File 'lib/muck-users/models/user.rb', line 118

def generate_password
  self.password = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{self.email}--#{self.id}")
end

#has_role?(rolename) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/muck-users/models/user.rb', line 134

def has_role?(rolename)
  self.any_role?(rolename)
end

#is_in_role?(object, roles) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/muck-users/models/user.rb', line 130

def is_in_role?(object, roles)
  raise 'not implemented'
end

#lower_loginObject

lowercase all logins



195
196
197
# File 'lib/muck-users/models/user.rb', line 195

def 
  self. = self..nil? ? nil : self..downcase 
end

#reset_password!(user) ⇒ Object

Since password reset doesn’t need to change openid_identifier, we save without block as usual.



124
125
126
127
128
# File 'lib/muck-users/models/user.rb', line 124

def reset_password!(user)
  self.password = user[:password]
  self.password_confirmation = user[:password_confirmation]
  save
end

#short_nameObject



209
210
211
# File 'lib/muck-users/models/user.rb', line 209

def short_name
  CGI::escapeHTML(self.first_name) || self.display_name
end

#to_xml(options = {}) ⇒ Object



173
174
175
176
# File 'lib/muck-users/models/user.rb', line 173

def to_xml(options = {})
  options = add_exclude_fields(options)
  super(options)
end