Module: Rarbac::UserHelper

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

Overview

A collection of useful functions to be used by the user model.

include Rarbac::UserHelper

That’s it! Now you have access to some awesome RBAC helper functions.

Instance Method Summary collapse

Instance Method Details

#has_permission?(action) ⇒ true|false

Determines if the user has permission to a given action. If the action does not exist at all, it is assumed the action is publicly available.

Parameters:

  • action (String)

    name of the action to check permission for.

Returns:

  • (true|false)

    true if the user has at least one role linked to it with permission to the given action.



33
34
35
36
# File 'app/helpers/rarbac/user_helper.rb', line 33

def has_permission?(action)
  return true if Action.where(name: action).count == 0
  actions.where(name: action).count > 0
end

#has_role?(*args) ⇒ true|false

Determines if the user has one or more of the given roles linked to it.

Parameters:

  • args (String)

    an argument list of one or more roles to check for.

Returns:

  • (true|false)

    true if the user is linked to at least one of the given roles.



13
14
15
16
# File 'app/helpers/rarbac/user_helper.rb', line 13

def has_role?(*args)
  throw Exception.new("Must supply at least one role.") if args.empty?
  roles.where(name: args).count > 0
end

#has_roles?(*args) ⇒ true|false

Determines if the user has all of the given roles linked to it.

Parameters:

  • args (String)

    an argument list of one or more roles to check for.

Returns:

  • (true|false)

    true if the user is linked to all of the given roles.



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

def has_roles?(*args)
  throw Exception.new("Must supply at least one role.") if args.empty?
  roles.where(name: args).count == args.count
end