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

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\Plugin\KernelPluginCollection;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class PluginAclPrivilegesSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var KernelPluginCollection
  13.      */
  14.     private $plugins;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(KernelPluginCollection $plugins)
  19.     {
  20.         $this->plugins $plugins;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             AclRoleEvents::ACL_ROLE_LOADED_EVENT => 'onAclRoleLoaded',
  26.         ];
  27.     }
  28.     public function onAclRoleLoaded(EntityLoadedEvent $event): void
  29.     {
  30.         if (!$event->getDefinition() instanceof AclRoleDefinition) {
  31.             return;
  32.         }
  33.         /** @var AclRoleEntity[] $aclRoles */
  34.         $aclRoles $event->getEntities();
  35.         if (!$aclRoles) {
  36.             return;
  37.         }
  38.         $additionalRolePrivileges $this->getAdditionalRolePrivileges();
  39.         foreach ($additionalRolePrivileges as $additionalRole => $additionalPrivileges) {
  40.             foreach ($aclRoles as $aclRole) {
  41.                 if ($additionalRole === AclRoleDefinition::ALL_ROLE_KEY || \in_array($additionalRole$aclRole->getPrivileges(), true)) {
  42.                     $newPrivileges array_values(array_unique(array_merge($aclRole->getPrivileges(), $additionalPrivileges)));
  43.                     $aclRole->setPrivileges($newPrivileges);
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     /**
  49.      * returns a unique, merged array of all role privileges to be added by plugins
  50.      */
  51.     private function getAdditionalRolePrivileges(): array
  52.     {
  53.         $rolePrivileges = [];
  54.         foreach ($this->plugins->getActives() as $plugin) {
  55.             $rolePrivileges array_replace_recursive($rolePrivileges$plugin->enrichPrivileges());
  56.         }
  57.         return $rolePrivileges;
  58.     }
  59. }