Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Acl Laravel Package

alexdpy/acl

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require alexdpy/acl
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Alexdpy\Acl\AclServiceProvider"
    
  2. 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'],
    ],
    
  3. First Use Case: Check Permission

    use Alexdpy\Acl\Facades\Acl;
    
    if (Acl::check('create_user')) {
        // User has permission
    }
    
  4. Assign Roles to Users

    // In a User model or middleware
    Acl::assignRole('admin', $userId);
    

Implementation Patterns

Role-Based Workflows

  • 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
    }
    

Dynamic Permissions

  • 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;
    });
    

Integration with Laravel Features

  • 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]);
        }
    }
    

Gotchas and Tips

Pitfalls

  • No Built-in Migration Support

    • Roles and permissions are stored in memory by default. For persistence, extend the package or use a database table (e.g., acl_roles).
    • Example migration:
      Schema::create('acl_roles', function (Blueprint $table) {
          $table->id();
          $table->unsignedBigInteger('user_id');
          $table->string('role');
          $table->timestamps();
      });
      
  • Caching Quirks

    • Permissions are cached by default. Clear cache after role changes:
      php artisan cache:clear
      
    • Or manually:
      Acl::clearCache();
      
  • No Fine-Grained Resource Ownership

    • The package lacks built-in support for checking ownership (e.g., "can edit own posts"). Implement this in policies or middleware:
      public function update(User $user, Post $post) {
          return $user->id === $post->user_id && Acl::check('edit_post');
      }
      

Debugging Tips

  • 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());
    

Extension Points

  • 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());
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel