Installation:
composer require beelab/user-bundle
Add to config/bundles.php:
BeeLab\UserBundle\BeeLabUserBundle::class => ['all' => true],
Database Migration:
Run the bundle’s schema update (check Resources/doc/index.md for exact command) or manually create the user table:
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
is_active BOOLEAN DEFAULT true,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
First Use Case:
security.yaml with BeeLab\UserBundle\Security\UserProvider as the user provider.BeeLab\UserBundle\Entity\UserManager to create users:
$user = $userManager->createUser('user@example.com', 'plainPassword');
$userManager->saveUser($user);
Authentication:
security.yaml to use the bundle’s user_provider:
security:
providers:
app_user_provider:
id: BeeLab\UserBundle\Security\UserProvider
BeeLab\UserBundle\Entity\User for custom fields if needed.User Management:
UserManager for all operations:
// Find user
$user = $userManager->findUserByEmail('user@example.com');
// Update user
$user->setActive(false);
$userManager->saveUser($user);
ImpersonateListener (if enabled) to switch users temporarily:
$this->get('security.token_storage')->setToken(
new UsernamePasswordToken($user, null, 'main', $user->getRoles())
);
Password Changes:
UserManager::changePassword($user, 'newPassword') with Symfony’s PasswordHasher (injected automatically).Event Listeners:
UserEvents (e.g., UserCreatedEvent) for post-save logic:
$dispatcher->addListener(UserEvents::USER_CREATED, function (UserCreatedEvent $event) {
// Send welcome email
});
User entity and override UserManager methods to handle new fields.Validator with constraints on the User entity (e.g., @Assert\Email).User entity with ApiPlatform or JMSSerializer (exclude sensitive fields like password).No Registration Flow:
UserManager::createUser() manually or integrate with BeeLabUserPasswordBundle for email validation.Password Hashing:
security.yaml hashes passwords with algorithm: auto:
security:
encoders:
BeeLab\UserBundle\Entity\User:
algorithm: auto
Doctrine Only:
Impersonation Quirks:
STORAGE_TOKEN in security.yaml:
security:
firewalls:
main:
anonymous: ~
form_login: ~
switch_user: { role: ROLE_ADMIN } # Enables impersonation
UserProvider is correctly configured in security.yaml and the user table exists.PasswordHasher is properly injected (Symfony 5+ auto-configures this).EventDispatcher is bound to the UserManager (check services.yaml for overrides).Custom User Provider:
BeeLab\UserBundle\Security\UserProvider to add logic (e.g., multi-table queries).Override User Entity:
User class extending BeeLab\UserBundle\Entity\User and update services.yaml:
services:
App\Entity\User:
parent: BeeLab\UserBundle\Entity\User
tags: ['beelab.user.user_class']
Add Fields:
User entity and update the UserManager to handle new fields in saveUser()/createUser().Custom Roles:
roles field to the User entity and update the UserProvider to load roles from the database.User entity to customize validation.is_active field controls login access. Set to false to disable users programmatically.How can I help you explore Laravel packages today?