<?php
declare(strict_types=1);
namespace Harmonizely\Calendar\Voter;
use Harmonizely\Model\CalendarInterface;
use Harmonizely\Model\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class CanEditCalendarVoter extends Voter
{
public const EDIT = 'edit';
protected function supports($attribute, $subject): bool
{
if (self::EDIT !== $attribute) {
return false;
}
if (false === $subject instanceof CalendarInterface) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var UserInterface $loggedInUser */
$loggedInUser = $token->getUser();
if (!$loggedInUser instanceof UserInterface) {
return false;
}
/** @var CalendarInterface $subject */
$calendarUser = $subject->getUser();
return $loggedInUser->getId() === $calendarUser->getId();
}
}