The DirectoryTree Authorization package by Steve Bauman is an easy, native role and permission management system for Laravel.
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>If you need a super simple native feeling Laravel Authorization package, this one may tickle your fancyhttps://t.co/Xiu4Ty0IkR
— Steve (@ste_bau) April 23, 2024
It works with Laravel's Gate and authorization methods out of the box, and offers the following lightweight API to manage roles and permissions:
use DirectoryTree\Authorization\Permission;
use DirectoryTree\Authorization\Role;
$createUsers = Permission::create([
'name' => 'users.create',
'label' => 'Create Users',
]);
$admin = Role::create([
'name' => 'administrator',
'label' => 'Admin',
]);
// Grant the permission to a role
$admin->permissions()->save($createUsers);
// Assign the role to a user
$user->roles()->save($admin);
// `can()` method usage in PHP:
Auth::user()->can('users.create');
// Using Laravel's `Gate`:
Gate::allows('users.create');
// Using Laravel's `@can()` directive:
@can('users.create')
<!-- This user can create other users. -->
@endcan
The above code snippet doesn't contain every method available—see the readme for usage details on managing roles and permissions with this package, which includes the following main features:
- Manage User roles and Permissions
- Create user-specific permissions
- Checking permissions and roles
- Caching permissions by default
- Use with Laravel's native Gate and authorization methods.
- Permissions are registered in Laravel's Gate by default
- Includes useful permission and role middleware
To get started with this package, check out package on GitHub at directorytree/authorization.
The post DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.