<?php
namespace Crea\ParameterBundle\Voter;
use Crea\ParameterBundle\Entity\Parameter;
use Crea\ParameterBundle\Entity\ParameterType;
use Crea\ParameterBundle\Provider\ParameterRightCodeProvider;
use Exception;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class ParameterVoter extends Voter
{
protected ParameterRightCodeProvider $parameterRightCodeProvider;
public function __construct(ParameterRightCodeProvider $parameterRightCodeProvider)
{
$this->parameterRightCodeProvider = $parameterRightCodeProvider;
}
/**
* @inheritDoc
* @throws Exception
*/
protected function supports($attribute, $subject): bool
{
$chapterAttributes = [];
if ($subject instanceof ParameterType) {
$chapterAttributes[] = $this->parameterRightCodeProvider->getParameterViewRightCode($subject);
$chapterAttributes[] = $this->parameterRightCodeProvider->getParameterCreateRightCode($subject);
}
if ($subject instanceof Parameter) {
$chapterAttributes[] = $this->parameterRightCodeProvider->getParameterUpdateRightCode($subject);
$chapterAttributes[] = $this->parameterRightCodeProvider->getParameterRemoveRightCode($subject);
}
return in_array($attribute, $chapterAttributes);
}
/**
* @inheritDoc
* @throws Exception
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var UserInterface $loggedUser */
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
if ($subject instanceof ParameterType) {
switch ($attribute) {
case $this->parameterRightCodeProvider->getParameterViewRightCode($subject):
return $this->voteOnParameterView($subject, $loggedUser);
case $this->parameterRightCodeProvider->getParameterCreateRightCode($subject):
return $this->voteOnParameterCreate($subject, $loggedUser);
}
}
if ($subject instanceof Parameter) {
switch ($attribute) {
case $this->parameterRightCodeProvider->getParameterUpdateRightCode($subject):
return $this->voteOnParameterUpdate($subject, $loggedUser);
case $this->parameterRightCodeProvider->getParameterRemoveRightCode($subject):
return $this->voteOnParameterRemove($subject, $loggedUser);
}
}
return false;
}
private function voteOnParameterView(ParameterType $parameterType, UserInterface $loggedUser): bool
{
return in_array($this->parameterRightCodeProvider->getParameterViewRightCode($parameterType), $loggedUser->getRoles());
}
private function voteOnParameterCreate(ParameterType $parameterType, UserInterface $loggedUser): bool
{
return in_array($this->parameterRightCodeProvider->getParameterCreateRightCode($parameterType), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnParameterUpdate(Parameter $parameter, UserInterface $loggedUser): bool
{
return in_array($this->parameterRightCodeProvider->getParameterUpdateRightCode($parameter), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnParameterRemove(Parameter $parameter, UserInterface $loggedUser): bool
{
return in_array($this->parameterRightCodeProvider->getParameterRemoveRightCode($parameter), $loggedUser->getRoles());
}
}