<?php
namespace App\Security;
use App\Entity\AccessControl;
use App\Repository\AccessControlRepository;
use App\Service\Access;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AccessManager extends Voter
{
private $accessService;
private $accessControlRepository;
public function __construct(Access $accessService, AccessControlRepository $accessControlRepository)
{
$this->accessService = $accessService;
$this->accessControlRepository = $accessControlRepository;
}
protected function supports($attribute, $subject)
{
$features = $this->accessControlRepository->findAll();
$features = array_values(array_unique(array_map(function(AccessControl $ac) {
return $ac->getFeature();
}, $features)));
return in_array($attribute, $features);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
return $this->accessService->getAccessControl($attribute, $subject === true);
}
}