vendor/shopware/core/Framework/Plugin/Subscriber/PluginAclPrivilegesSubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Plugin\Subscriber;
  3. use Shopware\Core\Framework\Api\Acl\Role\AclRoleDefinition;
  4. use Shopware\Core\Framework\Api\Acl\Role\AclRoleEntity;
  5. use Shopware\Core\Framework\Api\Acl\Role\AclRoleEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Plugin\KernelPluginCollection;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  12.  */
  13. #[Package('core')]
  14. class PluginAclPrivilegesSubscriber implements EventSubscriberInterface
  15. {
  16.     private KernelPluginCollection $plugins;
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(KernelPluginCollection $plugins)
  21.     {
  22.         $this->plugins $plugins;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             AclRoleEvents::ACL_ROLE_LOADED_EVENT => 'onAclRoleLoaded',
  28.         ];
  29.     }
  30.     public function onAclRoleLoaded(EntityLoadedEvent $event): void
  31.     {
  32.         if (!$event->getDefinition() instanceof AclRoleDefinition) {
  33.             return;
  34.         }
  35.         /** @var AclRoleEntity[] $aclRoles */
  36.         $aclRoles $event->getEntities();
  37.         if (!$aclRoles) {
  38.             return;
  39.         }
  40.         $additionalRolePrivileges $this->getAdditionalRolePrivileges();
  41.         foreach ($additionalRolePrivileges as $additionalRole => $additionalPrivileges) {
  42.             foreach ($aclRoles as $aclRole) {
  43.                 if ($additionalRole === AclRoleDefinition::ALL_ROLE_KEY || \in_array($additionalRole$aclRole->getPrivileges(), true)) {
  44.                     $newPrivileges array_values(array_unique(array_merge($aclRole->getPrivileges(), $additionalPrivileges)));
  45.                     $aclRole->setPrivileges($newPrivileges);
  46.                 }
  47.             }
  48.         }
  49.     }
  50.     /**
  51.      * returns a unique, merged array of all role privileges to be added by plugins
  52.      *
  53.      * @return array<string, list<string>>
  54.      */
  55.     private function getAdditionalRolePrivileges(): array
  56.     {
  57.         $rolePrivileges = [];
  58.         foreach ($this->plugins->getActives() as $plugin) {
  59.             $rolePrivileges array_replace_recursive($rolePrivileges$plugin->enrichPrivileges());
  60.         }
  61.         return $rolePrivileges;
  62.     }
  63. }