src/Security/Voter/Profile/Payments/PaymentsVoter.php line 15

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter\Profile\Payments;
  4. use App\Model\User\Entity\User\Role\Permission;
  5. use App\Model\User\Entity\User\Role\RoleConstants;
  6. use App\Security\UserIdentity;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  10. use Symfony\Component\Security\Core\Security;
  11. class PaymentsVoter extends Voter
  12. {
  13.     public const PAYMENT_LIST 'payment_list';
  14.     public const PAYMENT_SHOW 'payment_show';
  15.     public const PAYMENT_WITHDRAW 'payment_withdraw';
  16.     public const PAYMENT_DEPOSIT 'payment_deposit';
  17.     private $security;
  18.     public function __construct(Security $security)
  19.     {
  20.         $this->security $security;
  21.     }
  22.     protected function supports(string $attribute$subject): bool
  23.     {
  24.         return in_array($attribute, [
  25.             self::PAYMENT_LIST,
  26.             self::PAYMENT_SHOW,
  27.             self::PAYMENT_WITHDRAW,
  28.             self::PAYMENT_DEPOSIT
  29.         ], true);
  30.     }
  31.     /**
  32.      * @param string $attribute
  33.      * @param \App\ReadModel\Profile\Payment\DetailView $subject
  34.      * @param TokenInterface $token
  35.      * @return bool
  36.      */
  37.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  38.     {
  39.         // Moderators have full access
  40.         if ($this->security->isGranted('ROLE_MODERATOR')) {
  41.             return true;
  42.         }
  43.         $user $token->getUser();
  44.         // Only authenticated UserIdentity can proceed
  45.         if (!$user instanceof UserIdentity) {
  46.             return false;
  47.         }
  48.         // Only profile owner can proceed
  49.         if ($subject->profile_id !== $user->getProfileId()) {
  50.             return false;
  51.         }
  52.         $role = (new RoleConstants($user->getRole()));
  53.         if ($role->isOrganizerUser()) {
  54.             if ($this->checkPermissionEmployee($user$attribute) === false) {
  55.                 throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  56.             }
  57.         }
  58.         // Check specific permissions
  59. //        switch ($attribute) {
  60. //            case self::PAYMENT_SHOW:
  61. //                return $user->isPermission(Permission::PAYMENT_SHOW);
  62. //
  63. //            default:
  64. //                return false;
  65. //        }
  66.         return true;
  67.     }
  68.     /**
  69.      * Проверка разрешений сотрудника
  70.      * @param UserIdentity $user
  71.      * @param string $attribute
  72.      * @return bool
  73.      */
  74.     private function checkPermissionEmployee(UserIdentity $userstring $attribute): bool
  75.     {
  76.         // Check specific permissions
  77.         switch ($attribute) {
  78.             case self::PAYMENT_WITHDRAW:
  79.                 return $user->isPermission(Permission::PAYMENT_WITHDRAW);
  80.             case self::PAYMENT_SHOW:
  81.                 return $user->isPermission(Permission::PAYMENT_SHOW);
  82.             case self::PAYMENT_DEPOSIT:
  83.                 return $user->isPermission(Permission::PAYMENT_DEPOSIT);
  84.             default:
  85.                 return true;
  86.         }
  87.     }
  88. }