Module: Devise::Models::G5Authenticatable

Extended by:
ActiveSupport::Concern
Includes:
DeviseG5Authenticatable::Models::ProtectedAttributes
Defined in:
lib/devise_g5_authenticatable/models/g5_authenticatable.rb,
lib/devise_g5_authenticatable/models/protected_attributes.rb

Overview

Authenticatable module, responsible for remote credential management in G5 Auth.

The module assumes that the following attributes have already been defined on the model:

* `provider`: the value will always be 'g5' for G5 Auth users
* `uid`: the unique id for this user in G5 Auth
* `g5_access_token`: the current OAuth access token, if one exists

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#auth_userObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/devise_g5_authenticatable/models/g5_authenticatable.rb', line 24

def auth_user
  begin
    if new_record?
      G5::AuthUserCreator.new(self).create
    else
      G5::AuthUserUpdater.new(self).update
    end
  rescue OAuth2::Error => e
    logger.error("Couldn't save user credentials because: #{e}")
    raise ActiveRecord::RecordNotSaved.new(e.code)
  rescue StandardError => e
    logger.error("Couldn't save user credentials because: #{e}")
    raise ActiveRecord::RecordNotSaved.new(e.message)
  end
end

#clean_up_passwordsObject



40
41
42
# File 'lib/devise_g5_authenticatable/models/g5_authenticatable.rb', line 40

def clean_up_passwords
  self.password = self.password_confirmation = self.current_password = nil
end

#revoke_g5_credentials!Object



68
69
70
71
# File 'lib/devise_g5_authenticatable/models/g5_authenticatable.rb', line 68

def revoke_g5_credentials!
  self.g5_access_token = nil
  save!
end

#update_g5_credentials(oauth_data) ⇒ Object



64
65
66
# File 'lib/devise_g5_authenticatable/models/g5_authenticatable.rb', line 64

def update_g5_credentials(oauth_data)
  self.g5_access_token = oauth_data.credentials.token
end

#update_with_password(params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/devise_g5_authenticatable/models/g5_authenticatable.rb', line 49

def update_with_password(params)
  updated_attributes = params.reject { |k,v| k =~ /password/ && v.blank? }
  current_password = updated_attributes.delete(:current_password)

  if valid = valid_password?(current_password)
    valid = update_attributes(updated_attributes)
  elsif current_password.blank?
    errors.add(:current_password, :blank)
  else
    errors.add(:current_password, :invalid)
  end

  valid
end

#valid_password?(password_to_check) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/devise_g5_authenticatable/models/g5_authenticatable.rb', line 44

def valid_password?(password_to_check)
  validator = Devise::G5::AuthPasswordValidator.new(self)
  validator.valid_password?(password_to_check)
end