Sha256: 93979e23ea126d9143b8665ecd94019d70ea1a6527d8ffd609ac338c9295f9ca

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 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 { |role| add_role(role.name) }
    end

    private
    def self.extended_auth_attributes(auth_data)
      {
        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
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
g5_authenticatable-0.5.1 app/models/g5_authenticatable/user.rb
g5_authenticatable-0.5.0 app/models/g5_authenticatable/user.rb