easycorp/easy-security-bundle
DEPRECATED/UNMAINTAINED: Symfony 3.4 includes similar features. EasySecurityBundle adds a “security” service with shortcuts for common Symfony Security tasks (get current user, check roles, login errors) to reduce complexity and verbosity.
Installation:
composer require easycorp/easy-security-bundle
(Note: While this package is for Symfony, Laravel developers can adapt its core security logic for similar use cases.)
Register the Service:
In Laravel, manually register the Security service in config/app.php under providers:
'providers' => [
// ...
EasyCorp\Bundle\EasySecurityBundle\Security\SecurityServiceProvider::class,
],
(Since Laravel doesn’t natively support Symfony bundles, this requires a custom wrapper or service class.)
First Use Case:
Inject the security service into a controller or service to simplify user checks:
use EasyCorp\Bundle\EasySecurityBundle\Security\Security;
class UserController extends Controller
{
public function __construct(private Security $security) {}
public function dashboard()
{
if ($this->security->isFullyAuthenticated()) {
return view('dashboard');
}
return redirect()->route('login');
}
}
User Authentication Shortcuts: Replace verbose Symfony checks with concise methods:
// Instead of:
$user = auth()->user();
$isAdmin = $user && $user->hasRole('ROLE_ADMIN');
// Use:
if ($this->security->isGranted('ROLE_ADMIN')) {
// Admin logic
}
Programmatic Login: Simplify manual token creation for API/auth flows:
$user = User::find(1);
$this->security->login($user); // Auto-handles token generation
Password Handling: Encode and validate passwords without manual hashing:
$encoded = $this->security->encodePassword('plaintext');
$isValid = $this->security->isPasswordValid('user_input', $user);
Laravel-Specific Adaptation:
Create a facade or helper class to bridge Symfony’s Security with Laravel’s Auth:
class LaravelSecurityFacade
{
public function isGranted($role)
{
return auth()->check() && auth()->user()->hasRole($role);
}
}
(Use this to avoid direct Symfony dependency.)
Event Listeners:
Hook into Laravel’s auth.attempting or auth.login events to extend the bundle’s logic:
Event::listen('auth.login', function ($user) {
$this->security->login($user); // Custom post-login logic
});
Testing:
Mock the Security service in PHPUnit:
$mockSecurity = Mockery::mock(EasyCorp\Bundle\EasySecurityBundle\Security\Security::class);
$mockSecurity->shouldReceive('isGranted')->andReturn(true);
$this->app->instance('security', $mockSecurity);
Symfony Dependency:
LaravelSecurityAdapter).TokenStorage, AuthorizationChecker).Auth methods for core logic and only adopt specific shortcuts (e.g., isFullyAuthenticated).Deprecated Methods:
addClassesToCompile (v1.0.4), which may break older Symfony integrations.Authentication State Confusion:
isAnonymous()/isRemembered() differ from Symfony’s defaults.dd($this->security->isAuthenticated(), auth()->check());
Login Errors: Check failed attempts with:
$error = $this->security->getLoginError();
logger($error); // Log to Laravel’s log channel
Role Hierarchy Issues:
Use hasRole() with explicit user objects to debug:
$user = User::find(1);
if (!$this->security->hasRole('ROLE_ADMIN', $user)) {
logger("User lacks role: " . json_encode($user->roles));
}
Custom Authentication Logic:
Extend the Security class to add Laravel-specific methods:
class ExtendedSecurity extends \EasyCorp\Bundle\EasySecurityBundle\Security\Security
{
public function checkApiToken($token)
{
return Hash::check($token, config('api.token'));
}
}
Override User Checks:
Replace isFullyAuthenticated() with Laravel’s auth()->viaRemember():
public function isFullyAuthenticated()
{
return auth()->check() && !auth()->viaRemember();
}
Password Encoding:
Use Laravel’s Hash facade instead of the bundle’s encoder:
$encoded = Hash::make('plaintext');
How can I help you explore Laravel packages today?