Module: Rabarber::HasRoles

Extended by:
ActiveSupport::Concern
Defined in:
lib/rabarber/models/concerns/has_roles.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.roleable_classObject



75
76
77
# File 'lib/rabarber/models/concerns/has_roles.rb', line 75

def roleable_class
  @@included.constantize
end

Instance Method Details

#assign_roles(*role_names, context: nil, create_new: true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rabarber/models/concerns/has_roles.rb', line 27

def assign_roles(*role_names, context: nil, create_new: true)
  processed_role_names = process_role_names(role_names)
  processed_context = process_context(context)

  create_new_roles(processed_role_names, context: processed_context) if create_new

  roles_to_assign = Rabarber::Role.where(
    name: (processed_role_names - rabarber_roles.names(context: processed_context)), **processed_context
  )

  if roles_to_assign.any?
    delete_roleable_cache(context: processed_context)
    rabarber_roles << roles_to_assign

    Rabarber::Audit::Events::RolesAssigned.trigger(
      self,
      roles_to_assign: roles_to_assign.names(context: processed_context),
      current_roles: roles(context: processed_context),
      context: processed_context
    )
  end

  roles(context: processed_context)
end

#has_role?(*role_names, context: nil) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/rabarber/models/concerns/has_roles.rb', line 22

def has_role?(*role_names, context: nil)
  processed_context = process_context(context)
  roles(context: processed_context).intersection(process_role_names(role_names)).any?
end

#revoke_roles(*role_names, context: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rabarber/models/concerns/has_roles.rb', line 52

def revoke_roles(*role_names, context: nil)
  processed_role_names = process_role_names(role_names)
  processed_context = process_context(context)

  roles_to_revoke = Rabarber::Role.where(
    name: processed_role_names.intersection(roles(context: processed_context)), **processed_context
  )

  if roles_to_revoke.any?
    delete_roleable_cache(context: processed_context)
    self.rabarber_roles -= roles_to_revoke

    Rabarber::Audit::Events::RolesRevoked.trigger(
      self,
      roles_to_revoke: roles_to_revoke.names(context: processed_context),
      current_roles: roles(context: processed_context),
      context: processed_context
    )
  end

  roles(context: processed_context)
end

#roles(context: nil) ⇒ Object



17
18
19
20
# File 'lib/rabarber/models/concerns/has_roles.rb', line 17

def roles(context: nil)
  processed_context = process_context(context)
  Rabarber::Core::Cache.fetch(roleable_id, context: processed_context) { rabarber_roles.names(context: processed_context) }
end