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

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\MobileAppAuthBundle\Installation\Steps\UpsertMobileAppAclRoleInstallationStep;
  12. use Shopware\Core\Framework\Api\Acl\Role\AclRoleDefinition;
  13. use Shopware\Core\Framework\Api\Acl\Role\AclUserRoleDefinition;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  15. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Validator\ConstraintViolation;
  18. use Symfony\Component\Validator\ConstraintViolationList;
  19. class MobileAppAuthAclRoleWriteRestrictor implements EventSubscriberInterface
  20. {
  21.     private const ERROR_CODE_NAMESPACE 'PICKWARE_MOBILE_APP_AUTH_BUNDLE__WRITE_RESTRICTOR';
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [PreWriteValidationEvent::class => 'restrictWriteOnMobileAppAuthRole'];
  25.     }
  26.     public function restrictWriteOnMobileAppAuthRole(PreWriteValidationEvent $event): void
  27.     {
  28.         $commands $event->getCommands();
  29.         $violations = new ConstraintViolationList();
  30.         foreach ($commands as $command) {
  31.             $className $command->getDefinition()->getClass();
  32.             if (!(
  33.                     $className === AclRoleDefinition::class
  34.                     && $command->getPrimaryKey()['id'] === UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN
  35.                 ) && !(
  36.                     $className == AclUserRoleDefinition::class
  37.                     && $command->getPrimaryKey()['acl_role_id'] === UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN
  38.             )) {
  39.                 continue;
  40.             }
  41.             $entityName $command->getDefinition()->getEntityName();
  42.             $message 'The pickware mobile app auth acl role should not be deleted, updated or assigned to a user.';
  43.             $violations->add(new ConstraintViolation(
  44.                 $message,
  45.                 $message,
  46.                 [],
  47.                 null,
  48.                 '/',
  49.                 null,
  50.                 null,
  51.                 sprintf(
  52.                     '%s__%s',
  53.                     self::ERROR_CODE_NAMESPACE,
  54.                     mb_strtoupper($entityName),
  55.                 ),
  56.             ));
  57.         }
  58.         if ($violations->count() > 0) {
  59.             $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  60.         }
  61.     }
  62. }