Module: UserHelper

Defined in:
lib/generators/templates/helpers/user_helper.rb

Overview

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.



29
30
31
32
# File 'lib/generators/templates/helpers/user_helper.rb', line 29

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.



9
10
11
12
# File 'lib/generators/templates/helpers/user_helper.rb', line 9

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.



18
19
20
21
# File 'lib/generators/templates/helpers/user_helper.rb', line 18

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