diff --git a/appinfo/routes.php b/appinfo/routes.php index fb346ddff..9ad74e1f8 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -79,7 +79,10 @@ ['name' => 'Admin#editDescription', 'url' => '/admin/{emulated}/circles/{circleId}/description', 'verb' => 'PUT'], ['name' => 'Admin#editSetting', 'url' => '/admin/{emulated}/circles/{circleId}/setting', 'verb' => 'PUT'], ['name' => 'Admin#editConfig', 'url' => '/admin/{emulated}/circles/{circleId}/config', 'verb' => 'PUT'], - ['name' => 'Admin#link', 'url' => '/admin/{emulated}/link/{circleId}/{singleId}', 'verb' => 'GET'] + ['name' => 'Admin#link', 'url' => '/admin/{emulated}/link/{circleId}/{singleId}', 'verb' => 'GET'], + + ['name' => 'Settings#getValues', 'url' => '/settings/', 'verb' => 'GET'], + ['name' => 'Settings#setValue', 'url' => '/settings/{key}/', 'verb' => 'POST'], ], 'routes' => [ diff --git a/lib/ConfigLexicon.php b/lib/ConfigLexicon.php index 797a7bffb..af0d139c1 100644 --- a/lib/ConfigLexicon.php +++ b/lib/ConfigLexicon.php @@ -21,6 +21,8 @@ */ class ConfigLexicon implements ILexicon { public const USER_SINGLE_ID = 'userSingleId'; + public const FEDERATED_TEAMS_ENABLED = 'federated_teams_enabled'; + public const FEDERATED_TEAMS_FRONTAL = 'federated_teams_frontal'; public function getStrictness(): Strictness { return Strictness::IGNORE; @@ -28,6 +30,8 @@ public function getStrictness(): Strictness { public function getAppConfigs(): array { return [ + new Entry(key: self::FEDERATED_TEAMS_ENABLED, type: ValueType::BOOL, defaultRaw: false, definition: 'disable/enable Federated Teams', lazy: true), + new Entry(key: self::FEDERATED_TEAMS_FRONTAL, type: ValueType::STRING, defaultRaw: '', definition: 'domain name used to auth public request', lazy: true), ]; } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php new file mode 100644 index 000000000..6bc8bc0b5 --- /dev/null +++ b/lib/Controller/SettingsController.php @@ -0,0 +1,103 @@ +setFrontalValue($value)) { + return $this->getValues(); + } + + return new DataResponse(['data' => ['message' => 'wrongly formated value']], Http::STATUS_BAD_REQUEST); + } + + if ($key === ConfigLexicon::FEDERATED_TEAMS_ENABLED) { + $this->appConfig->setAppValueBool(ConfigLexicon::FEDERATED_TEAMS_ENABLED, $value === 'yes'); + return $this->getValues(); + } + + return new DataResponse(['data' => ['message' => 'unsupported key']], Http::STATUS_BAD_REQUEST); + } + + public function getValues(): DataResponse { + return new DataResponse([ + ConfigLexicon::FEDERATED_TEAMS_FRONTAL => $this->getFrontalValue() ?? '', + ConfigLexicon::FEDERATED_TEAMS_ENABLED => $this->appConfig->getAppValueBool(ConfigLexicon::FEDERATED_TEAMS_ENABLED), + ]); + } + + private function setFrontalValue(string $url): bool { + [$scheme, $cloudId, $path] = $this->parseFrontalAddress($url); + if (is_null($scheme)) { + return false; + } + + $this->appConfig->setAppValueString(ConfigLexicon::FEDERATED_TEAMS_FRONTAL, $url); + $this->appConfig->setAppValueString(ConfigService::FRONTAL_CLOUD_SCHEME, $scheme); + $this->appConfig->setAppValueString(ConfigService::FRONTAL_CLOUD_ID, $cloudId); + $this->appConfig->setAppValueString(ConfigService::FRONTAL_CLOUD_PATH, $path); + + return true; + } + + private function getFrontalValue(): ?string { + if ($this->appConfig->hasAppKey(ConfigLexicon::FEDERATED_TEAMS_FRONTAL)) { + return $this->appConfig->getAppValueString(ConfigLExicon::FEDERATED_TEAMS_FRONTAL); + } + + if (!$this->appConfig->hasAppKey(ConfigService::FRONTAL_CLOUD_SCHEME) + || !$this->appConfig->hasAppKey(ConfigService::FRONTAL_CLOUD_ID) + || !$this->appConfig->hasAppKey(ConfigService::FRONTAL_CLOUD_PATH)) { + return null; + } + + return $this->appConfig->getAppValueString(ConfigService::FRONTAL_CLOUD_SCHEME) . '://' . + $this->appConfig->getAppValueString(ConfigService::FRONTAL_CLOUD_ID) . + $this->appConfig->getAppValueString(ConfigService::FRONTAL_CLOUD_PATH) . '/'; + } + + private function parseFrontalAddress(string $url): ?array { + $scheme = parse_url($url, PHP_URL_SCHEME); + $cloudId = parse_url($url, PHP_URL_HOST); + $cloudIdPort = parse_url($url, PHP_URL_PORT); + $path = parse_url($url, PHP_URL_PATH); + + if (is_bool($scheme) || is_bool($cloudId) || is_null($scheme) || is_null($cloudId)) { + return null; + } + + if (is_null($path) || is_bool($path)) { + $path = ''; + } + $path = rtrim($path, '/'); + if (!is_null($cloudIdPort)) { + $cloudId .= ':' . ((string)$cloudIdPort); + } + + return [$scheme, $cloudId, $path]; + } +}