Installation
composer require alexdpy/acl
Publish the config file (if needed):
php artisan vendor:publish --provider="Alexdpy\Acl\AclServiceProvider"
Basic ACL Definition
Define permissions in a config file (config/acl.php):
'permissions' => [
'admin' => ['create_user', 'edit_user', 'delete_user'],
'editor' => ['create_post', 'edit_post'],
'user' => ['view_profile'],
],
First Use Case: Check Permission
use Alexdpy\Acl\Facades\Acl;
if (Acl::check('create_user')) {
// User has permission
}
Assign Roles to Users
// In a User model or middleware
Acl::assignRole('admin', $userId);
Role Assignment
// Assign multiple roles
Acl::assignRole(['admin', 'editor'], $userId);
// Check role
if (Acl::hasRole('admin', $userId)) {
// Admin-specific logic
}
Permission Checks in Controllers
public function editUser(Request $request, $id) {
if (!Acl::check('edit_user')) {
abort(403, 'Unauthorized action.');
}
// Proceed with edit logic
}
Runtime Permission Checks
// Check if user has permission for a specific resource
if (Acl::check('edit_post', ['post_id' => $post->id])) {
// Allow edit
}
Custom Permission Logic
// Extend with custom logic (e.g., time-based permissions)
Acl::extend('publish_post', function ($userId, $params = []) {
return Acl::hasRole('editor', $userId) && now()->hour >= 9;
});
Middleware for Role-Based Access
namespace App\Http\Middleware;
use Closure;
use Alexdpy\Acl\Facades\Acl;
class CheckRole {
public function handle($request, Closure $next, $role) {
if (!Acl::hasRole($role, auth()->id())) {
abort(403);
}
return $next($request);
}
}
Register in app/Http/Kernel.php:
'role' => \App\Http\Middleware\CheckRole::class,
Policy Integration
use Alexdpy\Acl\Facades\Acl;
class PostPolicy {
public function update(User $user, Post $post) {
return Acl::check('edit_post', ['post_id' => $post->id]);
}
}
No Built-in Migration Support
acl_roles).Schema::create('acl_roles', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('role');
$table->timestamps();
});
Caching Quirks
php artisan cache:clear
Acl::clearCache();
No Fine-Grained Resource Ownership
public function update(User $user, Post $post) {
return $user->id === $post->user_id && Acl::check('edit_post');
}
Enable Logging
Add to config/acl.php:
'debug' => env('ACL_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Check Permission Logic
Use Acl::getPermissions() to inspect loaded permissions:
dd(Acl::getPermissions());
Custom Storage Backend Override the default storage by binding a custom service:
$this->app->bind('acl.storage', function () {
return new \App\Services\CustomAclStorage();
});
Event Hooks Listen for role assignment events (if supported in future versions) or extend the package to dispatch events:
// Example: Extend AclServiceProvider
public function boot() {
event(new RoleAssigned($userId, $role));
}
Localization Support
Add localized permission names in config/acl.php:
'permissions' => [
'admin' => [
'en' => ['create_user', 'edit_user'],
'es' => ['crear_usuario', 'editar_usuario'],
],
],
Then check dynamically:
$permission = Acl::translatePermission('create_user', app()->getLocale());
How can I help you explore Laravel packages today?