|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2020 Vincent Petry <[email protected]> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\Guests\Command; |
| 25 | + |
| 26 | +use OC\Core\Command\Base; |
| 27 | +use OCA\Guests\GuestManager; |
| 28 | +use OCP\IUser; |
| 29 | +use OCP\IUserManager; |
| 30 | +use OCP\Mail\IMailer; |
| 31 | +use Symfony\Component\Console\Input\InputArgument; |
| 32 | +use Symfony\Component\Console\Input\InputInterface; |
| 33 | +use Symfony\Component\Console\Input\InputOption; |
| 34 | +use Symfony\Component\Console\Output\OutputInterface; |
| 35 | +use Symfony\Component\Console\Question\Question; |
| 36 | + |
| 37 | +class AddCommand extends Base { |
| 38 | + private $guestManager; |
| 39 | + private $mailer; |
| 40 | + |
| 41 | + public function __construct(IUserManager $userManager, IMailer $mailer, GuestManager $guestManager) { |
| 42 | + parent::__construct(); |
| 43 | + $this->userManager = $userManager; |
| 44 | + $this->guestManager = $guestManager; |
| 45 | + $this->mailer = $mailer; |
| 46 | + } |
| 47 | + |
| 48 | + protected function configure() { |
| 49 | + $this |
| 50 | + ->setName('guests:add') |
| 51 | + ->setDescription('Add a new guest account') |
| 52 | + ->addArgument( |
| 53 | + 'created-by', |
| 54 | + InputArgument::REQUIRED, |
| 55 | + 'User ID who is set as creator' |
| 56 | + ) |
| 57 | + ->addArgument( |
| 58 | + 'uid', |
| 59 | + InputArgument::REQUIRED, |
| 60 | + 'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)' |
| 61 | + ) |
| 62 | + ->addArgument( |
| 63 | + 'email', |
| 64 | + InputArgument::REQUIRED, |
| 65 | + 'Email address' |
| 66 | + ) |
| 67 | + ->addOption( |
| 68 | + 'generate-password', |
| 69 | + null, |
| 70 | + InputOption::VALUE_NONE, |
| 71 | + 'Do not set an initial password' |
| 72 | + ) |
| 73 | + ->addOption( |
| 74 | + 'password-from-env', |
| 75 | + null, |
| 76 | + InputOption::VALUE_NONE, |
| 77 | + 'Read password from environment variable OC_PASS' |
| 78 | + ) |
| 79 | + ->addOption( |
| 80 | + 'display-name', |
| 81 | + null, |
| 82 | + InputOption::VALUE_OPTIONAL, |
| 83 | + 'User name used in the web UI (can contain any characters)', |
| 84 | + '' |
| 85 | + ) |
| 86 | + ->addOption( |
| 87 | + 'language', |
| 88 | + null, |
| 89 | + InputOption::VALUE_OPTIONAL, |
| 90 | + 'Language', |
| 91 | + '' |
| 92 | + ); |
| 93 | + parent::configure(); |
| 94 | + } |
| 95 | + |
| 96 | + protected function execute(InputInterface $input, OutputInterface $output) { |
| 97 | + $creatorUser = $this->userManager->get($input->getArgument('created-by')); |
| 98 | + if ($creatorUser === null) { |
| 99 | + $output->writeln('<error>The user "' . $input->getArgument('created-by') . '" does not exist.</error>'); |
| 100 | + return 1; |
| 101 | + } |
| 102 | + |
| 103 | + $uid = $input->getArgument('uid'); |
| 104 | + if ($this->userManager->userExists($uid)) { |
| 105 | + $output->writeln('<error>The user "' . $uid . '" already exists.</error>'); |
| 106 | + return 1; |
| 107 | + } |
| 108 | + |
| 109 | + $password = null; |
| 110 | + if (!$input->getOption('generate-password')) { |
| 111 | + if ($input->getOption('password-from-env')) { |
| 112 | + $password = getenv('OC_PASS'); |
| 113 | + if (!$password) { |
| 114 | + $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
| 115 | + return 1; |
| 116 | + } |
| 117 | + } elseif ($input->isInteractive()) { |
| 118 | + /** @var QuestionHelper $helper */ |
| 119 | + $helper = $this->getHelper('question'); |
| 120 | + |
| 121 | + $question = new Question('Enter password: '); |
| 122 | + $question->setHidden(true); |
| 123 | + $password = $helper->ask($input, $output, $question); |
| 124 | + |
| 125 | + $question = new Question('Confirm password: '); |
| 126 | + $question->setHidden(true); |
| 127 | + $confirm = $helper->ask($input, $output,$question); |
| 128 | + |
| 129 | + if ($password !== $confirm) { |
| 130 | + $output->writeln("<error>Passwords did not match!</error>"); |
| 131 | + return 1; |
| 132 | + } |
| 133 | + } else { |
| 134 | + $output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>"); |
| 135 | + return 1; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (!$this->mailer->validateMailAddress($input->getArgument('email'))) { |
| 140 | + $output->writeln('<error>Invalid email address</error>'); |
| 141 | + return 1; |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + $user = $this->guestManager->createGuest( |
| 146 | + $creatorUser, |
| 147 | + $input->getArgument('uid'), |
| 148 | + $input->getArgument('email'), |
| 149 | + $input->getOption('display-name'), |
| 150 | + $input->getOption('language'), |
| 151 | + $password |
| 152 | + ); |
| 153 | + } catch (\Exception $e) { |
| 154 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 155 | + return 1; |
| 156 | + } |
| 157 | + |
| 158 | + if ($user instanceof IUser) { |
| 159 | + $output->writeln('<info>The guest account user "' . $user->getUID() . '" was created successfully</info>'); |
| 160 | + } else { |
| 161 | + $output->writeln('<error>An error occurred while creating the user</error>'); |
| 162 | + return 1; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments