Weave Code
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

Credential Bundle Laravel Package

2lenet/credential-bundle

CredentialBundle is a Symfony bundle that manages credentials for complex apps by simplifying the association of user groups and roles. Includes dashboard UI, routes integration, Doctrine migrations, CLI commands, and optional remote repository integration.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Use

  1. Install the Package

    composer require 2lenet/credential-bundle
    

    Note: If using Laravel, ensure Doctrine ORM is installed (doctrine/orm) or use a bridge like laravel-doctrine.

  2. Configure Routes Add to config/routes/credential.yaml (or routes/web.php in Laravel):

    credential:
        resource: '@LleCredentialBundle/Resources/config/routes.yaml'
    

    Laravel equivalent: Manually define routes in web.php or use a package like spatie/laravel-package-tools to auto-load Symfony-style routes.

  3. Run Migrations

    php bin/console make:migration
    php bin/console doctrine:migrations:migrate
    

    Laravel: Use php artisan migrate after adapting the migration files.

  4. Initialize Remote Repository (Optional) Configure config/packages/lle_credential.yaml:

    lle_credential:
        client_url: http://your-repo-api
        client_public_url: https://your-repo.com
        project_code: YOUR_PROJECT
        project_token: your_token_here
    
  5. Generate Permissions for an Entity Use the warmup command to auto-generate CRUD permissions for a Doctrine entity:

    php bin/console lle:credential:warmup App\Entity\YourEntity
    

    Laravel: Ensure the entity is registered with Doctrine or use a custom command to replicate this logic.


First Use Case: Assigning Group-Based Permissions

  1. Create a Group and Role Use the admin dashboard (linked in the README screenshot) or manually via Doctrine:

    // Example using Doctrine (Laravel equivalent would use Eloquent)
    $group = new Group();
    $group->setName('Admin');
    $role = new Role();
    $role->setName('Super Admin');
    $group->addRole($role);
    $em->persist($group);
    $em->flush();
    
  2. Assign Permissions to a Group Use the warmup command to generate permissions for an entity (e.g., Product):

    php bin/console lle:credential:warmup App\Entity\Product
    

    Then assign the generated permissions (e.g., product_list, product_create) to the Admin group via the dashboard or CLI.

  3. Verify in Code Check permissions in a controller:

    use LleCredentialBundle\Security\Authorization\PermissionChecker;
    
    public function index(PermissionChecker $permissionChecker)
    {
        if (!$permissionChecker->hasPermission('product_list')) {
            throw new AccessDeniedException();
        }
        // ...
    }
    

Implementation Patterns

Core Workflows

1. Permission Generation (Warmup)

  • Use Case: Auto-generate CRUD permissions for entities (e.g., after adding a new User entity).
  • Pattern:
    # Generate permissions for all entities in a namespace
    php bin/console lle:credential:warmup --namespace="App\Entity"
    
    # Generate for a specific entity
    php bin/console lle:credential:warmup App\Entity\Post
    
  • Laravel Adaptation:
    • Extend the WarmupTrait or replicate its logic in a Laravel command.
    • Use Eloquent’s getTable() and getFillable() to infer CRUD actions.

2. Remote Repository Sync

  • Use Case: Keep permissions in sync across environments (e.g., dev/staging/prod).
  • Pattern:
    # Upload current permissions to remote repo
    php bin/console lle:credential:init
    
    # Pull latest permissions from remote repo
    php bin/console lle:credential:load
    
  • Integration Tips:
    • API Endpoint: Ensure your remote repo has an API endpoint to handle POST/PUT requests for credentials.
    • Webhook Trigger: Use Laravel’s queue:work to run lle:credential:load on repo updates.
    • Fallback: Cache permissions locally (e.g., Redis) if the remote repo is unavailable.

3. Group-Role-Permission Matrix

  • Use Case: Assign permissions to groups dynamically (e.g., "Editors" can post_update but not post_delete).
  • Pattern:
    // Assign a permission to a group (via Doctrine)
    $groupCredential = new GroupCredential();
    $groupCredential->setGroup($group);
    $groupCredential->setCredential($credential); // e.g., 'post_update'
    $groupCredential->setEnabled(true);
    $em->persist($groupCredential);
    
  • Laravel Equivalent:
    GroupCredential::create([
        'group_id' => $group->id,
        'credential_id' => $credential->id,
        'enabled' => true,
    ]);
    

4. Permission Checks in Controllers

  • Use Case: Gate access to routes/actions.
  • Pattern:
    use LleCredentialBundle\Security\Authorization\PermissionChecker;
    
    public function edit(PermissionChecker $permissionChecker, Post $post)
    {
        if (!$permissionChecker->hasPermission('post_update')) {
            abort(403);
        }
        // ...
    }
    
  • Laravel Middleware: Create middleware to wrap PermissionChecker:
    namespace App\Http\Middleware;
    
    use Closure;
    use LleCredentialBundle\Security\Authorization\PermissionChecker;
    
    class CheckPermission
    {
        public function __construct(private PermissionChecker $checker) {}
    
        public function handle($request, Closure $next, $permission)
        {
            if (!$this->checker->hasPermission($permission)) {
                abort(403);
            }
            return $next($request);
        }
    }
    
    Register in app/Http/Kernel.php:
    protected $routeMiddleware = [
        'permission' => \App\Http\Middleware\CheckPermission::class,
    ];
    
    Use in routes:
    Route::get('/posts/{post}/edit', function (Post $post) {
        // ...
    })->middleware('permission:post_update');
    

Advanced Patterns

1. Customizing Warmup Logic

  • Use Case: Extend auto-generated permissions (e.g., add publish action to Post).
  • Pattern: Override the WarmupTrait or create a custom command:
    namespace App\Command;
    
    use LleCredentialBundle\Command\WarmupTrait;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    
    class CustomWarmup extends Command
    {
        use WarmupTrait;
    
        protected function configure()
        {
            $this->setName('app:credential:warmup');
        }
    
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $this->warmup($input, $output, function ($entity) {
                // Add custom permissions
                if ($entity->getClass() === Post::class) {
                    $this->addPermission($entity, 'publish');
                }
            });
        }
    }
    

2. Exporting/Importing Permissions

  • Use Case: Backup or migrate permissions between environments.
  • Pattern:
    # Export permissions to a JSON file
    php bin/console lle:credential:dump > permissions.json
    
    # Import permissions from a JSON file
    php bin/console lle:credential:load --file=permissions.json
    
  • Laravel Adaptation: Use Laravel’s Artisan::call() to trigger the dump/load logic manually.

3. Event-Driven Permission Updates

  • Use Case: Update permissions dynamically (e.g., when a user role changes).
  • Pattern: Listen to Doctrine events or Symfony’s kernel events:
    // Example: Update permissions when a group is updated
    $eventManager->addEventListener(
        GroupsUpdatedEvent::class,
        function (GroupsUpdatedEvent $event) {
            $this->permissionService->syncGroupPermissions($event->getGroups());
        }
    );
    
    Laravel: Use Eloquent events or Laravel’s Observers.

Gotchas and Tips

Pitfalls

  1. Doctrine Dependency

    • Issue: The bundle assumes Doctrine ORM. Laravel’s Eloquent may not work out-of-the-box.
    • Fix: Use a Doctrine bridge (e.g., laravel-doctrine) or rewrite entity interactions to use Eloquent.
  2. Remote Repository Assumptions

    • Issue: The init and load commands assume a custom API endpoint. If your remote repo uses Laravel Sanctum/Passport, conflicts may arise.
    • Fix: Mock the remote API during development or use Laravel’s HTTP client to adapt requests.
  3. **Permission

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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor