Roles Read API

kristianmandrup edited this page Jan 5, 2011 · 2 revisions

valid_role?

Is the given role valid in the current Roles setup?

@admin_user.valid_role?(:guest)
User.valid_role?(:guest)

valid_roles

List the valid roles in the current Roles setup?

@admin_user.valid_roles.should include(:guest, :admin)
User.valid_roles.should include(:guest, :admin) 

valid_roles?

Are the given roles valid in the current Roles setup?

@admin_user.valid_roles?(:guest, :admin) 
User.valid_roles?(:guest, :admin)

has_role?

Does the user have the given role?

@admin_user.has_role?(:user)

has?

Does the user have the given role?

@admin_user.has?(:admin)

has_any_role?

Does the user have ANY of the given roles?

@admin_user.has_any_role?(:admin, :editor, :publisher)

has_roles?

Does the user have ALL the given roles?

@admin_user.has_roles?(:admin, :editor)

admin?

Does the user have the single role :admin and no other roles?

@admin_user.admin?

is?

Does the user have the single role given and no other roles?

@admin_user.is?(:admin)

roles_list

@admin_user.roles_list.should include(:admin)

roles

The method #roles can return either a single value or an error depending on whether the strategy allows for multiple roles and multiple roles have been assigned. Each role can either be an instance of the Role class (if the strategy uses this approach) or a string (for inline attribute strategies)

roles = @admin_user.roles
if roles.kind_of? Role
...
elsif roles.kind_of? Array
...
elsif roles == 'admin'
...
end