<?php
/**
* @license Silk Software House Sp. Z O. O.
*/
declare(strict_types=1);
namespace App\Security;
use App\Entity\TemperatureNotification;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class TemperatureNotificationVoter extends Voter
{
public const SHOW = 'show';
public const NEW = 'new';
public const EDIT = 'edit';
public const DELETE = 'delete';
protected function supports(string $attribute, $subject): bool
{
return $subject instanceof TemperatureNotification
&& in_array($attribute, [self::SHOW, self::NEW, self::EDIT, self::DELETE], true);
}
/**
* @param TemperatureNotification $subject
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if (self::NEW === $attribute && null === $subject->getId()) {
return true;
}
return null !== $subject->getUser()
&& $subject->getUser()->getId() === $user->getId();
}
}