diff --git a/README.md b/README.md
index 56c533d..98ad459 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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`
diff --git a/src/Command/UserDeleteCommand.php b/src/Command/UserDeleteCommand.php
index 084e3b0..bb06e8d 100644
--- a/src/Command/UserDeleteCommand.php
+++ b/src/Command/UserDeleteCommand.php
@@ -3,11 +3,15 @@
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
{
@@ -15,15 +19,15 @@ class UserDeleteCommand extends Command
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;
@@ -31,8 +35,8 @@ public function __construct(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');
}
/**
@@ -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(''.$e->getMessage().'');
+ 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);
}
}
diff --git a/src/Command/UserListCommand.php b/src/Command/UserListCommand.php
index 121df23..032fee1 100644
--- a/src/Command/UserListCommand.php
+++ b/src/Command/UserListCommand.php
@@ -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;
@@ -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;
@@ -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(),
diff --git a/src/Exception/UserNotFoundException.php b/src/Exception/UserNotFoundException.php
new file mode 100644
index 0000000..d46c22a
--- /dev/null
+++ b/src/Exception/UserNotFoundException.php
@@ -0,0 +1,9 @@
+connection = $connection;
- }
-
- /**
- * @return UserCollection
- */
- public function getAll(): UserCollection
- {
- $builder = $this->connection->createQueryBuilder();
- $collection = new UserCollection;
-
- $rows = $builder->select(['*'])
- ->from('user')
- ->execute()
- ->fetchAll();
-
- foreach ($rows as $row) {
- $user = $this->getUserEntityByData($row);
- $collection->add($user);
- }
-
- return $collection;
- }
-
- /**
- * @param string $username
- * @return UserEntity
- */
- public function getByUsername(string $username): UserEntity
- {
- $builder = $this->connection->createQueryBuilder();
- $query = $builder->select('*')
- ->from('user')
- ->where('username = :username')
- ->setParameter('username', $username)
- ->execute();
- $row = $query->fetch();
- return $this->getUserEntityByData($row);
- }
-
- /**
- * @param string $email
- * @return UserEntity
- */
- public function getByEmail(string $email): UserEntity
- {
- $builder = $this->connection->createQueryBuilder();
- $query = $builder->select('*')
- ->from('user')
- ->where('email = :email')
- ->setParameter('email', $email)
- ->execute();
- $row = $query->fetch();
- return $this->getUserEntityByData($row);
- }
-
- /**
- * @param string $username
- * @return bool
- * @throws DBALException
- * @throws InvalidArgumentException
- */
- public function deleteByUsername(string $username): bool
- {
- $user = $this->getByUsername($username);
- $this->delete($user);
- return true;
- }
-
- /**
- * @param string $email
- * @return bool
- * @throws DBALException
- * @throws InvalidArgumentException
- */
- public function deleteByEmail(string $email): bool
- {
- $user = $this->getByEmail($email);
- $this->delete($user);
- return true;
- }
-
- /**
- * @param UserEntity $user
- * @throws DBALException
- * @throws InvalidArgumentException
- */
- public function delete(UserEntity $user)
- {
- $users = $this->getAll();
- if (count($users) <= 1) {
- throw new CannotDeleteLastUserException('Cannot delete last user in this application');
- }
-
- $this->connection->delete('user', ['id' => $user->getId()]);
- }
-
- /**
- * @param array $data
- * @return UserEntity
- */
- private function getUserEntityByData(array $data): UserEntity
- {
- $user = new UserEntity();
- $user->setId($data['id']);
- $user->setUsername((string)$data['username']);
- $user->setFirstName((string)$data['first_name']);
- $user->setLastName((string)$data['last_name']);
- $user->setEmail((string)$data['email']);
- $user->setActive((bool)$data['active']);
- $user->setAdmin((bool)$data['admin']);
- return $user;
- }
-}
diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml
index 5e1adc0..87833da 100644
--- a/src/Resources/config/services.xml
+++ b/src/Resources/config/services.xml
@@ -4,17 +4,13 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
-
+
-
+
-
-
-
-