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

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(EntityRepositoryInterface $customerRepository,
  17.                                 SessionService            $sessionService,
  18.                                 BudgetLoaderService       $budgetLoaderService,
  19.                                 StoreLoaderService        $storeLoaderService)
  20.     {
  21.         $this->budgetLoaderService $budgetLoaderService;
  22.         $this->sessionService $sessionService;
  23.         $this->customerRepository $customerRepository;
  24.         $this->storeLoaderService $storeLoaderService;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             CustomerPermissionGroupIdsLoadedEvent::class => 'onCustomerGroupsLoadedEvent',
  30.             CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent'
  31.         ];
  32.     }
  33.     public function onCustomerGroupsLoadedEvent(CustomerPermissionGroupIdsLoadedEvent $event)
  34.     {
  35.         $groups $event->getPermissionGroupIds();
  36.         $currentBudgetId $this->sessionService->getCurrentBudgetId();
  37.         if ($customerStores $this->storeLoaderService->getCustomerStores($event->getCustomer())) {
  38.             $currentBudget null;
  39.             // find current budget in all budgets of the customer
  40.             foreach ($customerStores as $customerStore) {
  41.                 if (is_array($customerStore) && key_exists('id'$customerStore)) {
  42.                     $storeBudgetId $this->budgetLoaderService->getBudgetIdByStoreId($customerStore['id']);
  43.                     if ($storeBudgetId == $currentBudgetId) {
  44.                         // store data with ACL becomes budget data
  45.                         $currentBudget $customerStore;
  46.                         $currentBudget['id'] = $storeBudgetId;
  47.                         break;
  48.                     }
  49.                 }
  50.             }
  51.             // iterate over all aclgroups of the current selected budget of the customer
  52.             if ($currentBudget && isset($currentBudget['aclgroup']) && $currentBudget['aclgroup']) {
  53.                 if (is_array($currentBudget['aclgroup'])) {
  54.                     foreach ($currentBudget['aclgroup'] as $aclgroup) {
  55.                         $groups[] = $aclgroup;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.         $event->setPermissionGroupIds($groups);
  61.     }
  62.     public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
  63.     {
  64.         $event->addRoles([
  65.             [
  66.                 'title' => 'ALLOW_CALCULATE_POINTS',
  67.                 '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.'
  68.             ]
  69.         ]);
  70.     }
  71. }