|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | +namespace OCA\User_SAML\Command; |
| 9 | + |
| 10 | +use OC\Core\Command\Base; |
| 11 | +use OCA\User_SAML\UserBackend; |
| 12 | +use OCP\IUserManager; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | +use Symfony\Component\Console\Input\InputArgument; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +class UserAdd extends Base { |
| 20 | + public function __construct( |
| 21 | + protected IUserManager $userManager, |
| 22 | + protected UserBackend $backend, |
| 23 | + private LoggerInterface $logger, |
| 24 | + ) { |
| 25 | + parent::__construct(); |
| 26 | + } |
| 27 | + protected function configure(): void { |
| 28 | + $this |
| 29 | + ->setName('saml:user:add') |
| 30 | + ->setDescription('Add a SAML account') |
| 31 | + ->addArgument( |
| 32 | + 'uid', |
| 33 | + InputArgument::REQUIRED, |
| 34 | + 'Account ID as provided by the IdP (must only contain a-z, A-Z, 0-9, -, _ and @)' |
| 35 | + ) |
| 36 | + ->addOption( |
| 37 | + 'display-name', |
| 38 | + null, |
| 39 | + InputOption::VALUE_REQUIRED, |
| 40 | + 'Name as presented in the web interface (can contain any characters)' |
| 41 | + ) |
| 42 | + ->addOption( |
| 43 | + 'email', |
| 44 | + null, |
| 45 | + InputOption::VALUE_OPTIONAL, |
| 46 | + 'Set user default email in user profile' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 51 | + $uid = $input->getArgument('uid'); |
| 52 | + |
| 53 | + if ($this->userManager->userExists($uid)) { |
| 54 | + $output->writeln('<error>The account "' . $uid . '" already exists.</error>'); |
| 55 | + return 1; |
| 56 | + } |
| 57 | + |
| 58 | + if (!$output->isQuiet()) { |
| 59 | + $output->writeln('<info>The account "' . $uid . '" is to be added to the SAML backend.</info>'); |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + $this->backend->createUserIfNotExists($uid); |
| 64 | + } catch (\Exception $e) { |
| 65 | + $output->writeln('<error>SAML create user ' . $e->getMessage() . '</error>'); |
| 66 | + return 1; |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + $this->backend->setDisplayName($uid, $input->getOption('display-name')); |
| 71 | + $email = $input->getOption('email'); |
| 72 | + if (!empty($email)) { |
| 73 | + $user = $this->userManager->get($uid); |
| 74 | + $user->setSystemEMailAddress($email); |
| 75 | + } |
| 76 | + } catch (\Exception $e) { |
| 77 | + $output->writeln('<error>SAML create user Email and DisplayName ' . $e->getMessage() . '</error>'); |
| 78 | + return 1; |
| 79 | + } |
| 80 | + |
| 81 | + if (!$output->isQuiet()) { |
| 82 | + $output->writeln('<info>SAML user "' . $uid . '" added.</info>'); |
| 83 | + } |
| 84 | + |
| 85 | + return 0; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments