class Accessly::PermittedActions::Query

Public Class Methods

new(actors, segment_id) click to toggle source
Calls superclass method Accessly::PermittedActions::Base::new
# File lib/accessly/permitted_actions/query.rb, line 7
def initialize(actors, segment_id)
  super(actors, segment_id)
end

Public Instance Methods

can?(action_id, object_type) click to toggle source

Ask whether the actor has permission to perform action_id in the given namespace. Multiple actions can have the same id as long as their namespace is different. The namespace can be any String. We recommend using namespace to group a class of permissions, such as to group parts of a particular feature in your application.

Lookups are cached in the object to prevent redundant database calls.

@param action_id [Integer, Array<Integer>] The action or actions we're checking whether the actor has. If this is an array, then the check is ORed. @param object_type [String] The namespace of the given action_id. @return [Boolean] Returns true if actor has been granted the permission, false otherwise.

@example

# Can the user perform the action with id 3 for posts?
Accessly::Query.new(user).can?(3, Post)

@example

# Can the user perform the action with id 3 for posts on segment 1?
Accessly::Query.new(user).on_segment(1).can?(3, Post)
# File lib/accessly/permitted_actions/query.rb, line 30
def can?(action_id, object_type)
  find_or_set_value(action_id, object_type) do
    Accessly::QueryBuilder.with_actors(Accessly::PermittedAction, @actors)
      .where(
        segment_id: @segment_id,
        action: action_id,
        object_type: String(object_type),
      ).exists?
  end
end