|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OC\Core\Command\User; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OCP\Accounts\IAccount; |
| 13 | +use OCP\Accounts\IAccountManager; |
| 14 | +use OCP\Accounts\PropertyDoesNotExistException; |
| 15 | +use OCP\IUser; |
| 16 | +use OCP\IUserManager; |
| 17 | +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Input\InputOption; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
| 22 | + |
| 23 | +class Profile extends Base { |
| 24 | + public function __construct( |
| 25 | + protected IUserManager $userManager, |
| 26 | + protected IAccountManager $accountManager, |
| 27 | + ) { |
| 28 | + parent::__construct(); |
| 29 | + } |
| 30 | + |
| 31 | + protected function configure() { |
| 32 | + parent::configure(); |
| 33 | + $this |
| 34 | + ->setName('user:profile') |
| 35 | + ->setDescription('Read and modify user profile properties') |
| 36 | + ->addArgument( |
| 37 | + 'uid', |
| 38 | + InputArgument::REQUIRED, |
| 39 | + 'Account ID used to login' |
| 40 | + ) |
| 41 | + ->addArgument( |
| 42 | + 'key', |
| 43 | + InputArgument::OPTIONAL, |
| 44 | + 'Profile property to set, get or delete', |
| 45 | + '' |
| 46 | + ) |
| 47 | + |
| 48 | + // Get |
| 49 | + ->addOption( |
| 50 | + 'default-value', |
| 51 | + null, |
| 52 | + InputOption::VALUE_REQUIRED, |
| 53 | + '(Only applicable on get) If no default value is set and the property does not exist, the command will exit with 1' |
| 54 | + ) |
| 55 | + |
| 56 | + // Set |
| 57 | + ->addArgument( |
| 58 | + 'value', |
| 59 | + InputArgument::OPTIONAL, |
| 60 | + 'The new value of the property', |
| 61 | + null |
| 62 | + ) |
| 63 | + ->addOption( |
| 64 | + 'update-only', |
| 65 | + null, |
| 66 | + InputOption::VALUE_NONE, |
| 67 | + 'Only updates the value, if it is not set before, it is not being added' |
| 68 | + ) |
| 69 | + |
| 70 | + // Delete |
| 71 | + ->addOption( |
| 72 | + 'delete', |
| 73 | + null, |
| 74 | + InputOption::VALUE_NONE, |
| 75 | + 'Specify this option to delete the property value' |
| 76 | + ) |
| 77 | + ->addOption( |
| 78 | + 'error-if-not-exists', |
| 79 | + null, |
| 80 | + InputOption::VALUE_NONE, |
| 81 | + 'Checks whether the property exists before deleting it' |
| 82 | + ) |
| 83 | + ; |
| 84 | + } |
| 85 | + |
| 86 | + protected function checkInput(InputInterface $input): IUser { |
| 87 | + $uid = $input->getArgument('uid'); |
| 88 | + $user = $this->userManager->get($uid); |
| 89 | + if (!$user) { |
| 90 | + throw new \InvalidArgumentException('The user "' . $uid . '" does not exist.'); |
| 91 | + } |
| 92 | + // normalize uid |
| 93 | + $input->setArgument('uid', $user->getUID()); |
| 94 | + |
| 95 | + $key = $input->getArgument('key'); |
| 96 | + if ($key === '') { |
| 97 | + if ($input->hasParameterOption('--default-value')) { |
| 98 | + throw new \InvalidArgumentException('The "default-value" option can only be used when specifying a key.'); |
| 99 | + } |
| 100 | + if ($input->getArgument('value') !== null) { |
| 101 | + throw new \InvalidArgumentException('The value argument can only be used when specifying a key.'); |
| 102 | + } |
| 103 | + if ($input->getOption('delete')) { |
| 104 | + throw new \InvalidArgumentException('The "delete" option can only be used when specifying a key.'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if ($input->getArgument('value') !== null && $input->hasParameterOption('--default-value')) { |
| 109 | + throw new \InvalidArgumentException('The value argument can not be used together with "default-value".'); |
| 110 | + } |
| 111 | + if ($input->getOption('update-only') && $input->getArgument('value') === null) { |
| 112 | + throw new \InvalidArgumentException('The "update-only" option can only be used together with "value".'); |
| 113 | + } |
| 114 | + |
| 115 | + if ($input->getOption('delete') && $input->hasParameterOption('--default-value')) { |
| 116 | + throw new \InvalidArgumentException('The "delete" option can not be used together with "default-value".'); |
| 117 | + } |
| 118 | + if ($input->getOption('delete') && $input->getArgument('value') !== null) { |
| 119 | + throw new \InvalidArgumentException('The "delete" option can not be used together with "value".'); |
| 120 | + } |
| 121 | + if ($input->getOption('error-if-not-exists') && !$input->getOption('delete')) { |
| 122 | + throw new \InvalidArgumentException('The "error-if-not-exists" option can only be used together with "delete".'); |
| 123 | + } |
| 124 | + |
| 125 | + return $user; |
| 126 | + } |
| 127 | + |
| 128 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 129 | + try { |
| 130 | + $user = $this->checkInput($input); |
| 131 | + } catch (\InvalidArgumentException $e) { |
| 132 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 133 | + return self::FAILURE; |
| 134 | + } |
| 135 | + |
| 136 | + $uid = $input->getArgument('uid'); |
| 137 | + $key = $input->getArgument('key'); |
| 138 | + $userAccount = $this->accountManager->getAccount($user); |
| 139 | + |
| 140 | + if ($key === '') { |
| 141 | + $settings = $this->getAllProfileProperties($userAccount); |
| 142 | + $this->writeArrayInOutputFormat($input, $output, $settings); |
| 143 | + return self::SUCCESS; |
| 144 | + } |
| 145 | + |
| 146 | + $value = $this->getStoredValue($userAccount, $key); |
| 147 | + $inputValue = $input->getArgument('value'); |
| 148 | + if ($inputValue !== null) { |
| 149 | + if ($input->hasParameterOption('--update-only') && $value === null) { |
| 150 | + $output->writeln('<error>The property does not exist for user "' . $uid . '".</error>'); |
| 151 | + return self::FAILURE; |
| 152 | + } |
| 153 | + |
| 154 | + return $this->editProfileProperty($output, $userAccount, $key, $inputValue); |
| 155 | + } elseif ($input->hasParameterOption('--delete')) { |
| 156 | + if ($input->hasParameterOption('--error-if-not-exists') && $value === null) { |
| 157 | + $output->writeln('<error>The property does not exist for user "' . $uid . '".</error>'); |
| 158 | + return self::FAILURE; |
| 159 | + } |
| 160 | + |
| 161 | + return $this->deleteProfileProperty($output, $userAccount, $key); |
| 162 | + } elseif ($value !== null) { |
| 163 | + $output->writeln($value); |
| 164 | + } elseif ($input->hasParameterOption('--default-value')) { |
| 165 | + $output->writeln($input->getOption('default-value')); |
| 166 | + } else { |
| 167 | + $output->writeln('<error>The property does not exist for user "' . $uid . '".</error>'); |
| 168 | + return self::FAILURE; |
| 169 | + } |
| 170 | + |
| 171 | + return self::SUCCESS; |
| 172 | + } |
| 173 | + |
| 174 | + private function deleteProfileProperty(OutputInterface $output, IAccount $userAccount, string $key): int { |
| 175 | + return $this->editProfileProperty($output, $userAccount, $key, ''); |
| 176 | + } |
| 177 | + |
| 178 | + private function editProfileProperty(OutputInterface $output, IAccount $userAccount, string $key, string $value): int { |
| 179 | + try { |
| 180 | + $userAccount->getProperty($key)->setValue($value); |
| 181 | + } catch (PropertyDoesNotExistException $exception) { |
| 182 | + $output->writeln('<error>' . $exception->getMessage() . '</error>'); |
| 183 | + return self::FAILURE; |
| 184 | + } |
| 185 | + |
| 186 | + $this->accountManager->updateAccount($userAccount); |
| 187 | + return self::SUCCESS; |
| 188 | + } |
| 189 | + |
| 190 | + private function getStoredValue(IAccount $userAccount, string $key): ?string { |
| 191 | + try { |
| 192 | + $property = $userAccount->getProperty($key); |
| 193 | + } catch (PropertyDoesNotExistException) { |
| 194 | + return null; |
| 195 | + } |
| 196 | + return $property->getValue() === '' ? null : $property->getValue(); |
| 197 | + } |
| 198 | + |
| 199 | + private function getAllProfileProperties(IAccount $userAccount): array { |
| 200 | + $properties = []; |
| 201 | + |
| 202 | + foreach ($userAccount->getAllProperties() as $property) { |
| 203 | + if ($property->getValue() !== '') { |
| 204 | + $properties[$property->getName()] = $property->getValue(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return $properties; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * @param string $argumentName |
| 213 | + * @param CompletionContext $context |
| 214 | + * @return string[] |
| 215 | + */ |
| 216 | + public function completeArgumentValues($argumentName, CompletionContext $context): array { |
| 217 | + if ($argumentName === 'uid') { |
| 218 | + return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord())); |
| 219 | + } |
| 220 | + if ($argumentName === 'key') { |
| 221 | + $userId = $context->getWordAtIndex($context->getWordIndex() - 1); |
| 222 | + $user = $this->userManager->get($userId); |
| 223 | + if (!($user instanceof IUser)) { |
| 224 | + return []; |
| 225 | + } |
| 226 | + |
| 227 | + $account = $this->accountManager->getAccount($user); |
| 228 | + |
| 229 | + $properties = $this->getAllProfileProperties($account); |
| 230 | + return array_keys($properties); |
| 231 | + } |
| 232 | + return []; |
| 233 | + } |
| 234 | +} |
0 commit comments