custom/plugins/VioB2BLogin/src/Core/Services/PrivilegeService.php line 104

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioB2BLogin\Core\Services;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use VioB2BLogin\Core\System\SalesChannel\Context\SalesChannelContextFactory;
  10. use VioB2BLogin\Entity\Employee\EmployeeEntity;
  11. use VioB2BLogin\Entity\Privilege\PrivilegeCollection;
  12. use VioB2BLogin\Entity\Privilege\PrivilegeEntity;
  13. use VioB2BLogin\Entity\Privilege\PrivilegeNamespaces;
  14. class PrivilegeService
  15. {
  16.     private EntityRepositoryInterface $privilegeRepository;
  17.     /**
  18.      * PrivilegeService constructor.
  19.      * @param EntityRepositoryInterface $privilegeRepository
  20.      */
  21.     public function __construct(
  22.         EntityRepositoryInterface $privilegeRepository
  23.     )
  24.     {
  25.         $this->privilegeRepository $privilegeRepository;
  26.     }
  27.     public function getAllPrivileges(SalesChannelContext $context): PrivilegeCollection
  28.     {
  29.         $criteria = new Criteria();
  30.         $criteria->addFilter(new EqualsFilter('active'true));
  31.         $result $this->privilegeRepository->search($criteria$context->getContext());
  32.         $result->sort(function (PrivilegeEntity $aPrivilegeEntity $b) {
  33.             return $a->getNamespace() . $a->getName() <=> $b->getNamespace() . $b->getName();
  34.         });
  35.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  36.         return $result->getEntities();
  37.     }
  38.     /**
  39.      * @param SalesChannelContext $context
  40.      * @return array<string,string>
  41.      */
  42.     public function getAllGroupedPrivileges(SalesChannelContext $context): array
  43.     {
  44.         $privileges $this->getAllPrivileges($context);
  45.         $allNamespaces = [];
  46.         $allPermissions = [];
  47.         // project to array
  48.         $projection = [];
  49.         foreach ($privileges as $privilege) {
  50.             if (!in_array($privilege->getNamespace(), $allNamespacestrue)) {
  51.                 $allNamespaces[] = $privilege->getNamespace();
  52.             }
  53.             if (!in_array($privilege->getName(), $allPermissionstrue)) {
  54.                 $allPermissions[] = $privilege->getName();
  55.             }
  56.             if (!array_key_exists($privilege->getNamespace(), $projection)) {
  57.                 $projection[$privilege->getNamespace()] = [];
  58.             }
  59.             $projection[$privilege->getNamespace()][$privilege->getName()] = $privilege;
  60.         }
  61.         foreach ( $allNamespaces as $namespace )
  62.         {
  63.             if( !array_key_exists($namespace$projection ) ) {
  64.                 $projection[$namespace] = [];
  65.             }
  66.             foreach ($allPermissions as $permission)
  67.             {
  68.                 if( !array_key_exists($permission$projection[$namespace] ) ) {
  69.                     $projection[$namespace][$permission] = null;
  70.                 }
  71.             }
  72.             ksort($projection[$namespace]);
  73.         }
  74.         return $projection;
  75.     }
  76.     /**
  77.      * @param SalesChannelContext $context
  78.      * @return PrivilegeCollection
  79.      */
  80.     public function getPrivilegesOfUser(SalesChannelContext $context): PrivilegeCollection
  81.     {
  82.         $user $context->getCustomer();
  83.         /** @var EmployeeEntity $employee */
  84.         $employee $context->getExtensionOfType(SalesChannelContextFactory::EMPLOYEE_KEYEmployeeEntity::class);
  85.         if(!$user instanceof CustomerEntity) {
  86.             $collection $this->getAllActivePriveleges($context);
  87.             $collection->filterAndReduceByProperty('namespace'PrivilegeNamespaces::EMPLOYEE);
  88.             return $collection;
  89.         }
  90.         if(!$employee instanceof EmployeeEntity || $employee->getPrivileges()->count() === 0) {
  91.             // if customer is directly logged in all privileges are granted
  92.             return $this->getAllActivePriveleges($context);
  93.         }
  94.         return $employee->getPrivileges();
  95.     }
  96.     public function checkPrivilege(SalesChannelContext $contextstring $namespacestring $name): bool
  97.     {
  98.         return $this->checkPrivilegeByCollection($this->getPrivilegesOfUser($context), $namespace$name);
  99.     }
  100.     public function checkPrivilegeByCollection(PrivilegeCollection $privilegesstring $namespacestring $name): bool
  101.     {
  102.         $privilege $privileges->filter(
  103.         /**
  104.          * @param $p PrivilegeEntity
  105.          * @return bool
  106.          */
  107.             function (PrivilegeEntity $p) use ($namespace$name) {
  108.                 return ($p->getNamespace() === $namespace && $p->getName() === $name);
  109.             })->first();
  110.         return ($privilege instanceof PrivilegeEntity);
  111.     }
  112.     /**
  113.      * @param SalesChannelContext $context
  114.      * @return PrivilegeCollection
  115.      */
  116.     private function getAllActivePriveleges(SalesChannelContext $context): PrivilegeCollection
  117.     {
  118.         $criteria = new Criteria();
  119.         $criteria->addFilter(new EqualsFilter('active'true));
  120.         $result $this->privilegeRepository->search($criteria$context->getContext());
  121.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  122.         return $result->getEntities();
  123.     }
  124. }