Module: Authegy::Authorizable

Extended by:
ActiveSupport::Concern
Included in:
Person
Defined in:
lib/authegy/authorizable.rb

Instance Method Summary collapse

Instance Method Details

#assign_role(role_name, resource_type_or_instance = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/authegy/authorizable.rb', line 19

def assign_role(role_name, resource_type_or_instance = nil)
  assignment_attributes = {
    role: ::Role.find_or_create_by(name: role_name)
  }

  if resource_type_or_instance.present?
    assignment_attributes.merge! Authegy
      .extract_resource_attributes(resource_type_or_instance)
  end

  role_assignments.find_or_create_by assignment_attributes
end

#has_role?(role_name, resource_type_or_instance = nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/authegy/authorizable.rb', line 32

def has_role?(role_name, resource_type_or_instance = nil)
  matching_attributes = { role: role_name.to_s }

  return role_assignment_list.select do |assignment|
    assignment[:role] == matching_attributes[:role]
  end.any? if resource_type_or_instance.blank?

  matching_attributes.merge! Authegy
      .extract_resource_attributes(resource_type_or_instance)

  role_assignment_list.select do |assignment|
    assignment == matching_attributes
  end.any?
end