custom/plugins/PickwareWms/vendor/pickware/mobile-app-auth-bundle/src/Model/Subscriber/MobileAppAuthAclRoleWriteRestrictor.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\MobileAppAuthBundle\Model\Subscriber;
  11. use Pickware\DalBundle\EntityPreWriteValidationEvent;
  12. use Pickware\DalBundle\EntityPreWriteValidationEventDispatcher;
  13. use Pickware\MobileAppAuthBundle\Installation\Steps\UpsertMobileAppAclRoleInstallationStep;
  14. use Shopware\Core\Framework\Api\Acl\Role\AclRoleDefinition;
  15. use Shopware\Core\Framework\Api\Acl\Role\AclUserRoleDefinition;
  16. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Validator\ConstraintViolation;
  19. use Symfony\Component\Validator\ConstraintViolationList;
  20. class MobileAppAuthAclRoleWriteRestrictor implements EventSubscriberInterface
  21. {
  22.     private const ERROR_CODE_NAMESPACE 'PICKWARE_MOBILE_APP_AUTH_BUNDLE__WRITE_RESTRICTOR';
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             EntityPreWriteValidationEventDispatcher::getEventName(AclRoleDefinition::ENTITY_NAME) => 'restrictWriteOnMobileAppAuthRole',
  27.             EntityPreWriteValidationEventDispatcher::getEventName(AclUserRoleDefinition::ENTITY_NAME) => 'restrictWriteOnMobileAppAuthRole',
  28.         ];
  29.     }
  30.     public function restrictWriteOnMobileAppAuthRole($event): void
  31.     {
  32.         if (!($event instanceof EntityPreWriteValidationEvent)) {
  33.             // The subscriber is probably instantiated in its old version (with the Shopware PreWriteValidationEvent) in
  34.             // the container and will be updated on the next container rebuild (next request). Early return.
  35.             return;
  36.         }
  37.         $commands $event->getCommands();
  38.         $violations = new ConstraintViolationList();
  39.         foreach ($commands as $command) {
  40.             $className $command->getDefinition()->getClass();
  41.             if (!(
  42.                     $className === AclRoleDefinition::class
  43.                     && $command->getPrimaryKey()['id'] === UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN
  44.                 ) && !(
  45.                     $className == AclUserRoleDefinition::class
  46.                     && $command->getPrimaryKey()['acl_role_id'] === UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN
  47.             )) {
  48.                 continue;
  49.             }
  50.             $entityName $command->getDefinition()->getEntityName();
  51.             $message 'The pickware mobile app auth acl role should not be deleted, updated or assigned to a user.';
  52.             $violations->add(new ConstraintViolation(
  53.                 $message,
  54.                 $message,
  55.                 [],
  56.                 null,
  57.                 '/',
  58.                 null,
  59.                 null,
  60.                 sprintf(
  61.                     '%s__%s',
  62.                     self::ERROR_CODE_NAMESPACE,
  63.                     mb_strtoupper($entityName),
  64.                 ),
  65.             ));
  66.         }
  67.         if ($violations->count() > 0) {
  68.             $event->addViolation(new WriteConstraintViolationException($violations));
  69.         }
  70.     }
  71. }