code-colliders/basic-user-bundle
Installation
composer require code-colliders/basic-user-bundle
Enable the bundle in config/bundles.php:
CodeColliders\BasicUserBundle\CodeCollidersBasicUserBundle::class => ['all' => true],
Initialize the Bundle
Run the command to generate configurations and a User entity:
php bin/console basic-user:init -c User
This creates:
User entity with required fields (email, password, roles).security.yaml, firewalls).First Use Case: User Registration
User entity (e.g., add custom fields via make:entity).UserRepository to interact with users:
$user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => 'test@example.com']);
@IsGranted("ROLE_USER")).Authentication Flow
security.yaml (configured by the bundle) with form login:
firewalls:
main:
form_login:
login_path: login
check_path: login
logout:
path: app_logout
target: home
User Management
User entity’s setter methods:
$user = new User();
$user->setEmail('user@example.com')
->setPassword($passwordEncoder->encodePassword($user, 'rawPassword'))
->setRoles(['ROLE_USER']);
$entityManager->persist($user);
$users = $userRepository->findBy(['role' => 'ROLE_ADMIN']);
Role-Based Access
$user->addRole('ROLE_EDITOR');
$entityManager->flush();
#[IsGranted('ROLE_ADMIN')]
public function adminDashboard(): Response
Custom User Fields: Extend the User entity after initialization:
php bin/console make:entity User
Add fields like fullName or avatar, then update migrations.
Password Reset: Integrate with Symfony’s PasswordResetToken or use a third-party bundle (e.g., symfonycasts/verify-email).
API Authentication: Use api firewall with JWT or stateless token authentication:
firewalls:
api:
pattern: ^/api
stateless: true
jwt: ~
Outdated Bundle
symfony/security-bundle + custom User entity).Missing Doctrine Migrations
basic-user:init command creates the User entity but does not generate migrations.php bin/console make:migration
php bin/console doctrine:migrations:migrate
Hardcoded Configurations
security.yaml or config/packages/security.yaml without warning.basic-user:init.No Built-in Password Reset
symfonycasts/verify-email or implement manually with PasswordResetToken.User Not Found Errors:
User entity implements UserInterface and EquatableInterface from Symfony\Component\Security\Core\User.loadUserByUsername() method in your custom UserProvider (if overridden).Role Assignment Issues:
['ROLE_USER']), not strings.ROLES property in the User entity is typed as array.Custom User Provider
Override the default provider in security.yaml:
providers:
app_users:
entity:
class: App\Entity\User
property: email
Event Listeners
Subscribe to security events (e.g., AuthenticationSuccess):
// src/EventListener/LoginListener.php
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
$user = $event->getUser();
// Custom logic (e.g., log activity)
}
Register in services.yaml:
services:
App\EventListener\LoginListener:
tags:
- { name: 'kernel.event_listener', event: 'security.authentication.success' }
API Tokens
For API auth, pair with lexik/jwt-authentication-bundle:
composer require lexik/jwt-authentication-bundle
Configure in config/packages/security.yaml:
firewalls:
api:
pattern: ^/api
stateless: true
jwt: ~
How can I help you explore Laravel packages today?