vendor/xearts/taobao-daiko-bundle/src/Security/Voter/ApiEstimatePutStepVoter.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Xearts\Bundle\TaobaoDaikoBundle\Security\Voter;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Xearts\Bundle\TaobaoDaikoBundle\Entity\Admin;
  7. use Xearts\Bundle\TaobaoDaikoBundle\Entity\Estimate;
  8. use Xearts\Bundle\TaobaoDaikoBundle\Entity\EstimateStep;
  9. use Xearts\Bundle\TaobaoDaikoBundle\Entity\User;
  10. class ApiEstimatePutStepVoter extends Voter
  11. {
  12.     public const STEP_CANCEL 'step_cancel';
  13.     public const STEP_APPROVE 'step_approve';
  14.     public const STEP_PAYMENT 'step_payment';
  15.     public const STEP_PAYMENT_POSTAGE 'step_payment_postage';
  16.     protected function supports($attribute$subject)
  17.     {
  18.         if (!$subject instanceof Estimate) {
  19.             return false;
  20.         }
  21.         return in_array(
  22.             $attribute,
  23.             [self::STEP_CANCELself::STEP_APPROVEself::STEP_PAYMENTself::STEP_PAYMENT_POSTAGE]
  24.         );
  25.     }
  26.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  27.     {
  28.         assert($subject instanceof Estimate);
  29.         $user $token->getUser();
  30.         if ($user instanceof Admin) {
  31.             return true;
  32.         }
  33.         if (!$user instanceof User) {
  34.             return false;
  35.         }
  36.         if ($subject->getUser() !== $user) {
  37.             return false;
  38.         }
  39.         $estimateStep $subject->getEstimateStep();
  40.         if (!$estimateStep) {
  41.             return false;
  42.         }
  43.         switch ($attribute) {
  44.             case self::STEP_APPROVE:
  45.             case self::STEP_CANCEL:
  46.                 return EstimateStep::CODE_ESTIMATE_CONFIRM === $estimateStep->getCode();
  47.             case self::STEP_PAYMENT:
  48.                 return EstimateStep::CODE_PAYMENT_WAITING === $estimateStep->getCode();
  49.             case self::STEP_PAYMENT_POSTAGE:
  50.                 return EstimateStep::CODE_POSTAGE_WAITING === $estimateStep->getCode();
  51.         }
  52.         return false;
  53.     }
  54. }