custom/plugins/CompraFoundationSW6/src/CompraFoundationSW6.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\FoundationSW6;
  3. use Compra\EsiSW6\Core\System\Service\ApiService;
  4. use Shopware\Core\Framework\Api\Context\SystemSource;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Shopware\Core\System\User\UserEntity;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. class CompraFoundationSW6 extends Plugin
  15. {
  16.     public const API_USER_LOCALE 'de-DE';
  17.     public const API_USER 'CompraApiAdmin';
  18.     public const API_USER_PASSWORD 'elrond.1';
  19.     public const API_USER_SCOPE 'write';
  20.     public const API_USER_POSSIBLE_EMAILS = [
  21.         'webteam@compra.de',
  22.         'webdev@compra.de',
  23.         'websupport@compra.de'
  24.     ];
  25.     public const API_USER_FALLBACK_EMAIL 'webteam%random_int%@compra.de';
  26.     public function build(ContainerBuilder $container): void
  27.     {
  28.         $container->setParameter('compra_foundation_s_w6.plugin_dir'$this->getPath());
  29.         parent::build($container);
  30.     }
  31.     public function install(InstallContext $installContext): void
  32.     {
  33.         parent::install($installContext);
  34.         $this->createAdministrationUser($installContext->getContext());
  35.     }
  36.     public function update(UpdateContext $updateContext): void
  37.     {
  38.         parent::update($updateContext);
  39.         $this->createAdministrationUser($updateContext->getContext());
  40.     }
  41.     /**
  42.      * Helper function to get the ID of the ESI user.
  43.      * Return null if no ESI user exists.
  44.      *
  45.      * @param Context $context
  46.      */
  47.     public function createAdministrationUser(Context $context): void
  48.     {
  49.         /** @var EntityRepositoryInterface $userRepository */
  50.         $userRepository $this->container->get('user.repository');
  51.         if (!$userRepository) {
  52.             throw new \RuntimeException('Unable to load service user.repository');
  53.         }
  54.         $usersResult $userRepository->search(new Criteria(), $context);
  55.         $email null;
  56.         // check which email address we can use, because there could already be a user with one of our possible addresses
  57.         foreach (self::API_USER_POSSIBLE_EMAILS as $possibleEmail) {
  58.             $result $usersResult->filterByProperty('email'$possibleEmail)->first();
  59.             if (!$result) {
  60.                 // no user with this email address, so we can use it
  61.                 $email $possibleEmail;
  62.                 break;
  63.             }
  64.         }
  65.         if (!$email) {
  66.             $email str_replace('%random_int%', (string) random_int(1000099999), self::API_USER_FALLBACK_EMAIL);
  67.         }
  68.         /** @var UserEntity[] $users */
  69.         $users $usersResult->getElements();
  70.         $userId null;
  71.         /*
  72.          * Iterate through all users and check if the CompraAiAdmin user already exists
  73.          */
  74.         foreach ($users as $id => $user) {
  75.             if ($user->getUsername() === self::API_USER) {
  76.                 $userId $id;
  77.                 break;
  78.             }
  79.         }
  80.         if (!$userId) {
  81.             // GET LOCALE ID FOR API ADMIN
  82.             /** @var EntityRepositoryInterface|null $localeRepo */
  83.             $localeRepo $this->container->get('locale.repository');
  84.             if (!$localeRepo) {
  85.                 throw new \RuntimeException('Unable to load service locale.repository');
  86.             }
  87.             $localeCriteria = new Criteria();
  88.             $localeCriteria->addFilter(new EqualsFilter('code'self::API_USER_LOCALE));
  89.             $localeId $localeRepo->searchIds($localeCriteria$context)->firstId();
  90.             // CREATE USER PAYLOAD
  91.             $apiUserPayload = [
  92.                 [
  93.                     'username' => self::API_USER,
  94.                     'password' => self::API_USER_PASSWORD,
  95.                     'firstName' => 'COMPRA API',
  96.                     'lastName' => 'Admin',
  97.                     'active' => true,
  98.                     'admin' => true,
  99.                     'email' => $email,
  100.                     'localeId' => $localeId
  101.                 ]
  102.             ];
  103.             // CREATE THE USER
  104.             $userRepository->upsert($apiUserPayload, new Context(new SystemSource()));
  105.         }
  106.     }
  107. }