From 7d1fd1b49a8dc60715abd91c833ba41b6307fa16 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 13 Jul 2020 11:31:27 +0200 Subject: [PATCH] Simplify the settings module Signed-off-by: Joas Schilling --- lib/Controller/SettingsController.php | 43 ++------------ lib/Settings/RegistrationSettings.php | 83 +++++++++++++++++++++------ 2 files changed, 71 insertions(+), 55 deletions(-) diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index a487cf5..4c809e3 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -13,7 +13,6 @@ namespace OCA\Registration\Controller; use \OCP\IRequest; -use \OCP\AppFramework\Http\TemplateResponse; use \OCP\AppFramework\Http\DataResponse; use \OCP\AppFramework\Http; use \OCP\AppFramework\Controller; @@ -40,8 +39,6 @@ class SettingsController extends Controller { $this->appName = $appName; } - - /** * @AdminRequired * @@ -71,53 +68,25 @@ class SettingsController extends Controller { $this->config->deleteAppValue($this->appName, 'registered_user_group'); return new DataResponse([ 'data' => [ - 'message' => (string) $this->l10n->t('Saved'), + 'message' => $this->l10n->t('Saved'), ], - 'status' => 'success' - + 'status' => 'success', ]); } elseif (in_array($registered_user_group, $group_id_list)) { $this->config->setAppValue($this->appName, 'registered_user_group', $registered_user_group); return new DataResponse([ 'data' => [ - 'message' => (string) $this->l10n->t('Saved'), + 'message' => $this->l10n->t('Saved'), ], - 'status' => 'success' + 'status' => 'success', ]); } else { return new DataResponse([ 'data' => [ - 'message' => (string) $this->l10n->t('No such group'), + 'message' => $this->l10n->t('No such group'), ], - 'status' => 'error' + 'status' => 'error', ], Http::STATUS_NOT_FOUND); } } - /** - * @AdminRequired - * - * @return TemplateResponse - */ - public function displayPanel() { - // handle groups - $groups = $this->groupmanager->search(''); - $group_id_list = []; - foreach ($groups as $group) { - $group_id_list[] = $group->getGid(); - } - $current_value = $this->config->getAppValue($this->appName, 'registered_user_group', 'none'); - - // handle domains - $allowed_domains = $this->config->getAppValue($this->appName, 'allowed_domains', ''); - - // handle admin validation - $admin_approval_required = $this->config->getAppValue($this->appName, 'admin_approval_required', "no"); - - return new TemplateResponse('registration', 'admin', [ - 'groups' => $group_id_list, - 'current' => $current_value, - 'allowed' => $allowed_domains, - 'approval_required' => $admin_approval_required - ], ''); - } } diff --git a/lib/Settings/RegistrationSettings.php b/lib/Settings/RegistrationSettings.php index bbb36fb..0f9ed51 100644 --- a/lib/Settings/RegistrationSettings.php +++ b/lib/Settings/RegistrationSettings.php @@ -1,33 +1,80 @@ + * + * @author Pellaeon Lin + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + namespace OCA\Registration\Settings; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IGroupManager; use OCP\Settings\ISettings; -use OCA\Registration\Controller\SettingsController; - class RegistrationSettings implements ISettings { - public function getForm() { - $controller = \OC::$server->query(SettingsController::class); - return $controller->displayPanel(); + /** @var IConfig */ + private $config; + /** @var IGroupManager */ + private $groupManager; + /** @var string */ + protected $appName; + + public function __construct(string $appName, + IConfig $config, + IGroupManager $groupManager) { + $this->appName = $appName; + $this->config = $config; + $this->groupManager = $groupManager; + $this->appName = $appName; } - public function getSection() { + public function getForm(): TemplateResponse { + // handle groups + $groups = $this->groupManager->search(''); + $groupIds = []; + foreach ($groups as $group) { + $groupIds[] = $group->getGid(); + } + $assignedGroups = $this->config->getAppValue($this->appName, 'registered_user_group', 'none'); + + // handle domains + $allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', ''); + + // handle admin validation + $adminApprovalRequired = $this->config->getAppValue($this->appName, 'admin_approval_required', "no"); + + return new TemplateResponse('registration', 'admin', [ + 'groups' => $groupIds, + 'current' => $assignedGroups, + 'allowed' => $allowedDomains, + 'approval_required' => $adminApprovalRequired + ], ''); + } + + public function getSection(): string { return 'additional'; } - public function getPriority() { + public function getPriority(): int { return 50; } - - /* - * Below for ownCloud - */ - public function getPanel() { - return $this->getForm(); - } - - public function getSectionID() { - return $this->getSection(); - } }