From ba23c098df94a14120fea82554aafeb7d9666434 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Tue, 27 Aug 2024 11:46:44 +0200 Subject: [PATCH] IBX-8823: Added CLI command to update user --- phpstan-baseline.neon | 2 +- src/bundle/Command/UpdateUserCommand.php | 119 +++++++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/bundle/Command/UpdateUserCommand.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index aa56959..e7981c9 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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\\\\:\\:getClickedButton\\(\\)\\.$#" count: 4 path: src/bundle/Controller/UserRegisterController.php diff --git a/src/bundle/Command/UpdateUserCommand.php b/src/bundle/Command/UpdateUserCommand.php new file mode 100644 index 0000000..db76ce5 --- /dev/null +++ b/src/bundle/Command/UpdateUserCommand.php @@ -0,0 +1,119 @@ +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; + } + + if (is_numeric($userReference)) { + $user = $this->userService->loadUser((int)$userReference); + } else { + $user = $this->userService->loadUserByLogin($userReference); + } + + 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)'); + $input->setOption('password', $password); + } + + $userUpdateStruct = new UserUpdateStruct(); + $userUpdateStruct->password = $input->getOption('password'); + $userUpdateStruct->email = $email; + $userUpdateStruct->enabled = $enable === true || !$disable; + + $this->repository->sudo( + function () use ($user, $userUpdateStruct): User { + return $this->userService->updateUser($user, $userUpdateStruct); + } + ); + + $io->success('User was successfully updated.'); + + return Command::SUCCESS; + } +}