src/Security/AdminSogecVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. class AdminSogecVoter extends Voter
  8. {
  9.     const ADMIN_SOGEC_VOTER_VIEW 'admin_sogec_voter_view';
  10.     const ADMIN_SOGEC_VOTER_EDIT 'admin_sogec_voter_edit';
  11.     const ADMIN_SOGEC_VOTER_DELETE 'admin_sogec_voter_delete';
  12.     const ADMIN_SOGEC_VOTER_NEW 'admin_sogec_voter_new';
  13.     const ARRAY_ADMIN_SOGEC_VOTER = [
  14.         self::ADMIN_SOGEC_VOTER_VIEW,
  15.         self::ADMIN_SOGEC_VOTER_EDIT,
  16.         self::ADMIN_SOGEC_VOTER_DELETE,
  17.         self::ADMIN_SOGEC_VOTER_NEW,
  18.     ];
  19.     private $security;
  20.     public function __construct(Security $security)
  21.     {
  22.         $this->security $security;
  23.     }
  24.     protected function supports($attribute$subject): bool
  25.     {
  26.         if (!in_array($attributeself::ARRAY_ADMIN_SOGEC_VOTER)) {
  27.             return false;
  28.         }
  29.         if (!$subject instanceof User) {
  30.             return false;
  31.         }
  32.         return true;
  33.     }
  34.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  35.     {
  36.         if ($this->security->isGranted(User::ROLE_ADMIN_SOGEC)) {
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. }