src/Security/PosteDirectorRouteVoter.php line 18

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