Skip to content

Instantly share code, notes, and snippets.

@cesurapp
Created November 30, 2019 19:17
Show Gist options
  • Save cesurapp/959f56ddc9a9fe3aee365c6ba3e7653f to your computer and use it in GitHub Desktop.
Save cesurapp/959f56ddc9a9fe3aee365c6ba3e7653f to your computer and use it in GitHub Desktop.
Symfony 4.4 or 5.x Override AuthorizationChecker
<?php
# src/Security/AuthorizationChecker.php
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
class AuthorizationChecker implements AuthorizationCheckerInterface
{
private $tokenStorage;
private $accessDecisionManager;
private $authenticationManager;
private $alwaysAuthenticate;
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, AccessDecisionManagerInterface $accessDecisionManager, bool $alwaysAuthenticate = false)
{
$this->tokenStorage = $tokenStorage;
$this->authenticationManager = $authenticationManager;
$this->accessDecisionManager = $accessDecisionManager;
$this->alwaysAuthenticate = $alwaysAuthenticate;
}
/**
* {@inheritdoc}
*
* @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token
*/
final public function isGranted($attributes, $subject = null): bool
{
if (null === ($token = $this->tokenStorage->getToken())) {
throw new AuthenticationCredentialsNotFoundException('The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.');
}
if ($this->alwaysAuthenticate || !$token->isAuthenticated()) {
$this->tokenStorage->setToken($token = $this->authenticationManager->authenticate($token));
}
if (!\is_array($attributes)) {
$attributes = [$attributes];
}
return $this->accessDecisionManager->decide($token, $attributes, $subject);
}
}
<?php
# src/DependencyInjection/AuthorizationCheckerOverride.php
namespace App\DependencyInjection;
use App\Security\AuthorizationChecker;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Override AuthorizationChecker
*/
class AuthorizationCheckerOverride implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$checker = $container->getDefinition('security.authorization_checker');
$checker->setClass(AuthorizationChecker::class);
$checker->setAutowired(true);
}
}
<?php
# src/Kernel.php
use App\DependencyInjection\AuthorizationCheckerOverride;
class Kernel extends BaseKernel
{
protected function build(ContainerBuilder $container)
{
$container->addCompilerPass(new AuthorizationCheckerOverride());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment