<?php
/**
*
* (c) BonBonSlick
*
*/
/**
*
* (c) BonBonSlick
*
*/
declare(strict_types=1);
/*
* Created by BonBonSlick
* Contacts: google it
* Date: 9/17/18
* Time: 11:41 PM
*/
namespace App\Voters;
use App\Entity\User\User;
use function get_class;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
final class AdminVoter extends Voter
{
public const IS_IN_ROLE_ADMIN = 'in.role.admin';
/**
* @var AccessDecisionManagerInterface
*/
private $decisionManager;
/**
* self constructor.
*
* @param AccessDecisionManagerInterface $decisionManager
*/
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
/**
* {@inheritdoc}
*/
protected function supports($attribute, $subject): bool
{
return null !== $subject && get_class($subject) === User::class;
}
/**
* {@inheritdoc}
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
$response = false;
$isGranted = false === $user instanceof UserInterface ||
$this->decisionManager->decide($token, [
User::ROLE_ADMIN,
]);
if (false === $isGranted) {
return $response;
}
return true;
}
}