Laravel 7 Access Control brings a simple and light-weight role-based permissions system to Laravel's built in Auth system. Laravel Access Control brings support for the following ACL structure:
- Every user can have zero or more permissions.
- Every user can have zero or more roles.
- Every role can have zero or more permissions.
- Every role can have one special flags, all-access or no-access
-
composer require pierresilva/laravel-access-control
-
php artisan migrate
-
Add the
AccessControlTrait
trait to yourUser
model.
...
use pierresilva\AccessControl\Traits\AccessControlTrait;
class User extends Authenticatable
{
...
use AccessControlTrait;
...
}
AccessControl provides middleware that you may assign to specific routes in your application. To register, simply append the following middleware to your app/Http/Kernel.php file under the $routeMiddleware property.
'has.role' => \pierresilva\AccessControl\Middleware\UserHasRole::class,
'has.permission' => \pierresilva\AccessControl\Middleware\UserHasPermission::class,
AccessControl provides a facade to check user roles and permissions.
\AccessControl::userIs('role.slug')
\AccessControl::userIs(['role.slug.1', 'role.slug.2'])
\Accesscontrol::userCan('permission.slug')
\Accesscontrol::userCan(['permission.slug.1', 'permission.slug.2'])