|
| 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\Federation\Command; |
| 11 | + |
| 12 | +use OCA\Federation\TrustedServers; |
| 13 | +use OCP\Console\Attribute\Argument; |
| 14 | +use OCP\Console\Attribute\AsCommand; |
| 15 | +use OCP\Console\ExitCode; |
| 16 | +use OCP\Console\IOutput; |
| 17 | + |
| 18 | +class ManageTrustedServers { |
| 19 | + private const STATUS_LABELS = [ |
| 20 | + TrustedServers::STATUS_OK => 'ok', |
| 21 | + TrustedServers::STATUS_PENDING => 'pending', |
| 22 | + TrustedServers::STATUS_FAILURE => 'failure', |
| 23 | + TrustedServers::STATUS_ACCESS_REVOKED => 'access revoked', |
| 24 | + ]; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private TrustedServers $trustedServersManager, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + #[AsCommand( |
| 32 | + name: 'federation:trusted-servers:add', |
| 33 | + description: 'Add trusted servers', |
| 34 | + )] |
| 35 | + public function add( |
| 36 | + IOutput $output, |
| 37 | + #[Argument(description: 'space-separated list of server URLs to add to the trusted servers list')] |
| 38 | + array $servers = [], |
| 39 | + ): ExitCode { |
| 40 | + if (empty($servers)) { |
| 41 | + $output->writeln('<error>You must provide at least one server URL.</error>'); |
| 42 | + return ExitCode::Invalid; |
| 43 | + } |
| 44 | + |
| 45 | + $output->writeln('Adding trusted servers:'); |
| 46 | + $hasFailure = false; |
| 47 | + foreach ($servers as $server) { |
| 48 | + if ($this->trustedServersManager->isTrustedServer($server)) { |
| 49 | + $output->writeln(" - $server already trusted, skipping"); |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + $this->trustedServersManager->addServer($server); |
| 55 | + $output->writeln(" - $server added"); |
| 56 | + } catch (\Exception $e) { |
| 57 | + $output->writeln(" - $server failed: " . $e->getMessage()); |
| 58 | + $hasFailure = true; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return $hasFailure ? ExitCode::Failure : ExitCode::Success; |
| 63 | + } |
| 64 | + |
| 65 | + #[AsCommand( |
| 66 | + name: 'federation:trusted-servers:remove', |
| 67 | + description: 'Remove trusted servers', |
| 68 | + )] |
| 69 | + public function remove( |
| 70 | + IOutput $output, |
| 71 | + #[Argument(description: 'space-separated list of server URLs to remove from the trusted servers list')] |
| 72 | + array $servers = [], |
| 73 | + ): ExitCode { |
| 74 | + if (empty($servers)) { |
| 75 | + $output->writeln('<error>You must provide at least one server URL.</error>'); |
| 76 | + return ExitCode::Invalid; |
| 77 | + } |
| 78 | + |
| 79 | + $output->writeln('Removing trusted servers:'); |
| 80 | + $hasFailure = false; |
| 81 | + foreach ($servers as $server) { |
| 82 | + $id = $this->findServerId($server); |
| 83 | + if ($id === null) { |
| 84 | + $output->writeln(" - $server not found, skipping"); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + $this->trustedServersManager->removeServer($id); |
| 90 | + $output->writeln(" - $server removed"); |
| 91 | + } catch (\Exception $e) { |
| 92 | + $output->writeln(" - $server failed: " . $e->getMessage()); |
| 93 | + $hasFailure = true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return $hasFailure ? ExitCode::Failure : ExitCode::Success; |
| 98 | + } |
| 99 | + |
| 100 | + #[AsCommand( |
| 101 | + name: 'federation:trusted-servers:list', |
| 102 | + description: 'List trusted servers', |
| 103 | + supportsOutputFormat: true, |
| 104 | + )] |
| 105 | + public function list( |
| 106 | + IOutput $output, |
| 107 | + ): ExitCode { |
| 108 | + $servers = $this->trustedServersManager->getServers(); |
| 109 | + |
| 110 | + if (empty($servers)) { |
| 111 | + $output->writeln('No trusted servers configured.'); |
| 112 | + return ExitCode::Success; |
| 113 | + } |
| 114 | + |
| 115 | + $output->writeTableInOutputFormat( |
| 116 | + array_map(fn (array $server) => [ |
| 117 | + 'id' => $server['id'], |
| 118 | + 'url' => $server['url'], |
| 119 | + 'status' => self::STATUS_LABELS[$server['status']] ?? (string)$server['status'], |
| 120 | + ], $servers), |
| 121 | + ); |
| 122 | + |
| 123 | + return ExitCode::Success; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Trusted servers are stored with a normalized (https:// prefixed, |
| 128 | + * trailing-slash trimmed) URL, so removal has to look up the id by |
| 129 | + * matching that same normalization rather than the raw user input. |
| 130 | + */ |
| 131 | + private function findServerId(string $url): ?int { |
| 132 | + $normalized = rtrim( |
| 133 | + (str_starts_with($url, 'http://') || str_starts_with($url, 'https://')) |
| 134 | + ? $url |
| 135 | + : 'https://' . $url, |
| 136 | + '/' |
| 137 | + ); |
| 138 | + |
| 139 | + foreach ($this->trustedServersManager->getServers() ?? [] as $server) { |
| 140 | + if (rtrim($server['url'], '/') === $normalized) { |
| 141 | + return $server['id']; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return null; |
| 146 | + } |
| 147 | +} |
0 commit comments