Class: Thincloud::Authentication::Identity

Inherits:
OmniAuth::Identity::Models::ActiveRecord
  • Object
show all
Includes:
ActiveModel::ForbiddenAttributesProtection
Defined in:
app/models/thincloud/authentication/identity.rb

Overview

Public: This class represents a User identity (name, email, login provider)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_omniauth(omniauth) ⇒ Object

Public: Find an Identity by OmniAuth parameters.

omniauth - An instance of OmniAuth::AuthHash

Returns: An instance of Identity or nil.



34
35
36
37
38
# File 'app/models/thincloud/authentication/identity.rb', line 34

def self.find_omniauth(omniauth)
  if omniauth["uid"].present?
    find_by_provider_and_uid omniauth["provider"], omniauth["uid"]
  end
end

.human_attribute_name(attr, options = {}) ⇒ Object

Public: Use a helpful attribute name when displaying errors.



25
26
27
# File 'app/models/thincloud/authentication/identity.rb', line 25

def self.human_attribute_name(attr, options={})
  attr == :password_digest ? "Password" : super
end

.verify!(token) ⇒ Object

Public: Mark the Identity as having been verified.

token - A String containing the verification_token to look up.

Returns: An instance of the found Identity. Raises: ActiveRecord::RecordNotFound if the token cannot be retrieved. ActiveRecord::RecordInvalid if the record cannot be saved.



47
48
49
50
51
52
53
54
55
# File 'app/models/thincloud/authentication/identity.rb', line 47

def self.verify!(token)
  find_by_verification_token!(token).tap do |identity|
    # ensure 'uid' exists, needed for 'identity' provider
    identity.uid = identity.id if identity.uid.blank?
    identity.verification_token = nil
    identity.verified_at = Time.zone.now
    identity.save!
  end
end

Instance Method Details

#apply_omniauth(omniauth) ⇒ Object

Public: Apply attributes returned from OmniAuth.

omniauth - An instance of OmniAuth::AuthHash.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/thincloud/authentication/identity.rb', line 72

def apply_omniauth(omniauth)
  info = omniauth["info"]

  user_name = %Q(#{info["first_name"]} #{info["last_name"]})
  user_name.gsub!(/\s+/, " ").strip!

  self.provider = omniauth["provider"]
  self.uid      = omniauth["uid"]
  self.name     = user_name if self.name.blank?
  self.email    = info["email"] if info["email"] && self.email.blank?
  self
end

#clear_password_reset!Object

Public: Clear password reset fields, reset password_required? requirement

Returns: true

Raises: ActiveRecord::RecordInvalid



101
102
103
104
105
# File 'app/models/thincloud/authentication/identity.rb', line 101

def clear_password_reset!
  self.password_reset_token = nil
  self.password_reset_sent_at = nil
  save!
end

#generate_password_reset!Object

Public: Generate a password reset token

Returns: true

Raises: ActiveRecord::RecordInvalid



90
91
92
93
94
# File 'app/models/thincloud/authentication/identity.rb', line 90

def generate_password_reset!
  self.password_reset_token = SecureRandom.urlsafe_base64
  self.password_reset_sent_at = Time.zone.now
  save_with_identity_password_reset!
end

#identity_provider?Boolean

Public: Determine if the provider is 'identity'

Returns: true or false

Returns:

  • (Boolean)


110
111
112
# File 'app/models/thincloud/authentication/identity.rb', line 110

def identity_provider?
  provider == "identity"
end

#password_confirmation_required?Boolean

Public: Determine if the password confirmation must be provided

Returns: true or false

Returns:

  • (Boolean)


126
127
128
129
130
# File 'app/models/thincloud/authentication/identity.rb', line 126

def password_confirmation_required?
  password_required? || (
    password.present? || password_confirmation.present?
  )
end

#password_required?Boolean

Public: Determine if the password must be provided

Returns: true or false

Returns:

  • (Boolean)


117
118
119
120
121
# File 'app/models/thincloud/authentication/identity.rb', line 117

def password_required?
  (identity_provider? && check_identity_password?) && (
    new_record? || password_reset_token.present?
  )
end

#uidObject

Public: Shim to overcome odd behavior seen during testing with SQLite



58
59
60
# File 'app/models/thincloud/authentication/identity.rb', line 58

def uid
  read_attribute :uid
end

#verified?Boolean

Public: Indicate if the Identity has been verified.

Returns: Boolean.

Returns:

  • (Boolean)


65
66
67
# File 'app/models/thincloud/authentication/identity.rb', line 65

def verified?
  verification_token.blank? && verified_at.present?
end