chill-bundles/src/Bundle/ChillPersonBundle/Security/Authorization/AccompanyingPeriodWorkEvaluationVoter.php

64 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Security\Authorization;
use Chill\MainBundle\Security\Authorization\ChillVoterInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use UnexpectedValueException;
use function in_array;
class AccompanyingPeriodWorkEvaluationVoter extends Voter implements ChillVoterInterface
{
public const ALL = [
self::SEE,
self::STATS,
];
public const SEE = 'CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_EVALUATION_SHOW';
public const STATS = 'CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_EVALUATION_STATS';
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
return $subject instanceof AccompanyingPeriodWorkEvaluation
&& in_array($attribute, self::ALL, true);
}
/**
* @param string $attribute
* @param AccompanyingPeriodWorkEvaluation $subject
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::STATS:
return $this->security->isGranted(AccompanyingPeriodWorkVoter::STATS, $subject);
case self::SEE:
return $this->security->isGranted(AccompanyingPeriodWorkVoter::SEE, $subject->getAccompanyingPeriodWork());
default:
throw new UnexpectedValueException("attribute {$attribute} is not supported");
}
}
}