<?php
namespace App\Security\Translator;
use App\Entity\Translator\Order;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class OrderVoter
*
* @package App\Security\Translator
*/
class OrderVoter extends Voter
{
const EDIT = 'edit';
const VIEW = 'view';
const DELETE = 'delete';
/**
* base voter actions
*/
private const ATTRIBUTES = [
self::EDIT,
self::VIEW,
self::DELETE
];
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function supports($attribute, $subject)
{
return $subject instanceof Order
&& in_array($attribute, self::ATTRIBUTES);
}
/**
* @param string $attribute
* @param Order $order
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(
$attribute,
$order,
TokenInterface $token
) {
switch ($attribute) {
case self::EDIT:
case self::DELETE:
case self::VIEW:
return $order->getUser() == $token->getUser();
break;
}
throw new \LogicException('Invalid attribute: ' . $attribute);
}
}