<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\StatefulInterface;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class StatefulEntityEditVoter extends Voter
{
public const EDIT = 'EDIT';
private AuthorizationCheckerInterface $authorizationChecker;
/**
* ShowVoter constructor.
*/
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
protected function supports(string $attribute, $subject)
{
if ($attribute === self::EDIT && $subject instanceof StatefulInterface) {
return true;
}
return false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
assert($subject instanceof StatefulInterface);
// 管理者は全て編集可能
if ($this->authorizationChecker->isGranted(User::ROLE_SUPER_ADMIN)) {
return true;
}
// 編集者は下書または完了のみ
if ($this->authorizationChecker->isGranted(User::ROLE_EDITOR)) {
return $subject->getStatus()->isEditableByEditor();
}
return false;
}
}