darvinstudio/darvin-user-bundle
Installation Add the bundle via Composer:
composer require darvinstudio/darvin-user-bundle
Enable the bundle in config/bundles.php:
DarvinStudio\UserBundle\DarvinUserBundle::class => ['all' => true],
Database Setup
Run migrations (if provided) or manually create tables based on the bundle’s schema (e.g., users, roles, permissions). Example structure:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Basic Usage Register a user via the bundle’s service or manually:
use DarvinStudio\UserBundle\Entity\User;
use DarvinStudio\UserBundle\Service\UserManager;
$userManager = $this->container->get('darvin_user.user_manager');
$user = new User();
$user->setEmail('user@example.com');
$user->setPassword('plaintext_password'); // Handled by encoder
$userManager->createUser($user);
Authentication Configure Symfony’s security.yaml to use the bundle’s user provider:
security:
providers:
darvin_user_provider:
id: darvin_user.user_provider
User CRUD
Use the UserManager service for all user operations:
// Create
$userManager->createUser($user);
// Update
$userManager->updateUser($user);
// Delete (soft/hard)
$userManager->deleteUser($user->getId());
Role/Permission Management Attach roles to users:
$user->addRole('ROLE_ADMIN');
$userManager->updateUser($user);
Check permissions in controllers:
$this->denyAccessUnlessGranted('ROLE_EDITOR', $user);
Event Listeners
Subscribe to bundle events (e.g., UserCreatedEvent) for post-actions:
# config/services.yaml
services:
App\EventListener\UserListener:
tags:
- { name: kernel.event_listener, event: darvin_user.user_created, method: onUserCreated }
API Integration Expose endpoints via Symfony’s serializer:
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
$normalizer = new ObjectNormalizer();
$userData = $normalizer->normalize($user);
Custom User Entity: Extend the bundle’s User entity to add fields:
use DarvinStudio\UserBundle\Entity\User as BaseUser;
class AppUser extends BaseUser {
private $customField;
}
Update config/packages/darvin_user.yaml to point to your entity.
Password Reset: Use the bundle’s PasswordResetManager for token-based resets:
$token = $passwordResetManager->generateResetToken($user);
Multi-Tenancy: Override the UserProvider to filter users by tenant ID.
Outdated Releases
Missing Documentation
UserManager internals) lack examples. Use php app/console debug:container DarvinUserBundle to inspect services.Hardcoded Table Names
# config/packages/darvin_user.yaml
darvin_user:
db_driver: doctrine
user_table: app_users
Security Gaps
firewall config:
security:
firewalls:
main:
pattern: ^/
form_login:
check_path: /login_check
enable_csrf: true
logout: true
max_attempts: 5
# config/packages/monolog.yaml
handlers:
darvin_user:
type: stream
path: "%kernel.logs_dir%/darvin_user.log"
level: debug
public function onUserCreated(UserCreatedEvent $event) {
dump($event->getUser());
}
Custom Validators
Add constraints to the User entity:
use Symfony\Component\Validator\Constraints as Assert;
class AppUser extends BaseUser {
/**
* @Assert\Length(min=8)
*/
private $password;
}
Override Templates
The bundle uses Twig templates for emails/reset pages. Override them in templates/DarvinUserBundle/:
{# templates/DarvinUserBundle/Reset/reset.html.twig #}
<h1>Custom Reset Page</h1>
API Resources
Extend the bundle’s UserResource (if using API Platform):
use DarvinStudio\UserBundle\ApiResource\UserResource as BaseUserResource;
class AppUserResource extends BaseUserResource {
public function getOperations(EntityManagerInterface $em) {
$reflection = new \ReflectionClass($this);
return [
// Add custom operations
];
}
}
How can I help you explore Laravel packages today?