src/Daikin/FrontendBundle/Security/CalculationVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Daikin\FrontendBundle\Security;
  3. use App\Daikin\BaseBundle\Entity\FillingSecurity\Calculation;
  4. use App\Daikin\BaseBundle\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Class CalculationVoter
  9.  * @package App\Daikin\FrontendBundle\Security
  10.  */
  11. class CalculationVoter extends Voter
  12. {
  13.     const VIEW 'view';
  14.     const EDIT 'edit';
  15.     const REMOVE 'remove';
  16.     protected function supports($attribute$subject): bool
  17.     {
  18.         if ($attribute != self::VIEW && $attribute != self::EDIT && $attribute != self::REMOVE) {
  19.             return false;
  20.         }
  21.         if (!$subject instanceof Calculation) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  27.     {
  28.         $user $token->getUser();
  29.         if (!$user instanceof User) {
  30.             return false;
  31.         }
  32.         /** @var Calculation $calculation */
  33.         $calculation $subject;
  34.         if ($calculation->getUser()->getCompany() !== $user->getCompany()) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39. }