-
Notifications
You must be signed in to change notification settings - Fork 217
Validate Permissions and Roles
maxinminax edited this page Nov 25, 2021
·
9 revisions
Roles can be validated by calling hasRole
method.
Validate based on User.
Laravel 5.3 uses
hasRole()
instead ofis()
.
Method is()/hasRole() supports comma for AND and pipe as OR operator. Or you can pass operator as a second param.
// laravel 5.3 uses hasRole()
$user = User::first();
$user->hasRole('administrator');
$user->hasRoleAdministrator(); // using method
$user->hasRole('administrator|moderator');
// laravel < 5.3
$user = User::first();
$user->is('administrator');
$user->isAdministrator(); // using method
// using pipe as OR operator
// any one role allowed will result to true.
$user->is('administrator|moderator');
// using comma as AND operator
// all roles defined must be allowed to result true.
$user->is('administrator,moderator');
// or pass second param as operator
$user->is(array('administrator', 'moderator'), 'or');
Permissions can be validated by calling hasPermission
method.
Validate based on Role.
Method hasPermission() supports comma for AND and pipe as OR operator. Or you can pass operator as a second param.
$admin = Role::first(); // administrator
$admin->hasPermission('view.user');
// by an array
$admin->hasPermission(array('view.user', 'edit.user'));
// by an array with or operator
$admin->hasPermission(array('view.user', 'edit.user'), 'or');
// using pipe as OR operator
// any allowed permission will result to true.
$admin->hasPermission('view.user|edit.user|view.admin|delete.admin');
// using comma as AND operator
// all permissions defined must be allowed to result true.
$admin->hasPermission('view.user,edit.user,view.admin,delete.admin');
Validate based on User
$user = User::first();
$user->can('delete.user');