Class: Accessly::PermittedActions::Query

Inherits:
Base
  • Object
show all
Defined in:
lib/accessly/permitted_actions/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(actors, segment_id) ⇒ Query

Returns a new instance of Query.



7
8
9
# File 'lib/accessly/permitted_actions/query.rb', line 7

def initialize(actors, segment_id)
  super(actors, segment_id)
end

Instance Method Details

#can?(action_id, object_type) ⇒ Boolean

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.

Examples:

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

Parameters:

  • 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.

  • object_type (String)

    The namespace of the given action_id.

Returns:

  • (Boolean)

    Returns true if actor has been granted the permission, false otherwise.



30
31
32
33
34
35
36
37
38
39
# 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