cooolinho/symfony-security-bundle
Installation
composer require cooolinho/symfony-security-bundle
Verify the package appears in composer.json under require.
Configure .env
Add SMTP credentials for password reset/email functionality:
MAILER_DSN=smtp://user:pass@smtp.example.com:587
Update security.yaml
User entity:
security:
encoders:
App\Entity\User: # Replace with your User class if different
algorithm: auto
app_user_provider):
providers:
app_user_provider:
entity:
class: App\Entity\User # Replace with your User class
property: email # or 'username'
UserChecker and SecurityAuthenticator:
firewalls:
main:
provider: app_user_provider
user_checker: Cooolinho\Bundle\SecurityBundle\Security\UserChecker
custom_authenticator:
- Cooolinho\Bundle\SecurityBundle\Security\SecurityAuthenticator
Basic Role Hierarchy
Define roles in security.yaml:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
Access Control Restrict routes:
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
Bundle Configuration
Add cooolinho_security.yaml to config/packages/:
cooolinho_security:
# Minimal config (extend as needed)
role_hierarchy: ~
Password Reset Flow:
/reset-password (if the bundle exposes this route).UserChecker validates reset tokens (check Security\UserChecker for logic).SecurityAuthenticator to handle token-based authentication.Authentication
SecurityAuthenticator to add logic (e.g., 2FA, IP checks).
use Cooolinho\Bundle\SecurityBundle\Security\SecurityAuthenticator as BaseAuthenticator;
class CustomAuthenticator extends BaseAuthenticator {
public function supports(Request $request) { ... }
public function authenticate(TokenInterface $token) { ... }
}
security.yaml:
custom_authenticator:
- App\Security\CustomAuthenticator
User Providers
property: email or property: username in the provider to switch authentication fields.User entity implements Symfony\Component\Security\Core\User\UserInterface.Role-Based Access
role_hierarchy to simplify role checks (e.g., ROLE_ADMIN inherits ROLE_USER).Email Integration
reset_password.html.twig) in templates/cooolinho_security/.SecurityBundleEvents::PASSWORD_RESET to customize reset logic.UserType for registration/login forms:
use Cooolinho\Bundle\SecurityBundle\Form\UserType;
$builder->add('user', UserType::class);
User entity has password and roles fields (standard Symfony security requirements).SecurityAuthenticator and UserChecker in PHPUnit tests:
$this->container->set('security.user_checker', $mockChecker);
Missing Encoder Configuration
Invalid password hash or Bad credentials.User entity’s password field uses algorithm: auto in security.yaml.Provider Mismatch
User not found during login.class and property in the provider match your User entity (e.g., property: email vs. username).Firewall Misconfiguration
provider matches the provider name in security.yaml.Role Hierarchy Overrides
ROLE_ prefix consistently (e.g., ROLE_ADMIN, not admin).Email Delivery Failures
MAILER_DSN in .env and check SMTP server logs.APP_DEBUG=true) and check var/log/dev.log for security events.SecurityAuthenticator by dumping the TokenInterface:
dump($token->getCredentials());
Custom User Checker
Extend UserChecker to add logic (e.g., account expiration):
class CustomUserChecker extends \Cooolinho\Bundle\SecurityBundle\Security\UserChecker {
public function checkPreAuth(UserInterface $user) {
if ($user->isExpired()) {
throw new AccountExpiredException();
}
}
}
Register in services.yaml:
services:
App\Security\CustomUserChecker:
tags: [security.user_checker]
Event Listeners
Subscribe to bundle events (e.g., PASSWORD_RESET):
use Cooolinho\Bundle\SecurityBundle\Event\SecurityEvents;
$eventDispatcher->addListener(SecurityEvents::PASSWORD_RESET, function ($event) {
// Custom logic (e.g., log reset, send SMS)
});
Override Templates
Copy default templates from vendor/cooolinho/symfony-security-bundle/Resources/views/ to templates/cooolinho_security/ to customize:
login.html.twigreset_password.html.twigConfiguration Overrides
Extend cooolinho_security.yaml:
cooolinho_security:
role_hierarchy:
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# Custom settings
max_password_attempts: 5
algorithm: auto fails, explicitly set algorithm: bcrypt or argon2i.access_control are case-sensitive (ROLE_ADMIN ≠ role_admin).How can I help you explore Laravel packages today?