<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\ClinicalQuestion\ClinicalQuestion;
use App\Entity\Consultation\Report;
use App\Entity\StatefulInterface;
use App\Entity\User;
use App\Model\Enum\Status;
use Carbon\CarbonImmutable;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class StatefulEntityShowVoter extends Voter
{
public const SHOW = 'SHOW';
private AuthorizationCheckerInterface $authorizationChecker;
/**
* ShowVoter constructor.
*/
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
protected function supports(string $attribute, $subject)
{
if ($attribute === self::SHOW && $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_NVC)) {
return true;
}
// 公開ステータス出なければ不可
if ($subject->getStatus() !== Status::PUBLISHED()) {
return false;
}
// ゲスト会員の時
if (!$this->authorizationChecker->isGranted('ROLE_MEMBER')) {
if ($subject instanceof Report) {
// 報告日をチェック
if (!$subject->getReportedDate()) {
return false;
}
if ($subject->getReportedDate() < new CarbonImmutable(Report::GUEST_SHOW_LIMIT)) {
return false;
}
}
// ゲストはCQを閲覧できない。
if ($subject instanceof ClinicalQuestion) {
return false;
}
}
return true;
}
}