<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use App\Entity\User;
use App\Entity\Gruppi;
use App\Repository\GruppiRepository;
use App\Repository\RolesRepository;
class GruppiVoter extends Voter
{
private $m_RolesRep;
public function __construct(RolesRepository $a_RolesRep)
{
$this->m_RolesRep = $a_RolesRep;
}
protected function supports($attribute, $subject)
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
$roles = $this->m_RolesRep->findAll();
$aroles = array();
foreach ($roles as $role) {
$aroles[] = $role->getNome();
}
return in_array($attribute, $aroles) && ($subject === null);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!($user instanceof User)) {
return false;
}
$user_group_roles = $user->getGroupRoles();
return in_array($attribute, $user_group_roles);
}
}