Skip to content

Commit

Permalink
Use core repository instead of custom one
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Nov 16, 2020
1 parent 4aba16f commit f4ddb2f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 174 deletions.
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Shopware 6 additional user commands
Shopware 6 plugin adding additional user commands to the CLI. The default console only supports two commands (`user:create` and `user:change-password`). This plugin adds the commands `user:list` and `user:delete`. Additionally, this plugin offers a `UserRepository` with handy methods.
Shopware 6 plugin adding additional user commands to the CLI. The default console only supports two commands (`user:create` and `user:change-password`). This plugin adds the commands `user:list` and `user:delete`.

## Installation
```bash
Expand All @@ -12,16 +12,6 @@ bin/console cache:clear
## Usage
```bash
bin/console user:list
bin/console user:delete admin
bin/console user:delete admin@shopware.com
bin/console user:delete --username=admin
bin/console user:delete --email=admin@shopware.com
```

## Programming with the UserRepository
Inject the `\YireoAdditionalUserCommands\Repository\UserRepository` into your code and use its methods:

- `getAll`
- `getByUsername`
- `getByEmail`
- `deleteByUsername`
- `deleteByEmail`
- `delete`
53 changes: 41 additions & 12 deletions src/Command/UserDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,40 @@
namespace YireoAdditionalUserCommands\Command;

use InvalidArgumentException;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\Console\Input\InputArgument;
use YireoAdditionalUserCommands\Repository\UserRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use YireoAdditionalUserCommands\Exception\UserNotFoundException;

class UserDeleteCommand extends Command
{
protected static $defaultName = 'user:delete';
protected static $defaultDescription = 'Delete a specific user';

/**
* @var UserRepository
* @var EntityRepositoryInterface
*/
private $userRepository;

/**
* UserListCommand constructor.
* @param UserRepository $userRepository
* @param EntityRepositoryInterface $userRepository
*/
public function __construct(UserRepository $userRepository)
public function __construct(EntityRepositoryInterface $userRepository)
{
parent::__construct();
$this->userRepository = $userRepository;
}

protected function configure()
{
$this->addArgument('email', InputArgument::OPTIONAL, 'Email of user to delete');
$this->addArgument('username', InputArgument::OPTIONAL, 'Username of user to delete');
$this->addOption('email', false, InputArgument::OPTIONAL, 'Email of user to delete');
$this->addOption('username', false, InputArgument::OPTIONAL, 'Username of user to delete');
}

/**
Expand All @@ -42,19 +46,44 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$email = $input->getArgument('email');
$username = $input->getArgument('username');
$email = $input->getOption('email');
$username = $input->getOption('username');

if (empty($email) && empty($username)) {
throw new InvalidArgumentException('Either enter a username or an email');
}

if ($email) {
$this->userRepository->deleteByEmail($email);
$fieldName = $email ? 'email' : 'username';
$fieldValue = $email ? $email : $username;

try {
$this->deleteUserByField($fieldName, $fieldValue);
$output->writeln('Deleted user with '.$fieldName.' "'.$fieldValue.'"');
return 1;
} catch (UserNotFoundException $e) {
$output->writeln('<error>'.$e->getMessage().'</error>');
return 0;
}
}

/**
* @param string $field
* @param string $value
* @throws UserNotFoundException
*/
public function deleteUserByField(string $field, string $value)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter($field, $value));
$context = Context::createDefaultContext();
$result = $this->userRepository->search($criteria, $context);
$userIds = $result->getIds();

if (empty($userIds)) {
throw new UserNotFoundException('No such user with '.$field.' "'.$value.'"');
}

$this->userRepository->deleteByUsername($username);
return 1;
$userId = array_shift($userIds);
$this->userRepository->delete([['id' => $userId]], $context);
}
}
14 changes: 9 additions & 5 deletions src/Command/UserListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace YireoAdditionalUserCommands\Command;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\User\UserEntity;
use Symfony\Component\Console\Helper\Table;
use YireoAdditionalUserCommands\Repository\UserRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -15,15 +17,15 @@ class UserListCommand extends Command
protected static $defaultDescription = 'Show a listing of all current users';

/**
* @var UserRepository
* @var EntityRepositoryInterface
*/
private $userRepository;

/**
* UserListCommand constructor.
* @param UserRepository $userRepository
* @param EntityRepositoryInterface $userRepository
*/
public function __construct(UserRepository $userRepository)
public function __construct(EntityRepositoryInterface $userRepository)
{
parent::__construct();
$this->userRepository = $userRepository;
Expand All @@ -39,7 +41,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$headers = ['Username', 'First name', 'Last name', 'Email', 'Active', 'Admin'];
$rows = [];

foreach ($this->userRepository->getAll() as $user) {
$criteria = new Criteria();
$context = Context::createDefaultContext();
foreach ($this->userRepository->search($criteria, $context) as $user) {
/** @var UserEntity $user */
$rows[] = [
$user->getUsername(),
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/UserNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace YireoAdditionalUserCommands\Exception;

use Exception;

class UserNotFoundException extends Exception
{
}
138 changes: 0 additions & 138 deletions src/Repository/UserRepository.php

This file was deleted.

8 changes: 2 additions & 6 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="YireoAdditionalUserCommands\Command\UserListCommand">
<argument type="service" id="YireoAdditionalUserCommands\Repository\UserRepository"/>
<argument type="service" id="user.repository"/>
<tag name="console.command"/>
</service>

<service id="YireoAdditionalUserCommands\Command\UserDeleteCommand">
<argument type="service" id="YireoAdditionalUserCommands\Repository\UserRepository"/>
<argument type="service" id="user.repository"/>
<tag name="console.command"/>
</service>

<service id="YireoAdditionalUserCommands\Repository\UserRepository">
<argument type="service" id="Doctrine\DBAL\Connection" />
</service>
</services>
</container>

0 comments on commit f4ddb2f

Please sign in to comment.