<?php
namespace App\Voter\__Accounting;
use App\Entity\__Accounting\InsurancePrice;
use App\Entity\Product\ProductVariant;
use App\Voter\VoterTrait;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class AccountingInsurancePriceVoter extends Voter
{
use VoterTrait;
const LIST = "ACCOUNTING_INSURANCE_PRICE_LIST";
const CREATE = "ACCOUNTING_INSURANCE_PRICE_CREATE";
const UPDATE = "ACCOUNTING_INSURANCE_PRICE_UPDATE";
const DELETE = "ACCOUNTING_INSURANCE_PRICE_DELETE";
protected function supports($attribute, $subject): bool
{
return $attribute == self::LIST ||
($attribute == self::CREATE && $subject instanceof ProductVariant) ||
(in_array($attribute, [self::UPDATE, self::DELETE]) && $subject instanceof InsurancePrice);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::LIST:
return $this->voteOnList($loggedUser);
case self::CREATE:
return $this->voteOnCreate($subject, $loggedUser);
case self::UPDATE:
return $this->voteOnUpdate($subject, $loggedUser);
case self::DELETE:
return $this->voteOnDelete($subject, $loggedUser);
}
return false;
}
private function voteOnList(UserInterface $loggedUser): bool
{
return in_array(self::LIST, $loggedUser->getRoles());
}
private function voteOnCreate(?ProductVariant $productVariant, UserInterface $loggedUser): bool
{
if (null === $productVariant) {
return false;
}
return in_array(self::CREATE, $loggedUser->getRoles());
}
private function voteOnUpdate(?InsurancePrice $insurancePrice, UserInterface $loggedUser): bool
{
if (null === $insurancePrice) {
return false;
}
return in_array(self::UPDATE, $loggedUser->getRoles());
}
private function voteOnDelete(?InsurancePrice $insurancePrice, UserInterface $loggedUser): bool
{
if (null === $insurancePrice) {
return false;
}
return in_array(self::DELETE, $loggedUser->getRoles());
}
}