Laravel Packages

DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel

Published
DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel image

The DirectoryTree Authorization package by Steve Bauman is an easy, native role and permission management system for Laravel.

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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Pause All Laravel Queues During a Deploy image

Pause All Laravel Queues During a Deploy

Read article
Laravel Terminal UI for the artisan dev Command image

Laravel Terminal UI for the artisan dev Command

Read article
Pause All Queues and a New artisan dev UI in Laravel 13.25 image

Pause All Queues and a New artisan dev UI in Laravel 13.25

Read article
Laravel monitoring that doesn't bill you by your traffic image

Laravel monitoring that doesn't bill you by your traffic

Read article
Mock PHP Classes in Tests With the Double Library image

Mock PHP Classes in Tests With the Double Library

Read article
Laravel Discount: Coupon Codes, Usage Limits, and Stacking image

Laravel Discount: Coupon Codes, Usage Limits, and Stacking

Read article