Class: Accessly::Permission::Grant

Inherits:
Base
  • Object
show all
Defined in:
lib/accessly/permission/grant.rb

Instance Method Summary collapse

Methods inherited from Base

#on_segment

Constructor Details

#initialize(actor) ⇒ Grant

Create an instance of Accessly::Permission::Grant Pass in an ActiveRecord::Base for actor

Parameters:

  • actor (ActiveRecord::Base)

    The actor to grant permission



9
10
11
12
13
14
15
16
17
# File 'lib/accessly/permission/grant.rb', line 9

def initialize(actor)
  super(actor)
  @actor = case actor
  when ActiveRecord::Base
    actor
  else
    raise Accessly::GrantError.new("Actor is not an ActiveRecord::Base object")
  end
end

Instance Method Details

#grant!(action_id, object_type) ⇒ nil #grant!(action_id, object_type, object_id) ⇒ nil

Grant a permission to an actor.

Overloads:

  • #grant!(action_id, object_type) ⇒ nil

    Allow permission on a general action in the given namespace represented by object_type. A grant is universally unique and is enforced at the database level.

    Examples:

    # Allow the user access to posts for action id 3
    Accessly::Permission::Grant.new(user).grant!(3, "posts")
    # Allow the user access to posts for action id 3 on a segment
    Accessly::Permission::Grant.new(user).on_segment(1).grant!(3, "posts")

    Parameters:

    • action_id (Integer)

      The action to grant for the object

    • object_type (String)

      The namespace of the given action_id.

    Returns:

    • (nil)

      Returns nil if successful

    Raises:

  • #grant!(action_id, object_type, object_id) ⇒ nil

    Allow permission on an ActiveRecord object. A grant is universally unique and is enforced at the database level.

    Examples:

    # Allow the user access to Post 7 for action id 3
    Accessly::Permission::Grant.new(user).grant!(3, Post, 7)
    # Allow the user access to Post 7 for action id 3 on a segment
    Accessly::Permission::Grant.new(user).on_segment(1).grant!(3, Post, 7)

    Parameters:

    • action_id (Integer)

      The action to grant for the object

    • object_type (ActiveRecord::Base)

      The ActiveRecord model that receives a permission grant.

    • object_id (Integer)

      The id of the ActiveRecord object which receives a permission grant

    Returns:

    • (nil)

      Returns nil if successful

    Raises:

Returns:

  • (nil)


53
54
55
56
57
58
59
# File 'lib/accessly/permission/grant.rb', line 53

def grant!(action_id, object_type, object_id = nil)
  if object_id.nil?
    general_action_grant(action_id, object_type)
  else
    object_action_grant(action_id, object_type, object_id)
  end
end