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.
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.
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.
Run Migrations
php bin/console make:migration
php bin/console doctrine:migrations:migrate
Laravel: Use php artisan migrate after adapting the migration files.
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
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.
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();
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.
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();
}
// ...
}
User entity).# 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
WarmupTrait or replicate its logic in a Laravel command.getTable() and getFillable() to infer CRUD actions.# Upload current permissions to remote repo
php bin/console lle:credential:init
# Pull latest permissions from remote repo
php bin/console lle:credential:load
POST/PUT requests for credentials.queue:work to run lle:credential:load on repo updates.post_update but not post_delete).// 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);
GroupCredential::create([
'group_id' => $group->id,
'credential_id' => $credential->id,
'enabled' => true,
]);
use LleCredentialBundle\Security\Authorization\PermissionChecker;
public function edit(PermissionChecker $permissionChecker, Post $post)
{
if (!$permissionChecker->hasPermission('post_update')) {
abort(403);
}
// ...
}
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');
publish action to Post).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');
}
});
}
}
# 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
Artisan::call() to trigger the dump/load logic manually.// 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.Doctrine Dependency
laravel-doctrine) or rewrite entity interactions to use Eloquent.Remote Repository Assumptions
init and load commands assume a custom API endpoint. If your remote repo uses Laravel Sanctum/Passport, conflicts may arise.**Permission
How can I help you explore Laravel packages today?