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

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