src/FlexApp/Security/Voter/_ExtendVoter.php line 8

Open in your IDE?
  1. <?php
  2. namespace FlexApp\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. class _ExtendVoter extends Voter
  6. {
  7.     /**
  8.      * Переменная для наследования. Указываем свои методы
  9.      */
  10.     protected array $methods = [];
  11.     /**
  12.      * Дефолтные методы для проверок
  13.      */
  14.     private array $methodsDefault = [
  15.         'VIEW',
  16.         'CREATE',
  17.         'EDIT',
  18.         'DEL',
  19.         'DELETE',
  20.         'HISTORY',
  21.         'SWITCHUSER',
  22.     ];
  23.     public function __construct()
  24.     {
  25.     }
  26.     /**
  27.      * Проверяем наличие методов. Проерка не строгая.
  28.      * Если нет соответвий, то результат нейтральный
  29.      * Если $attribute является объектом, то выдаем сразу труе
  30.      * @param $attribute
  31.      * @return mixed
  32.      */
  33.     public function checkMethods($attribute)
  34.     {
  35.         if (is_string($attribute)) {
  36.             $attribute strtoupper($attribute);
  37.             $this->methodsDefault array_map('strtoupper'$this->methodsDefault);
  38.             if (in_array($attribute$this->methodsDefault)) {
  39.                 return $this::ACCESS_GRANTED;
  40.             }
  41.             $this->methods array_map('strtoupper'$this->methods);
  42.             if (in_array($attribute$this->methods)) {
  43.                 return $this::ACCESS_GRANTED;
  44.             }
  45.             return $this::ACCESS_ABSTAIN;
  46.         }
  47.         return $this::ACCESS_GRANTED;
  48.     }
  49.     /**
  50.      * Сначала проверка идет по всем supports вотеров до первого TRUE
  51.      */
  52.     protected function supports(string $attribute$subject): bool
  53.     {
  54.         return false;
  55.     }
  56.     /**
  57.      * Потом проверка идет по всем voteOnAttribute вотеров до первого TRUE
  58.      */
  59.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  60.     {
  61.         return false;
  62.     }
  63. }