Skip to content

Bug in has_any_role? implementation for new records #379

@AlexeyMK

Description

@AlexeyMK

From the Usage page, it should be possible to pass has_any_role? both the name of a global role, or a resource-specific role.

user.has_any_role? :admin, { :name => :moderator, :resource => Forum.first }, { :name => :visitor, :resource => Forum }

However, for a new record, when the {name: ..., resource: ...} form of a role is passed, has_any_role? passes the hash directly to has_role?.

    def has_any_role?(*args)
      if new_record?
        args.any? { |r| self.has_role?(r) }
      else
      # ...    
    end

has_role? is none-too-happy about this, expecting to get between one and two arguments.

def has_role?(role_name, resource = nil)
  # ...
end

For comparison, has_all_roles? gets this right.

def has_all_roles?(*args)
      args.each do |arg|
        if arg.is_a? Hash
          return false if !self.has_role?(arg[:name], arg[:resource])
        elsif arg.is_a?(String) || arg.is_a?(Symbol)
          return false if !self.has_role?(arg)
        else
     # ...
end

I've had this bug bite me locally and can confirm it is no fun.

Happy to submit a PR, but figured I'd get a report first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions