Class: MnoEnterprise::User

Inherits:
BaseResource show all
Extended by:
Devise::Models
Defined in:
app/models/mno_enterprise/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseResource

#==, base_class, #cache_key, #clear_association_cache, #clear_attribute_changes!, find_by, first, last, #max_updated_column_timestamp, #read_attribute, #reload, #save, #save!, #update, #write_attribute

Methods included from HerExtension::Validations::RemoteUniquenessValidation

included

Class Method Details

.authenticate(auth_hash) ⇒ Object

Class Methods

The auth_hash includes an email and password Return nil in case of failure



84
85
86
87
88
89
90
91
92
93
# File 'app/models/mno_enterprise/user.rb', line 84

def self.authenticate(auth_hash)
  u = self.post("user_sessions", auth_hash)
  
  if u && u.id
    u.clear_attribute_changes!
    return u
  end
  
  nil
end

.confirm_by_token(confirmation_token) ⇒ Object

Override Devise to allow confirmation via original token Less secure but useful if user has been created by Maestrano Enterprise (happens when an orga_invite is sent to a new user)

Find a user by its confirmation token and try to confirm it. If no user is found, returns a new user with an error. If the user is already confirmed, create an error for the user Options must have the confirmation_token



109
110
111
112
113
# File 'app/models/mno_enterprise/user.rb', line 109

def self.confirm_by_token(confirmation_token)
  confirmable = self.find_for_confirmation(confirmation_token)
  confirmable.perform_confirmation(confirmation_token)
  confirmable
end

.find_for_confirmation(confirmation_token) ⇒ Object

Find a user using a confirmation token



116
117
118
119
120
121
122
123
# File 'app/models/mno_enterprise/user.rb', line 116

def self.find_for_confirmation(confirmation_token)
  original_token     = confirmation_token
  confirmation_token = Devise.token_generator.digest(self, :confirmation_token, confirmation_token)
  
  confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
  confirmable = find_or_initialize_with_error_by(:confirmation_token, original_token) if confirmable.errors.any?
  confirmable
end

Instance Method Details

#authenticatable_saltObject

Override Devise default method



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

def authenticatable_salt
  read_attribute(:authenticatable_salt)
end

#errorsObject

It may happen that that the errors attribute become nil, which breaks the controller logic (rails responder) This getter ensures that ‘errors’ is always initialized



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

def errors
  @errors ||= ActiveModel::Errors.new(self)
end

#expire_user_cacheObject



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

def expire_user_cache
  Rails.cache.delete(['user', self.to_key])
  true # Don't skip save if above return false (memory_store)
end

#failed_attemptsObject

Default value for failed attempts



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

def failed_attempts
  read_attribute(:failed_attempts) || 0
end

#perform_confirmation(confirmation_token) ⇒ Object

Confirm the user and store confirmation_token



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

def perform_confirmation(confirmation_token)
  self.confirm if self.persisted?
  self.confirmation_token = confirmation_token
end

#refresh_user_cacheObject



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

def refresh_user_cache
  self.reload
  Rails.cache.write(['user', self.to_key], self)
end

#role(organization = nil) ⇒ Object

Return the role of this user for the provided organization



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

def role(organization = nil)
  # Return cached version if available
  return self.read_attribute(:role) if !organization
  
  org = self.organizations.to_a.find { |o| o.id.to_s == organization.id.to_s }
  org ? org.role : nil
end

#to_audit_eventObject

Format for audit log



146
147
148
149
150
151
# File 'app/models/mno_enterprise/user.rb', line 146

def to_audit_event
  {
    user_name: to_s,
    user_email: email
  }
end

#to_sObject

Instance Methods



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

def to_s
  "#{name} #{surname}"
end