|
| 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\ConfigLexicon; |
| 14 | +use OCA\Contacts\Service\FederatedInvitesService; |
| 15 | +use OCP\IAppConfig; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Helper\Table; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Manage OCM invite configuration. |
| 24 | + * |
| 25 | + * Usage: |
| 26 | + * occ contacts:ocm-invites-config |
| 27 | + * occ contacts:ocm-invites-config <key> |
| 28 | + * occ contacts:ocm-invites-config <key> <value> |
| 29 | + * |
| 30 | + * Boolean values accept on/off, true/false, 1/0, and yes/no. |
| 31 | + */ |
| 32 | +class OcmInvitesConfig extends Command { |
| 33 | + public function __construct( |
| 34 | + private IAppConfig $appConfig, |
| 35 | + ) { |
| 36 | + parent::__construct(); |
| 37 | + } |
| 38 | + |
| 39 | + #[\Override] |
| 40 | + protected function configure(): void { |
| 41 | + $supportedOptions = implode(', ', $this->getSupportedOptions()); |
| 42 | + $help = <<<HELP |
| 43 | +The <info>%command.name%</info> command reads and writes Contacts OCM invite settings. |
| 44 | +
|
| 45 | +Supported keys: |
| 46 | + {$supportedOptions} |
| 47 | +
|
| 48 | +Key info: |
| 49 | + <info>ocm_invites_enabled</info> - If set to true then the capability to send and accept invitations to exchange contact info is enabled in the app - bool, default false |
| 50 | + <info>ocm_invites_optional_mail</info> - If set to true then sending an invitation by email is optional - bool, default false |
| 51 | + <info>ocm_invites_cc_sender</info> - If set to true then the option to send a copy of the invitation to the sender is displayed - bool, default true |
| 52 | + <info>ocm_invites_encoded_copy_button</info> - If set to true then the button to copy the encoded invitation is displayed - bool, default false |
| 53 | + <info>ocm_invites_disable_ssrf_guard</info> - If set to true SSRF guard will be turned off. Warning: This is for development/testing purposes only! |
| 54 | + In production environments the value of this key should always be false - bool, default false |
| 55 | + <info>mesh_providers_service</info> - The url that returns the list of mesh providers that will be displayed on the WAYF page - string, default empty |
| 56 | +
|
| 57 | +Boolean values accept: on/off, true/false, 1/0, yes/no. |
| 58 | +
|
| 59 | +Examples: |
| 60 | + <info>occ %command.name%</info> |
| 61 | + <info>occ %command.name% ocm_invites_enabled on</info> |
| 62 | + <info>occ %command.name% mesh_providers_service "https://mesh.example"</info> |
| 63 | +
|
| 64 | +HELP; |
| 65 | + |
| 66 | + $this |
| 67 | + ->setName('contacts:ocm-invites-config') |
| 68 | + ->setDescription('Manage OCM invite configuration.') |
| 69 | + ->addArgument( |
| 70 | + 'option', |
| 71 | + InputArgument::OPTIONAL, |
| 72 | + 'Config key to read or write. Omit to list supported keys.', |
| 73 | + ) |
| 74 | + ->addArgument( |
| 75 | + 'value', |
| 76 | + InputArgument::OPTIONAL, |
| 77 | + 'Value to write. Omit to read the current value.', |
| 78 | + ) |
| 79 | + ->setHelp($help); |
| 80 | + } |
| 81 | + |
| 82 | + #[\Override] |
| 83 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 84 | + $option = $input->getArgument('option'); |
| 85 | + $value = $input->getArgument('value'); |
| 86 | + |
| 87 | + if ($option === null) { |
| 88 | + return $this->listAll($output); |
| 89 | + } |
| 90 | + |
| 91 | + if (!in_array($option, $this->getSupportedOptions(), true)) { |
| 92 | + $output->writeln(sprintf( |
| 93 | + '<error>Unknown OCM invite config key "%s". Allowed: %s.</error>', |
| 94 | + $option, |
| 95 | + implode(', ', $this->getSupportedOptions()), |
| 96 | + )); |
| 97 | + return self::FAILURE; |
| 98 | + } |
| 99 | + |
| 100 | + if ($value === null) { |
| 101 | + $output->writeln($this->getCurrentValue($option)); |
| 102 | + return self::SUCCESS; |
| 103 | + } |
| 104 | + |
| 105 | + if ($option === ConfigLexicon::MESH_PROVIDERS_SERVICE) { |
| 106 | + $normalised = trim($value); |
| 107 | + $current = $this->appConfig->getValueString(Application::APP_ID, $option); |
| 108 | + if ($current === $normalised) { |
| 109 | + $output->writeln(sprintf('%s is already "%s".', $option, $normalised)); |
| 110 | + return self::SUCCESS; |
| 111 | + } |
| 112 | + |
| 113 | + $this->appConfig->setValueString(Application::APP_ID, $option, $normalised); |
| 114 | + $output->writeln(sprintf('%s: "%s"', $option, $normalised)); |
| 115 | + return self::SUCCESS; |
| 116 | + } |
| 117 | + |
| 118 | + $parsed = $this->parseBool($value); |
| 119 | + if ($parsed === null) { |
| 120 | + $output->writeln(sprintf( |
| 121 | + '<error>Cannot parse "%s" as boolean. Use on/off, true/false, 1/0, or yes/no.</error>', |
| 122 | + $value, |
| 123 | + )); |
| 124 | + return self::INVALID; |
| 125 | + } |
| 126 | + |
| 127 | + $current = $this->appConfig->getValueBool(Application::APP_ID, $option); |
| 128 | + if ($current === $parsed) { |
| 129 | + $output->writeln(sprintf('%s is already %s.', $option, $this->formatBool($parsed))); |
| 130 | + return self::SUCCESS; |
| 131 | + } |
| 132 | + |
| 133 | + $this->appConfig->setValueBool(Application::APP_ID, $option, $parsed); |
| 134 | + $output->writeln(sprintf('%s: %s', $option, $this->formatBool($parsed))); |
| 135 | + return self::SUCCESS; |
| 136 | + } |
| 137 | + |
| 138 | + private function listAll(OutputInterface $output): int { |
| 139 | + $table = new Table($output); |
| 140 | + $table->setHeaders(['option', 'type', 'value']); |
| 141 | + foreach ($this->getSupportedOptions() as $key) { |
| 142 | + $table->addRow([ |
| 143 | + $key, |
| 144 | + $this->isBooleanOption($key) ? 'bool' : 'string', |
| 145 | + $this->getCurrentValue($key), |
| 146 | + ]); |
| 147 | + } |
| 148 | + $table->render(); |
| 149 | + return self::SUCCESS; |
| 150 | + } |
| 151 | + |
| 152 | + private function getSupportedOptions(): array { |
| 153 | + return [ |
| 154 | + ConfigLexicon::OCM_INVITES_ENABLED, |
| 155 | + ...FederatedInvitesService::OCM_INVITES_BOOL_KEYS, |
| 156 | + ConfigLexicon::MESH_PROVIDERS_SERVICE, |
| 157 | + ]; |
| 158 | + } |
| 159 | + |
| 160 | + private function isBooleanOption(string $option): bool { |
| 161 | + return in_array($option, [ |
| 162 | + ConfigLexicon::OCM_INVITES_ENABLED, |
| 163 | + ...FederatedInvitesService::OCM_INVITES_BOOL_KEYS, |
| 164 | + ], true); |
| 165 | + } |
| 166 | + |
| 167 | + private function getCurrentValue(string $option): string { |
| 168 | + if ($this->isBooleanOption($option)) { |
| 169 | + return $this->formatBool($this->appConfig->getValueBool(Application::APP_ID, $option)); |
| 170 | + } |
| 171 | + |
| 172 | + return $this->appConfig->getValueString(Application::APP_ID, $option); |
| 173 | + } |
| 174 | + |
| 175 | + private function formatBool(bool $value): string { |
| 176 | + return $value ? 'on' : 'off'; |
| 177 | + } |
| 178 | + |
| 179 | + private function parseBool(string $raw): ?bool { |
| 180 | + $normalised = strtolower(trim($raw)); |
| 181 | + if (in_array($normalised, ['true', '1', 'on', 'yes'], true)) { |
| 182 | + return true; |
| 183 | + } |
| 184 | + if (in_array($normalised, ['false', '0', 'off', 'no'], true)) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + return null; |
| 188 | + } |
| 189 | +} |
0 commit comments