src/Security/EmployeeRouteVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\AppActions;
  4. use App\Entity\AppUser;
  5. use App\Entity\Employee;
  6. use App\Entity\RoleAction;
  7. use App\Entity\UserRole;
  8. use App\Utils\Consts;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. class EmployeeRouteVoter extends Voter
  15. {
  16.     public const CREATE 'CREATE';
  17.     public const VIEW 'VIEW';
  18.     public const EDIT 'EDIT';
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         // replace with your own logic
  22.         // https://symfony.com/doc/current/security/voters.html
  23.         return in_array($attribute, [self::CREATEself::VIEWself::EDIT])
  24.             && $subject instanceof \App\Entity\Employee;
  25.     }
  26.     private $entityManager;
  27.     private $security;
  28.     public function __construct(EntityManagerInterface $entityManager,Security $security)
  29.     {
  30.         $this->entityManager $entityManager;
  31.         $this->security $security;
  32.     }
  33.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  34.     {
  35.         $user $token->getUser();
  36.         if (!$user instanceof AppUser) {
  37.             // the user must be logged in; if not, deny access
  38.             return false;
  39.         }
  40.         // you know $subject is a Post object, thanks to `supports()`
  41.         /** @var Employee $employee */
  42.         $employee $subject;
  43.         switch ($attribute) {
  44.             case self::VIEW:
  45.                 return $this->canView($employee$user);
  46.             case self::EDIT:
  47.                 return $this->canEdit($employee$user);
  48.             case self::CREATE:
  49.                 return $this->canCreate($employee$user);
  50.         }
  51.         return false;
  52.     }
  53.     private function canView(Employee $employeeAppUser $user): bool
  54.     {
  55.         // if they can edit, they can view
  56.         return $this->entityManager->getRepository(RoleAction::class)->
  57.         hasRoleActionWithReadPermission($user,'Employee');
  58.     }
  59.     private function canEdit(Employee $employeeAppUser $user): bool
  60.     {
  61.         // this assumes that the Employee object has a `getOwner()` method
  62.         return $this->entityManager->getRepository(RoleAction::class)->
  63.         hasRoleActionWithEditPermission($user,'Employee');
  64.     }
  65.     private function canCreate(Employee $employeeAppUser $user): bool
  66.     {
  67.         // this assumes that the Employee object has a `getOwner()` method
  68.         return $this->entityManager->getRepository(RoleAction::class)->
  69.         hasRoleActionWithCreatePermission($user,'Employee');
  70.     }
  71. }