aef / hypervel-permission
Role & Permission management for Hypervel, with cache support
dev-master
2025-04-23 18:19 UTC
Requires
- php: >=8.0
- hypervel/framework: ^0.1.5
This package is auto-updated.
Last update: 2025-04-23 18:34:42 UTC
README
Role & Permission management for the Hypervel Framework, with first-class cache support.
Effortlessly assign roles and permissions to your Hypervel users, speed-boosted by smart caching.
Table of Contents
Features
- ๐ Create roles & permissions via models or artisan commands
- ๐งโ๐คโ๐ง Assign multiple roles to users (or vice versa)
- ๐ Authorize via roles or direct permissions
- ๐ Cache layer with configurable store, key & TTL
- ๐ก Multi-guard support out of the box
- ๐ Clean, expressive API (
hasRole()
,hasPermissionTo()
, โฆ) - ๐งช Thoroughly tested & production-ready
Installation
composer require aef/hypervel-permission
Configuration
Publish the config file and the migrations:
php artisan vendor:publish permission-config php artisan vendor:publish permission-migrations
In the providers array, add the following line:
'providers' => [ // Other service providers... Hypervel\Permission\PermissionServiceProvider::class, ],
Basic Usage
Creating permissions & roles
use Hypervel\Permission\Models\Permission; use Hypervel\Permission\Models\Role; Permission::create(['name' => 'edit articles']); Permission::create(['name' => 'delete articles']); $writer = Role::create(['name' => 'writer']); $writer->givePermissionTo('edit articles', 'delete articles'); $admin = Role::create(['name' => 'admin']); $admin->givePermissionTo(Permission::all());
Assigning roles / permissions to users
$user = \App\Models\User::find(1); $user->assignRole('writer', 'admin'); $user->syncRoles(['writer', 'admin']); $user->givePermissionTo('publish articles'); $user->syncPermissions(['edit articles', 'delete articles']);
Checking abilities
$user->hasPermissionTo('edit articles'); // true/false $user->hasRole('writer'); // true/false $user->hasAnyRole(['writer', 'moderator']); // true/false $user->hasAllPermissions(['edit', 'publish']); // true/false
Check if the current user has permission
Gate::allows('edit articles')
Getting all permissions
$user->getAllPermissions(); // direct + via roles $user->getPermissionsViaRoles(); // only via roles $user->permissions; // only direct
Advanced Usage
Caching is on by default and automatically flushed whenever a permission, role, or related pivot changes.
'cache' => [ 'store' => env('PERMISSION_CACHE_STORE', 'redis'), 'key' => env('PERMISSION_CACHE_KEY', 'hypervel.permission.cache'), 'expiration' => env('PERMISSION_CACHE_TTL', 3600), // seconds ],
Multiple guards
// creating Permission::create(['name' => 'edit articles', 'guard_name' => 'api']); Role::create(['name' => 'writer', 'guard_name' => 'api']); // checking (guard auto-detected from current auth) $user->hasPermissionTo('edit articles'); // defaults to current guard $user->hasPermissionTo('edit articles', 'api'); // explicit