Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-8823: Added CLI command to update user #86

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ parameters:
path: src/bundle/Controller/PasswordResetController.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Form\\\\FormInterface\\:\\:getClickedButton\\(\\)\\.$#"
message: "#^Call to an undefined method Symfony\\\\Component\\\\Form\\\\FormInterface\\<mixed\\>\\:\\:getClickedButton\\(\\)\\.$#"
count: 4
path: src/bundle/Controller/UserRegisterController.php

Expand Down
119 changes: 119 additions & 0 deletions src/bundle/Command/UpdateUserCommand.php
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\User\Command;

use Ibexa\Contracts\Core\Repository\Repository;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Ibexa\Contracts\Core\Repository\Values\User\UserUpdateStruct;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
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\Style\SymfonyStyle;

#[AsCommand(name: 'ibexa:user:update-user', description: 'Updates basic user data.')]
final class UpdateUserCommand extends Command
{
public function __construct(
private readonly UserService $userService,
private readonly Repository $repository,
?string $name = null
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
) {
parent::__construct($name);
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
}

protected function configure(): void
{
$this->addArgument(
'user',
InputArgument::REQUIRED,
'User reference (id or login)',
);
$this->addOption(
'password',
null,
InputOption::VALUE_NONE,
'New plaintext password (type will be in a "hidden" mode)',
);
$this->addOption(
'email',
null,
InputOption::VALUE_REQUIRED,
'New e-mail address',
);
$this->addOption(
'enable',
null,
InputOption::VALUE_NONE,
'Flag enabling the user being updated',
);
$this->addOption(
'disable',
null,
InputOption::VALUE_NONE,
'Flag disabling the user being updated',
);
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$userReference = $input->getArgument('user');
$password = $input->getOption('password');
$enable = $input->getOption('enable');
$disable = $input->getOption('disable');
$email = $input->getOption('email');

if (!$password && !$enable && !$disable && $email === null) {
$io->success('No new user data specified, exiting.');

return Command::SUCCESS;
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
}

if (is_numeric($userReference)) {
$user = $this->userService->loadUser((int)$userReference);
} else {
$user = $this->userService->loadUserByLogin($userReference);
}
konradoboza marked this conversation as resolved.
Show resolved Hide resolved

if ($enable && $disable) {
$io->error('--enable and --disable options cannot be used simultaneously.');

return Command::FAILURE;
}

if ($password) {
$password = $io->askHidden('Password (your type will be hidden)');
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
$input->setOption('password', $password);
}

$userUpdateStruct = new UserUpdateStruct();
$userUpdateStruct->password = $input->getOption('password');
$userUpdateStruct->email = $email;
$userUpdateStruct->enabled = $enable === true || !$disable;
konradoboza marked this conversation as resolved.
Show resolved Hide resolved

$this->repository->sudo(
function () use ($user, $userUpdateStruct): User {
return $this->userService->updateUser($user, $userUpdateStruct);
}
);

$io->success('User was successfully updated.');
konradoboza marked this conversation as resolved.
Show resolved Hide resolved

return Command::SUCCESS;
}
}
Loading