vendor/shopware/platform/src/Core/Framework/Plugin/Subscriber/PluginAclPrivilegesSubscriber.php line 31

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