<?php
namespace SettingsBundle\Security;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* ModuleVoter votes based on active module
*/
class UserModuleVoter extends Voter
{
/**
* {@inheritdoc}
*/
protected function supports($attribute, $subject): bool
{
return $subject === null && 0 === strpos($attribute, 'USER_MODULE_');
}
/**
* {@inheritdoc}
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return $user->hasRole($attribute);
}
}