<?php
namespace CioBudget\Subscriber;
use CioBudget\Service\BudgetLoaderService;
use CioBudget\Service\SessionService;
use CioBudget\Service\StoreLoaderService;
use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
use CioCustomerPermissionGroups\Event\CustomerPermissionGroupIdsLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerGroupsLoadedSubscriber implements EventSubscriberInterface
{
private SessionService $sessionService;
private EntityRepositoryInterface $customerRepository;
private BudgetLoaderService $budgetLoaderService;
private StoreLoaderService $storeLoaderService;
public function __construct(EntityRepositoryInterface $customerRepository,
SessionService $sessionService,
BudgetLoaderService $budgetLoaderService,
StoreLoaderService $storeLoaderService)
{
$this->budgetLoaderService = $budgetLoaderService;
$this->sessionService = $sessionService;
$this->customerRepository = $customerRepository;
$this->storeLoaderService = $storeLoaderService;
}
public static function getSubscribedEvents(): array
{
return [
CustomerPermissionGroupIdsLoadedEvent::class => 'onCustomerGroupsLoadedEvent',
CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent'
];
}
public function onCustomerGroupsLoadedEvent(CustomerPermissionGroupIdsLoadedEvent $event)
{
$groups = $event->getPermissionGroupIds();
$currentBudgetId = $this->sessionService->getCurrentBudgetId();
if ($customerStores = $this->storeLoaderService->getCustomerStores($event->getCustomer())) {
$currentBudget = null;
// find current budget in all budgets of the customer
foreach ($customerStores as $customerStore) {
if (is_array($customerStore) && key_exists('id', $customerStore)) {
$storeBudgetId = $this->budgetLoaderService->getBudgetIdByStoreId($customerStore['id']);
if ($storeBudgetId == $currentBudgetId) {
// store data with ACL becomes budget data
$currentBudget = $customerStore;
$currentBudget['id'] = $storeBudgetId;
break;
}
}
}
// iterate over all aclgroups of the current selected budget of the customer
if ($currentBudget && isset($currentBudget['aclgroup']) && $currentBudget['aclgroup']) {
if (is_array($currentBudget['aclgroup'])) {
foreach ($currentBudget['aclgroup'] as $aclgroup) {
$groups[] = $aclgroup;
}
}
}
}
$event->setPermissionGroupIds($groups);
}
public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
{
$event->addRoles([
[
'title' => 'ALLOW_CALCULATE_POINTS',
'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.'
]
]);
}
}