luizhenriqueferreira / laravel-acl
ACL package for Laravel
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: ^7.2.5
- illuminate/container: ^6.20|^7.0|^8.0
- illuminate/contracts: ^6.20|^7.0|^8.0
- illuminate/database: ^6.20|^7.0|^8.0
- illuminate/support: ^6.20|^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^3.8|^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
This package is auto-updated.
Last update: 2024-09-17 08:44:04 UTC
README
Laravel Acl is a PHP package for Laravel Framework, used for manipulation of access control list. Package is providing an easier way to control roles and permissions of users on your site.
Requirements
- PHP >=7.0
Install
- Type next command in your terminal:
composer require luizhenriqueferreira/LaravelAcl
- Add the service provider to your config/app.php file in section providers:
Laravel 5.5 uses Package Auto-Discovery, so does not require you to manually add the ServiceProvider.
'providers' => [ // ... LuizHenriqueFerreira\LaravelAcl\LaravelAclServiceProvider::class, // ... ],
- Run the migrations:
php artisan migrate
Usage
Use the following traits on your User model:
// ... use LuizHenriqueFerreira\LaravelAcl\Models\Traits\HasRoles; class User extends Authenticatable { use HasRoles; // ... Your User Model Code }
Using in code
Check role
if ($user->hasRoles'('admin')) { // User is admin } // or if ($user->hasRoles('admin', 'writer')) { // User is admin or writer } // or if ($user->hasRoles(['admin', 'writer'])) { // User is admin or writer }
Attach role
$user->attachRoles(1); //or $user->attachRoles('admin'); //or $user->attachRoles(Role::find(1)); //or $user->attachRoles(1, 2); //or $user->attachRoles('admin', 'writer'); //or $user->attachRoles(Role::find(1), Role::find(2)); //or $user->attachRoles(1, 'writer', Role::find(3)); //or $user->attachRoles([1]); //or $user->attachRoles(['admin']); //or $user->attachRoles([Role::find(1)]); //or $user->attachRoles([1, 2]); //or $user->attachRoles(['admin', 'writer']); //or $user->attachRoles([Role::find(1), Role::find(2)]); //or $user->attachRoles([1, 'writer', Role::find(3)]);
The same function, Detach role
$user->detachRoles('writer'); // ... $user->detachRoles(2, 'writer', Role::find(2)); // ... $user->detachRoles([2, 'writer', Role::find(2)]);
Clear all roles
$user->detachRoles();
Check permission
if ($user->hasPermissions('create-post')) { // User has permission "create post" } // or if ($user->hasPermissions('create-post', 'update-post')) { // User has permission "create post" or "update post" } // or if ($user->hasPermissions(['create-post', 'update-post'])) { // User has permission "create post" or "update post" }
Attach permissions
$role->attachPermissions(1); //or $role->attachPermissions('create-post'); //or $role->attachPermissions(Permission::find(1)); //or $role->attachPermissions(1, 2); //or $role->attachPermissions('create-post', 'update-post'); //or $role->attachPermissions(Permission::find(1), Permission::find(2)); //or $role->attachPermissions(1, 'update-post', Permission::find(3)); //or $role->attachPermissions([1]); //or $role->attachPermissions(['create-post']); //or $role->attachPermissions([Permission::find(1)]); //or $role->attachPermissions([1, 2]); //or $role->attachPermissions(['create-post', 'update-post']); //or $role->attachPermissions([Permission::find(1), Permission::find(2)]); //or $role->attachPermissions([1, 'update-post', Permission::find(3)]);
The same function, Detach permissions
$role->detachPermissions('create-post'); // ... $role->detachPermissions(1, 'update-post', Permission::find(3)); // ... $role->detachPermissions([1, 'update-post', Permission::find(3)]);
Clear all permissions
$role->detachPermissions();
See the code for more information... =)
Using blade directives
You also can use directives to verify the currently logged in user has any roles or permissions.
Check roles:
@hasroles('admin') <!-- User has role admin --> @elsehasrole('writer') <!-- User has role writer --> <!-- ... --> @else <!-- User with other roles --> @endrole
or check more roles in one directive:
@hasroles(['admin', 'writer']) <!-- User has next roles: admin, writer --> @endhasrole
Check permissions:
@can('create-post') <!-- User can create post --> @elsecan('edit-post') <!-- User can edit post --> @endcan
Using middlewares
You can use role middleware for check access to some routes
Route::middleware(['role:admin'])->group(function() { // Only for user with role admin Route::get('/admin', function() { // some code }); });
also you can use permission middleware
Route::middleware(['permission:create-post'])->group(function() { // Only for user with permission create post Route::get('/admin/post', function() { // some code }); });
or use role and permission middleware together
Route::middleware(['role:admin,moderator', 'permission:remove-post'])->group(function() { // Only for user with role moderator and with permission create post Route::get('/admin/post/remove', function() { // some code }); });
License
Laravel Acl package is licensed under the MIT License.