<?php
namespace AppBundle\SfExtend;
use Biz\User\CurrentUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class AdminVoter implements VoterInterface
{
public const ADMIN = 'ROLE_ADMIN';
public const BACKEND = 'ROLE_BACKEND';
public function supportsAttribute($attribute)
{
return self::ADMIN === $attribute || self::BACKEND === $attribute;
}
public function supportsClass($class)
{
// TODO: Implement supportsClass() method.
}
public function vote(TokenInterface $token, $object, array $attributes)
{
foreach ($attributes as $attribute) {
if (!$this->supportsAttribute($attribute)) {
return self::ACCESS_ABSTAIN;
}
}
$user = $token->getUser();
if (empty($user) || !$user instanceof CurrentUser) {
return self::ACCESS_DENIED;
}
if ($token->getUser()->isAdmin()) {
return self::ACCESS_GRANTED;
} else {
return self::ACCESS_DENIED;
}
}
}