Sha256: 945b3f63b0c87b035eaa53ae3c49396403e39ce122ade15ae2a3d7da0d0b615c

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

module G5Authenticatable
  class User < ActiveRecord::Base
    devise :g5_authenticatable, :trackable, :timeoutable
    rolify role_cname: 'G5Authenticatable::Role',
           role_join_table_name: :g5_authenticatable_users_roles

    validates :email, presence: true, uniqueness: true
    validates_uniqueness_of :uid, scope: :provider

    def self.new_with_session(params, session)
      user = super(params, session)
      auth_data = session['omniauth.auth']

      if auth_data
        user.assign_attributes(extended_auth_attributes(auth_data))
        user.update_roles_from_auth(auth_data)
      end
      user
    end

    def self.find_and_update_for_g5_oauth(auth_data)
      user = super(auth_data)
      if user
        user.update_attributes(extended_auth_attributes(auth_data))
        user.update_roles_from_auth(auth_data)
      end

      user
    end

    def update_roles_from_auth(auth_data)
      roles.clear
      auth_data.extra.roles.each do |role|
        role.type == 'GLOBAL' ? add_role(role.name) : add_scoped_role(role)
      end
    end

    def clients
      G5Updatable::ClientPolicy::Scope.new(self, G5Updatable::Client).resolve
    end

    private

    def self.extended_auth_attributes(auth_data)
      h = {
        first_name: auth_data.info.first_name,
        last_name: auth_data.info.last_name,
        phone_number: auth_data.info.phone,
        title: auth_data.extra.title,
        organization_name: auth_data.extra.organization_name
      }
      auth_data.uid.present? ? h.merge!(uid: auth_data.uid) : h
    end

    def add_scoped_role(role)
      the_class = Object.const_get(role.type)
      resource = the_class.where(urn: role.urn).first
      add_role(role.name, resource)
    rescue => e
      Rails.logger.error(e)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
g5_authenticatable-0.7.4 app/models/g5_authenticatable/user.rb
g5_authenticatable-0.7.4.beta.1 app/models/g5_authenticatable/user.rb