<?php
namespace AppBundle\Security;
use AppBundle\Entity\Matter;
use AppBundle\Entity\User;
use AppBundle\Enum\FeatureAccessEnum;
use CommonBundle\Model\UsersConfinedInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class UsersConfinedVoter
*/
class UsersConfinedVoter extends Voter
{
/**
* {@inheritdoc}
*/
protected function supports($attribute, $subject)
{
return $subject instanceof UsersConfinedInterface;
}
/**
* {@inheritdoc}
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
// todo: find better way to handle rights with this voter
if ($subject instanceof Matter && $user->hasRole(FeatureAccessEnum::ALL_MATTER)) {
return true;
}
/** @var UsersConfinedInterface $subject */
foreach ($subject->getUsersWithRights() as $usersWithRight) {
if ($user->getId() === $usersWithRight->getId()) {
return true;
}
}
return false;
}
}