module UserHelper

Public Instance Methods

has_permission?(action) click to toggle source

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.

@param [String] action name of the action to check permission for. @return [true|false] true if the user has at least one role linked to it

with permission to the given action.
# 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) click to toggle source

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

@param [String] args an argument list of one or more roles to check for. @return [true|false] true if the user is linked to at least one of the

given roles.
# 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) click to toggle source

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

@param [String] args an argument list of one or more roles to check for. @return [true|false] true if the user is linked to all of the given roles.

# 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