custom/plugins/CioBudget/src/Subscriber/CustomerGroupsLoadedSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace CioBudget\Subscriber;
  3. use CioBudget\Service\BudgetLoaderService;
  4. use CioBudget\Service\SessionService;
  5. use CioBudget\Service\StoreLoaderService;
  6. use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
  7. use CioCustomerPermissionGroups\Event\CustomerPermissionGroupIdsLoadedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CustomerGroupsLoadedSubscriber implements EventSubscriberInterface
  11. {
  12.     private SessionService $sessionService;
  13.     private EntityRepositoryInterface $customerRepository;
  14.     private BudgetLoaderService $budgetLoaderService;
  15.     private StoreLoaderService $storeLoaderService;
  16.     public function __construct(
  17.         EntityRepositoryInterface $customerRepository,
  18.         SessionService            $sessionService,
  19.         BudgetLoaderService       $budgetLoaderService,
  20.         StoreLoaderService        $storeLoaderService
  21.     )
  22.     {
  23.         $this->budgetLoaderService $budgetLoaderService;
  24.         $this->sessionService $sessionService;
  25.         $this->customerRepository $customerRepository;
  26.         $this->storeLoaderService $storeLoaderService;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             CustomerPermissionGroupIdsLoadedEvent::class => 'onCustomerGroupsLoadedEvent',
  32.             CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent'
  33.         ];
  34.     }
  35.     public function onCustomerGroupsLoadedEvent(CustomerPermissionGroupIdsLoadedEvent $event)
  36.     {
  37.         $groups $event->getPermissionGroupIds();
  38.         $currentBudgetId $this->sessionService->getCurrentBudgetId();
  39.         if ($customerStores $this->storeLoaderService->getCustomerStores($event->getCustomer())) {
  40.             $currentBudget null;
  41.             // find current budget in all budgets of the customer
  42.             foreach ($customerStores as $customerStore) {
  43.                 if (is_array($customerStore) && key_exists('id'$customerStore)) {
  44.                     $storeBudgetId $this->budgetLoaderService->getBudgetIdByStoreId($customerStore['id']);
  45.                     if ($storeBudgetId == $currentBudgetId) {
  46.                         // store data with ACL becomes budget data
  47.                         $currentBudget $customerStore;
  48.                         $currentBudget['id'] = $storeBudgetId;
  49.                         break;
  50.                     }
  51.                 }
  52.             }
  53.             // iterate over all aclgroups of the current selected budget of the customer
  54.             if ($currentBudget && isset($currentBudget['aclgroup']) && $currentBudget['aclgroup']) {
  55.                 if (is_array($currentBudget['aclgroup'])) {
  56.                     foreach ($currentBudget['aclgroup'] as $aclgroup) {
  57.                         $groups[] = $aclgroup;
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.         $event->setPermissionGroupIds($groups);
  63.     }
  64.     public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
  65.     {
  66.         $event->addRoles([
  67.             [
  68.                 'title' => 'ALLOW_CALCULATE_POINTS',
  69.                 'description' => 'Für Kunden mit diesem Recht wird das Nutzbares Gesamtkonto auf Basis der Budget Historie errechnet. Hat der Kunde dieses Recht nicht ist der Kontostand immer 0.'
  70.             ]
  71.         ]);
  72.     }
  73. }