vendor/shopware/administration/Controller/UserConfigController.php line 38

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Administration\Controller;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Shopware\Core\Framework\Api\Context\Exception\InvalidContextSourceException;
  7. use Shopware\Core\Framework\Api\Controller\Exception\ExpectedUserHttpException;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\MultiInsertQueryQueue;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Log\Package;
  15. use Shopware\Core\Framework\Util\Json;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\System\User\Aggregate\UserConfig\UserConfigDefinition;
  18. use Shopware\Core\System\User\Aggregate\UserConfig\UserConfigEntity;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. #[Package('system-settings')]
  25. class UserConfigController extends AbstractController
  26. {
  27.     /**
  28.      * @internal
  29.      */
  30.     public function __construct(private readonly EntityRepository $userConfigRepository, private readonly Connection $connection)
  31.     {
  32.     }
  33.     #[Route(path'/api/_info/config-me'name'api.config_me.get'defaults: ['auth_required' => true'_routeScope' => ['administration']], methods: ['GET'])]
  34.     public function getConfigMe(Context $contextRequest $request): Response
  35.     {
  36.         $userConfigs $this->getOwnUserConfig($context$request->query->all('keys'));
  37.         $data = [];
  38.         /** @var UserConfigEntity $userConfig */
  39.         foreach ($userConfigs as $userConfig) {
  40.             $data[$userConfig->getKey()] = $userConfig->getValue();
  41.         }
  42.         return new JsonResponse(['data' => $data]);
  43.     }
  44.     #[Route(path'/api/_info/config-me'name'api.config_me.update'defaults: ['auth_required' => true'_routeScope' => ['administration']], methods: ['POST'])]
  45.     public function updateConfigMe(Context $contextRequest $request): Response
  46.     {
  47.         $postUpdateConfigs $request->request->all();
  48.         if (empty($postUpdateConfigs)) {
  49.             return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  50.         }
  51.         $this->massUpsert($context$postUpdateConfigs);
  52.         return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  53.     }
  54.     private function getOwnUserConfig(Context $context, array $keys): array
  55.     {
  56.         $userId $this->getUserId($context);
  57.         $criteria = new Criteria();
  58.         $criteria->addFilter(new EqualsFilter('userId'$userId));
  59.         if (!empty($keys)) {
  60.             $criteria->addFilter(new EqualsAnyFilter('key'$keys));
  61.         }
  62.         return $this->userConfigRepository->search($criteria$context)->getElements();
  63.     }
  64.     private function getUserId(Context $context): string
  65.     {
  66.         if (!$context->getSource() instanceof AdminApiSource) {
  67.             throw new InvalidContextSourceException(AdminApiSource::class, $context->getSource()::class);
  68.         }
  69.         $userId $context->getSource()->getUserId();
  70.         if (!$userId) {
  71.             throw new ExpectedUserHttpException();
  72.         }
  73.         return $userId;
  74.     }
  75.     private function massUpsert(Context $context, array $postUpdateConfigs): void
  76.     {
  77.         $userId $this->getUserId($context);
  78.         $userConfigs $this->getOwnUserConfig($contextarray_keys($postUpdateConfigs));
  79.         $userConfigsGroupByKey = [];
  80.         /** @var UserConfigEntity $userConfig */
  81.         foreach ($userConfigs as $userConfig) {
  82.             $userConfigsGroupByKey[$userConfig->getKey()] = $userConfig->getId();
  83.         }
  84.         $queue = new MultiInsertQueryQueue($this->connection250falsetrue);
  85.         foreach ($postUpdateConfigs as $key => $value) {
  86.             $data = [
  87.                 'value' => Json::encode($value),
  88.                 'user_id' => Uuid::fromHexToBytes($userId),
  89.                 'key' => $key,
  90.                 'id' => Uuid::randomBytes(),
  91.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  92.             ];
  93.             if (\array_key_exists($key$userConfigsGroupByKey)) {
  94.                 $data['id'] = Uuid::fromHexToBytes($userConfigsGroupByKey[$key]);
  95.             }
  96.             $queue->addInsert(UserConfigDefinition::ENTITY_NAME$data);
  97.         }
  98.         $queue->execute();
  99.     }
  100. }