<?php
/*
* Copyright (c) Pickware GmbH. All rights reserved.
* This file is part of software that is released under a proprietary license.
* You must not copy, modify, distribute, make publicly available, or execute
* its contents or parts thereof without express permission by the copyright
* holder, unless otherwise permitted by law.
*/
declare(strict_types=1);
namespace Pickware\MobileAppAuthBundle\Model\Subscriber;
use Pickware\MobileAppAuthBundle\Installation\Steps\UpsertMobileAppAclRoleInstallationStep;
use Shopware\Core\Framework\Api\Acl\Role\AclRoleDefinition;
use Shopware\Core\Framework\Api\Acl\Role\AclUserRoleDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
class MobileAppAuthAclRoleWriteRestrictor implements EventSubscriberInterface
{
private const ERROR_CODE_NAMESPACE = 'PICKWARE_MOBILE_APP_AUTH_BUNDLE__WRITE_RESTRICTOR';
public static function getSubscribedEvents(): array
{
return [PreWriteValidationEvent::class => 'restrictWriteOnMobileAppAuthRole'];
}
public function restrictWriteOnMobileAppAuthRole(PreWriteValidationEvent $event): void
{
$commands = $event->getCommands();
$violations = new ConstraintViolationList();
foreach ($commands as $command) {
$className = $command->getDefinition()->getClass();
if (!(
$className === AclRoleDefinition::class
&& $command->getPrimaryKey()['id'] === UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN
) && !(
$className == AclUserRoleDefinition::class
&& $command->getPrimaryKey()['acl_role_id'] === UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN
)) {
continue;
}
$entityName = $command->getDefinition()->getEntityName();
$message = 'The pickware mobile app auth acl role should not be deleted, updated or assigned to a user.';
$violations->add(new ConstraintViolation(
$message,
$message,
[],
null,
'/',
null,
null,
sprintf(
'%s__%s',
self::ERROR_CODE_NAMESPACE,
mb_strtoupper($entityName),
),
));
}
if ($violations->count() > 0) {
$event->getExceptions()->add(new WriteConstraintViolationException($violations));
}
}
}