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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article