Module: Rarbac::ApplicationHelper

Defined in:
app/helpers/rarbac/application_helper.rb

Overview

Defines application helpers, useful in controllers to add RBAC filters.

Instance Method Summary collapse

Instance Method Details

#ensure_permission!(action = nil, &block) ⇒ Object

Ensure that ‘current_user` has permission to a given action. If no action name is supplied, this will instead use the currently-executing controller and action names. If no block is given, a failed check will render a header-only response with status code 403 (Forbidden).

Parameters:

  • action (String) (defaults to: nil)

    name of the action to check permissions for.

  • block (Proc)

    if given, invoked with a single parameter, which is the result of the role check.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/rarbac/application_helper.rb', line 34

def ensure_permission!(action=nil, &block)
  unless action
    klass = self.class.name.underscore.sub("_controller", "")
    action = "#{klass}##{action_name}"
  end

  # If the action doesn't exist, don't worry about the current_user
  if Action.where(name: action).count == 0
    maybe_render(true, block)
  else
    has_something?(:has_permission?, action, block)
  end
end

#ensure_role!(*args, &block) ⇒ Object

Ensure that ‘current_user` has at least one of the given roles. If no block is given, a failed check will render a header-only response with status code 403 (Forbidden).

Parameters:

  • args (Array<String>)

    an argument list of roles.

  • block (Proc)

    if given, invoked with a single parameter, which is the result of the role check.



11
12
13
# File 'app/helpers/rarbac/application_helper.rb', line 11

def ensure_role!(*args, &block)
  has_something?(:has_role?, args, block)
end

#ensure_roles!(*args, &block) ⇒ Object

Ensure that ‘current_user` has all of the given roles. If no block is given, a failed check will render a header-only response with status code 403 (Forbidden).

Parameters:

  • args (Array<String>)

    an argument list of roles.

  • block (Proc)

    if given, invoked with a single parameter, which is the result of the role check.



22
23
24
# File 'app/helpers/rarbac/application_helper.rb', line 22

def ensure_roles!(*args, &block)
  has_something?(:has_roles?, args, block)
end