Skip to content

Commit

Permalink
Add contao:member:delete command
Browse files Browse the repository at this point in the history
Update README
  • Loading branch information
a-v-l committed Feb 1, 2024
1 parent fa77d70 commit 2275c44
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

[![Packagist Version](https://img.shields.io/packagist/v/a-v-l/contao-member-console)](https://packagist.org/packages/a-v-l/contao-member-console)

Adds `contao:member:create` and `contao:member:password` commads to [Contao-CLI](https://docs.contao.org/manual/en/cli/) to create front end users or reset passwords.
Adds `contao:member:create`, `contao:member:password` and `contao:member:delete` commads to [Contao-CLI](https://docs.contao.org/manual/en/cli/) to create front end users or reset passwords.

The command can either be run without flags in interactive mode or with required flags and `-n` (no interaction).

**Note:**
`contao:member:delete` without the `-n` flag will first print information about the given member and then ask for confirmation to delete the member.

## Usage

### Options for `contao:member:create`
Expand All @@ -19,7 +22,7 @@ The command can either be run without flags in interactive mode or with required
| --group[=GROUP] | The groups to assign the user to (optional) (multiple values allowed) |
| -n, --no-interaction | Do not ask any interactive question |

### Arguments for `contao:member:password`
### Arguments for `contao:member:password` and `contao:member:delete`
| Argument | Description |
|-------------------------|------------------------|
| username | The username of the front end user |
Expand Down
122 changes: 122 additions & 0 deletions src/Command/MemberDeleteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);

namespace AVL\MemberConsole\Command;

use Contao\MemberModel;
use Contao\MemberGroupModel;
use Contao\CoreBundle\Framework\ContaoFramework;
use Doctrine\DBAL\Connection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Delete Contao front end user.
*
* @internal
*/
class MemberDeleteCommand extends Command
{
protected static $defaultName = 'contao:member:delete';
protected static $defaultDescription = 'Delete Contao front end user.';

private ContaoFramework $framework;
private Connection $connection;
private Bool $delete = true;

public function __construct(ContaoFramework $framework, Connection $connection)
{
$this->framework = $framework;
$this->connection = $connection;

parent::__construct();
}

protected function configure(): void
{
$this
->addArgument('username', InputArgument::REQUIRED, 'The username of the front end user')
;
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
if (null === $input->getArgument('username')) {
throw new InvalidArgumentException('Please provide the username as argument.');
}
}

protected function execute(InputInterface $input, OutputInterface $output): int
{

$member = $this->getMember($input->getArgument('username'));

$io = new SymfonyStyle($input, $output);
$io->text(sprintf("Member '%s' found:", $input->getArgument('username')));
$io->table(
["id", "firstname", "lastname", "email", "group" . (count($member['groups_names']) > 1 ? 's' : '')],
[[$member['id'], $member['firstname'], $member['lastname'], $member['email'], implode(', ', $member['groups_names'])]]
);

if (false === $input->getOption('no-interaction')) {
$answer = $this->askChoice('Delete member?', ['no', 'yes'], $input, $output);

if('no' === $answer) {
return 0;
}
}

$this->connection->delete(
'tl_member',
['username' => $input->getArgument('username')]
);

$io->success(sprintf('Member %s deleted!', $input->getArgument('username')));

return 0;
}

private function askChoice(string $label, array $options, InputInterface $input, OutputInterface $output): string
{
$question = new ChoiceQuestion($label, $options);
$question->setAutocompleterValues($options);

/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');

return $helper->ask($input, $output, $question);
}

private function getMember(string $username): array
{
$this->framework->initialize();

$memberModel = $this->framework->getAdapter(MemberModel::class);
$member = $memberModel->findByUsername($username);

if (null === $member) {
throw new InvalidArgumentException(sprintf('Invalid username: %s', $username));
}

$groups = $member->getRelated('groups');
$groups_names = [];
foreach ($groups as $group) {
$groups_names[] = $group->name;
}

return [
'id' => $member->id,
'firstname' => $member->firstname,
'lastname' => $member->lastname,
'email' => $member->email,
'groups_names' => $groups_names
];
}
}
8 changes: 7 additions & 1 deletion src/Resources/config/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ services:
arguments:
- '@contao.framework'
- '@database_connection'
- '@security.password_hasher_factory'
- '@security.password_hasher_factory'

contao.command.member_delete:
class: AVL\MemberConsole\Command\MemberDeleteCommand
arguments:
- '@contao.framework'
- '@database_connection'

0 comments on commit 2275c44

Please sign in to comment.