custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT32889/PatchedStateMachineActionController.php line 25

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\Security\Fixes\NEXT32889;
  3. use Shopware\Core\Framework\Api\Acl\Role\AclRoleDefinition;
  4. use Shopware\Core\Framework\Api\Exception\MissingPrivilegeException;
  5. use Shopware\Core\Framework\Api\Response\ResponseFactoryInterface;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Shopware\Core\System\StateMachine\Api\StateMachineActionController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @internal
  15.  * @RouteScope(scopes={"api"})
  16.  */
  17. class PatchedStateMachineActionController extends StateMachineActionController
  18. {
  19.     /**
  20.      * @Route("/api/_action/state-machine/{entityName}/{entityId}/state", name="api.state_machine.states", methods={"GET"})
  21.      */
  22.     public function getAvailableTransitions(
  23.         Request $request,
  24.         Context $context,
  25.         string $entityName,
  26.         string $entityId
  27.     ): JsonResponse {
  28.         $this->validatePrivilege($entityNameAclRoleDefinition::PRIVILEGE_READ$context);
  29.         return parent::getAvailableTransitions($request$context$entityName$entityId);
  30.     }
  31.     /**
  32.      * @Route("/api/_action/state-machine/{entityName}/{entityId}/state/{transition}", name="api.state_machine.transition_state", methods={"POST"})
  33.      */
  34.     public function transitionState(
  35.         Request $request,
  36.         Context $context,
  37.         ResponseFactoryInterface $responseFactory,
  38.         string $entityName,
  39.         string $entityId,
  40.         string $transition
  41.     ): Response {
  42.         $this->validatePrivilege($entityNameAclRoleDefinition::PRIVILEGE_UPDATE$context);
  43.         return parent::transitionState($request$context$responseFactory$entityName$entityId$transition);
  44.     }
  45.     private function validatePrivilege(string $entityNamestring $privilegeContext $context): void
  46.     {
  47.         $permission $entityName ':' $privilege;
  48.         if (!$context->isAllowed($permission)) {
  49.             $reflectionException = new \ReflectionClass(MissingPrivilegeException::class);
  50.             $reflectionConstructor $reflectionException->getConstructor();
  51.             if ($reflectionConstructor->getNumberOfRequiredParameters() > 0) {
  52.                 throw new MissingPrivilegeException($permission);
  53.             } else {
  54.                 throw new MissingPrivilegeException([$permission]);
  55.             }
  56.         }
  57.     }
  58. }