<?php
namespace App\Security;
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;
use App\Entity\User;
class UserVoter extends Voter
{
const ACTIVE = 'active';
/**
* @var AccessDecisionManagerInterface
*/
private $decisionManager;
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
protected function supports($attribute, $subject): bool
{
if (!in_array($attribute, array(self::ACTIVE))) {
return false;
}
if (!$subject instanceof User) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
$actualUser = $subject;
if (!$user instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::ACTIVE:
if ($actualUser->isIsActive() && $actualUser->isIsAdminActive()){
return true;
}
break;
}
return false;
}
}