Skip to content

Checking Permissions

Generally speaking, you should not have a need to check roles directly. It is better to allow a role certain abilities, then check for those abilities instead. If what you need is very general, you can create very broad abilities. For example, an access-dashboard ability is always better than checking for admin or editor roles directly.

For the rare occasion that you do want to check a role, that functionality is available.

Warden can check if a user has a specific role:

Warden::is($user)->a('moderator');

If the role you’re checking starts with a vowel, you might want to use the an alias method:

Warden::is($user)->an('admin');

You can check if a user doesn’t have a specific role:

Warden::is($user)->notA('moderator');
Warden::is($user)->notAn('admin');

Check if a user has one of many roles:

Warden::is($user)->a('moderator', 'editor');

Check if the user has all of the given roles:

Warden::is($user)->all('editor', 'moderator');

Check if a user has none of the given roles:

Warden::is($user)->notAn('editor', 'moderator');

These checks can also be done directly on the user:

$user->isAn('admin');
$user->isA('subscriber');
$user->isNotAn('admin');
$user->isNotA('subscriber');
$user->isAll('editor', 'moderator');

Get all roles for a user:

$roles = $user->getRoles();

Get all abilities for a user:

$abilities = $user->getAbilities();

This will return a collection of the user’s allowed abilities, including any abilities granted to the user through their roles.

You can also get a list of abilities that have been explicitly forbidden:

$forbiddenAbilities = $user->getForbiddenAbilities();

Check if user is an admin:

if (Warden::is($user)->an('admin')) {
// User is an admin
}

Check if user is not a subscriber:

if ($user->isNotA('subscriber')) {
// User is not a subscriber
}

Check if user has multiple roles:

if ($user->isAll('editor', 'moderator')) {
// User has both editor and moderator roles
}

Get all of a user’s abilities:

$abilities = $user->getAbilities();
foreach ($abilities as $ability) {
echo $ability->name;
}