<?php
namespace App\Daikin\FrontendBundle\Security;
use App\Daikin\BaseBundle\Entity\CoolingMachineCalculation;
use App\Daikin\BaseBundle\Entity\EmergencyStock\Order;
use App\Daikin\BaseBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class RefillCalcVoter
* @package App\Daikin\FrontendBundle\Security
*/
class RefillCalcVoter extends Voter
{
const REFILLCALCULATIONVIEW = 'refillcalculationview';
private AccessDecisionManagerInterface $decisionManager;
/**
* OrderVoter constructor.
* @param AccessDecisionManagerInterface $decisionManager
*/
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
protected function supports($attribute, $subject): bool
{
// * @Security("is_granted('ROLE_COOLANT_REFILL')")
if ($attribute != self::REFILLCALCULATIONVIEW ) {
return false;
}
// if (!$subject instanceof CoolingMachineCalculation) {
// return false;
// }
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
if (!$this->decisionManager->decide($token, ['ROLE_COOLANT_REFILL'])) {
return false;
}
if (!$subject instanceof CoolingMachineCalculation) {
return true;
}
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
$coolingMachineCalculation = $subject;
if (!$coolingMachineCalculation->getUser() || $coolingMachineCalculation->getUser()->getCompany() !== $user->getCompany()) {
return false;
}
return true;
}
}