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

43 lines
1.2 KiB
PHP

<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Security\Authorization;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use UnexpectedValueException;
class AccompanyingPeriodCommentVoter extends Voter
{
public const DELETE = 'CHILL_PERSON_ACCOMPANYING_PERIOD_COMMENT_DELETE';
public const EDIT = 'CHILL_PERSON_ACCOMPANYING_PERIOD_COMMENT_EDIT';
protected function supports($attribute, $subject)
{
return $subject instanceof Comment;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var Comment $subject */
switch ($attribute) {
case self::EDIT:
case self::DELETE:
return $subject->getCreator() === $token->getUser();
default:
throw new UnexpectedValueException("This attribute {$attribute} is not supported");
}
}
}