Module: DisiidUser

Defined in:
lib/disiid_user.rb,
lib/disiid_user/version.rb

Overview

Usage:

in config/initializers/disid.rb:

DisiidUser::RemoteUser.site = 'http://id.provider.com'
DisiidUser::RemoteUser.auth_token = 'a secret token goes here'

# if resource name is different from 'user' (default)
DisiidUser::RemoteUser.element_name = 'user'

in app/models/user.rb:

class User < ActiveRecord::Base
  include DisiidUser
  ...
end

Defined Under Namespace

Modules: ClassMethods Classes: RemoteUser

Constant Summary collapse

REMOTE_ATTRIBUTES =

list of methid name that could be called from User instance to get data from the RemoteUser instance

%w(email first_name last_name homepage
nickname homepage position office_address office_num 
office_phone office_fax
provider ada_id okkam_id unitn_uid
photo photo_normal photo_thumb hide_photo
remote_created_at remote_updated_at)
VERSION =
"4.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



24
25
26
27
28
29
# File 'lib/disiid_user.rb', line 24

def self.included(klass)
  klass.class_eval do
    extend ClassMethods
    scope :with_local_role, lambda { |role| where('role & ? > 0', 2**LOCAL_ROLES.index(role.to_s)) }
  end
end

.version_stringObject



83
84
85
# File 'lib/disiid_user.rb', line 83

def self.version_string
  "DisiidUser version #{VERSION}"
end

Instance Method Details

#has_role?(role) ⇒ Boolean

Check if the user has the required role admin role has all privileges

has_role? 'manager'
has_role? :admin

Returns:

  • (Boolean)


127
128
129
# File 'lib/disiid_user.rb', line 127

def has_role?(role)
  roles.include?("admin") || roles.include?(role.to_s)
end

#local_rolesObject

List of roles for local setup



104
105
106
# File 'lib/disiid_user.rb', line 104

def local_roles
  LOCAL_ROLES.reject { |r| ((role || 0) & 2**LOCAL_ROLES.index(r)).zero? }
end

#local_roles=(*roles) ⇒ Object

Add roles to the local setup all of the following will work:

user.local_roles = :admin
user.local_roles = [:admin, :manager]
user.local_roles = 'admin'
user.local_roles = 'admin', 'manager' # w/o brackets


114
115
116
# File 'lib/disiid_user.rb', line 114

def local_roles=(*roles)
  self.role = (roles.flatten.map { |r| r.to_s } & LOCAL_ROLES).map { |r| 2**LOCAL_ROLES.index(r) }.sum
end

#remote_userObject

Get the remote user via get ActiveResource



150
151
152
153
154
155
# File 'lib/disiid_user.rb', line 150

def remote_user
  return nil unless uuid
  @remote_user ||= Rails.cache.fetch(uuid, expires_in: DisiidUser::RemoteUser.cache_expiry, race_condition_ttl: 5) do 
    DisiidUser::RemoteUser.find uuid, params: { auth_token: DisiidUser::RemoteUser.auth_token }
  end
rescue; nil; end

#rolesObject

List of roles both local and remote (no duplicates)



119
120
121
# File 'lib/disiid_user.rb', line 119

def roles
  @roles ||= remote_user ? (local_roles + remote_user.roles).uniq : local_roles
end

#uuidObject

Return the uuid of a user Last part of identity_url, after path ‘user’ (element_name of RemoteUser params)



159
160
161
162
163
# File 'lib/disiid_user.rb', line 159

def uuid
  @uuid ||= begin
    /.*\/#{DisiidUser::RemoteUser.collection_name}\/([a-z0-9\-]+)$/.match(identity_url.to_s)[1]
  rescue; nil; end
end