Class: Permisi::Backend::ActiveRecord::Actor

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/permisi/backend/active_record/actor.rb

Defined Under Namespace

Modules: ActorRolesCollectionProxy

Instance Method Summary collapse

Instance Method Details

#aggregate_permissionsObject

Aggregate permissions from all roles an actor plays



31
32
33
34
35
36
37
# File 'lib/permisi/backend/active_record/actor.rb', line 31

def aggregate_permissions
  roles.load.inject(HashWithIndifferentAccess.new) do |aggregate, role|
    aggregate.deep_merge(role.permissions) do |_key, effect, another_effect|
      effect == true || another_effect == true
    end
  end
end

#may_i?(action_path) ⇒ Boolean Also known as: may?

Returns:

  • (Boolean)


21
22
23
# File 'lib/permisi/backend/active_record/actor.rb', line 21

def may_i?(action_path)
  PermissionUtil.allows?(permissions, action_path)
end

#permissionsObject

Memoized and cached actor permissions



26
27
28
# File 'lib/permisi/backend/active_record/actor.rb', line 26

def permissions
  @permissions ||= Permisi.config.cache_store.fetch("#{cache_key}-p") { aggregate_permissions }
end

#reset_permissionsObject



39
40
41
# File 'lib/permisi/backend/active_record/actor.rb', line 39

def reset_permissions
  @permissions = nil
end

#role?(role_slug) ⇒ Boolean Also known as: has_role?

Returns:

  • (Boolean)


17
18
19
# File 'lib/permisi/backend/active_record/actor.rb', line 17

def role?(role_slug)
  roles.load.any? { |role| role.slug == role_slug.to_s }
end

#rolesObject



13
14
15
# File 'lib/permisi/backend/active_record/actor.rb', line 13

def roles
  super.extend(ActorRolesCollectionProxy)
end