<?php
namespace App\Daikin\FrontendBundle\Security;
use App\Daikin\BaseBundle\Entity\FillingSecurity\Calculation;
use App\Daikin\BaseBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class CalculationVoter
* @package App\Daikin\FrontendBundle\Security
*/
class CalculationVoter extends Voter
{
const VIEW = 'view';
const EDIT = 'edit';
const REMOVE = 'remove';
protected function supports($attribute, $subject): bool
{
if ($attribute != self::VIEW && $attribute != self::EDIT && $attribute != self::REMOVE) {
return false;
}
if (!$subject instanceof Calculation) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
/** @var Calculation $calculation */
$calculation = $subject;
if ($calculation->getUser()->getCompany() !== $user->getCompany()) {
return false;
}
return true;
}
}