<?php
namespace App\Security\ProfitCalculator;
use App\Entity\ProfitCalculator\ProductCalculation;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class CalculationVoter
*
* @package App\Security\ProfitCalculator
*/
class CalculationVoter 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 ProductCalculation
&& in_array($attribute, self::ATTRIBUTES);
}
/**
* @param $attribute
* @param ProductCalculation $calculation
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(
$attribute,
$calculation,
TokenInterface $token
) {
switch ($attribute) {
case self::EDIT:
case self::DELETE:
case self::VIEW:
return $calculation->getUser() == $token->getUser();
break;
}
throw new \LogicException('Invalid attribute: ' . $attribute);
}
}