|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Contacts\Command; |
| 11 | + |
| 12 | +use OCA\Contacts\AppInfo\Application; |
| 13 | +use OCA\Contacts\Service\FederatedInvitesService; |
| 14 | +use OCP\IAppConfig; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Helper\Table; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Read or write the per-deployment OCM invite admin toggles. |
| 23 | + * |
| 24 | + * Usage: |
| 25 | + * occ contacts:ocm-invites-config list all toggles |
| 26 | + * occ contacts:ocm-invites-config <option> print "1" or "0" |
| 27 | + * occ contacts:ocm-invites-config <option> on|off persist a new value |
| 28 | + * |
| 29 | + * The supported options match FederatedInvitesService::OCM_INVITES_BOOL_KEYS |
| 30 | + * so admins can drive the same flags as the settings UI, plus the occ-only |
| 31 | + * SSRF guard override, without needing one command per key. |
| 32 | + */ |
| 33 | +class OcmInvitesConfig extends Command { |
| 34 | + public function __construct( |
| 35 | + private IAppConfig $appConfig, |
| 36 | + ) { |
| 37 | + parent::__construct(); |
| 38 | + } |
| 39 | + |
| 40 | + #[\Override] |
| 41 | + protected function configure(): void { |
| 42 | + $this |
| 43 | + ->setName('contacts:ocm-invites-config') |
| 44 | + ->setDescription('Read or write OCM invite admin toggles.') |
| 45 | + ->addArgument( |
| 46 | + 'option', |
| 47 | + InputArgument::OPTIONAL, |
| 48 | + 'Toggle key (' . implode(', ', FederatedInvitesService::OCM_INVITES_BOOL_KEYS) . '). Omit to list all toggles.', |
| 49 | + ) |
| 50 | + ->addArgument( |
| 51 | + 'value', |
| 52 | + InputArgument::OPTIONAL, |
| 53 | + 'New value (on/off, true/false, 1/0, yes/no). Omit to read the current value.', |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + #[\Override] |
| 58 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 59 | + $option = $input->getArgument('option'); |
| 60 | + $value = $input->getArgument('value'); |
| 61 | + |
| 62 | + if ($option === null) { |
| 63 | + return $this->listAll($output); |
| 64 | + } |
| 65 | + |
| 66 | + if (!in_array($option, FederatedInvitesService::OCM_INVITES_BOOL_KEYS, true)) { |
| 67 | + $output->writeln(sprintf( |
| 68 | + '<error>Unknown OCM invite toggle "%s". Allowed: %s.</error>', |
| 69 | + $option, |
| 70 | + implode(', ', FederatedInvitesService::OCM_INVITES_BOOL_KEYS), |
| 71 | + )); |
| 72 | + return self::FAILURE; |
| 73 | + } |
| 74 | + |
| 75 | + if ($value === null) { |
| 76 | + $current = $this->appConfig->getValueBool(Application::APP_ID, $option); |
| 77 | + $output->writeln($current ? '1' : '0'); |
| 78 | + return self::SUCCESS; |
| 79 | + } |
| 80 | + |
| 81 | + $parsed = $this->parseBool($value); |
| 82 | + if ($parsed === null) { |
| 83 | + $output->writeln(sprintf( |
| 84 | + '<error>Cannot parse "%s" as boolean. Use on/off, true/false, 1/0, or yes/no.</error>', |
| 85 | + $value, |
| 86 | + )); |
| 87 | + return self::INVALID; |
| 88 | + } |
| 89 | + |
| 90 | + $current = $this->appConfig->getValueBool(Application::APP_ID, $option); |
| 91 | + if ($current === $parsed) { |
| 92 | + $output->writeln(sprintf('%s already %s.', $option, $parsed ? 'on' : 'off')); |
| 93 | + return self::SUCCESS; |
| 94 | + } |
| 95 | + |
| 96 | + $this->appConfig->setValueBool(Application::APP_ID, $option, $parsed); |
| 97 | + $output->writeln(sprintf('%s set to %s.', $option, $parsed ? 'on' : 'off')); |
| 98 | + return self::SUCCESS; |
| 99 | + } |
| 100 | + |
| 101 | + private function listAll(OutputInterface $output): int { |
| 102 | + $table = new Table($output); |
| 103 | + $table->setHeaders(['option', 'value']); |
| 104 | + foreach (FederatedInvitesService::OCM_INVITES_BOOL_KEYS as $key) { |
| 105 | + $current = $this->appConfig->getValueBool(Application::APP_ID, $key); |
| 106 | + $table->addRow([$key, $current ? 'on' : 'off']); |
| 107 | + } |
| 108 | + $table->render(); |
| 109 | + return self::SUCCESS; |
| 110 | + } |
| 111 | + |
| 112 | + private function parseBool(string $raw): ?bool { |
| 113 | + $normalised = strtolower(trim($raw)); |
| 114 | + if (in_array($normalised, ['true', '1', 'on', 'yes'], true)) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + if (in_array($normalised, ['false', '0', 'off', 'no'], true)) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | +} |
0 commit comments