From 5e894f9f08045de57ced3b57cd6b4a6774c06447 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 14 Oct 2020 16:14:50 +0200 Subject: [PATCH] add admin actions: remove cache, export configuration --- appinfo/routes.php | 12 +-- lib/Controller/AdminSettingController.php | 92 +++++++++++++++++++++++ src/l10n/fixtures/de.yaml | 2 + src/l10n/fixtures/fr.yaml | 2 + templates/settings/admin-form.php | 26 ++++++- 5 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 lib/Controller/AdminSettingController.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 90bb2be..1efd4b6 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -19,10 +19,12 @@ return [ 'routes' => [ - ['name' => 'Css#stylesheet', 'url' => '/css/stylesheet', 'verb' => 'GET'], - ['name' => 'Js#script', 'url' => '/js/script', 'verb' => 'GET'], - ['name' => 'Js#config', 'url' => '/js/config', 'verb' => 'GET'], - ['name' => 'Nav#items', 'url' => '/nav/items', 'verb' => 'GET'], - ['name' => 'PersonalSetting#valueSet', 'url' => '/personalSetting/valueSet', 'verb' => 'POST'], + ['name' => 'Css#stylesheet', 'url' => '/css/stylesheet', 'verb' => 'GET'], + ['name' => 'Js#script', 'url' => '/js/script', 'verb' => 'GET'], + ['name' => 'Js#config', 'url' => '/js/config', 'verb' => 'GET'], + ['name' => 'Nav#items', 'url' => '/nav/items', 'verb' => 'GET'], + ['name' => 'PersonalSetting#valueSet', 'url' => '/personalSetting/valueSet', 'verb' => 'POST'], + ['name' => 'AdminSetting#removeCache', 'url' => '/admin/cache/remove', 'verb' => 'GET'], + ['name' => 'AdminSetting#exportConfiguration', 'url' => '/admin/config/export', 'verb' => 'GET'], ], ]; diff --git a/lib/Controller/AdminSettingController.php b/lib/Controller/AdminSettingController.php new file mode 100644 index 0000000..0eb9b57 --- /dev/null +++ b/lib/Controller/AdminSettingController.php @@ -0,0 +1,92 @@ +. + */ + +namespace OCA\SideMenu\Controller; + +use OCA\SideMenu\AppInfo\Application; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\DataDownloadResponse; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\Response; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IURLGenerator; + +class AdminSettingController extends Controller +{ + /** + * @var IConfig + */ + protected $config; + + /** + * @var IURLGenerator + */ + protected $urlGenerator; + + public function __construct($appName, IRequest $request, IConfig $config, IURLGenerator $urlGenerator) + { + parent::__construct($appName, $request); + + $this->config = $config; + $this->urlGenerator = $urlGenerator; + } + + /** + * @NoCSRFRequired + * + * @return RedirectResponse + */ + public function removeCache() + { + $this->config->setAppValue(Application::APP_ID, 'cache-categories', '[]'); + + return new RedirectResponse($this->urlGenerator->linkToRoute('settings.AdminSettings.index', [ + 'section' => Application::APP_ID, + ]).'#more'); + } + + /** + * @NoCSRFRequired + * + * @return Response + */ + public function exportConfiguration() + { + $keys = $this->config->getAppKeys(Application::APP_ID); + $config = []; + $excludedKeys = [ + 'cache', + 'cache-categories', + ]; + + foreach ($keys as $key) { + if (in_array($key, $excludedKeys)) { + continue; + } + + $config[$key] = $this->config->getAppValue(Application::APP_ID, $key); + } + + return new DataDownloadResponse( + json_encode($config, JSON_PRETTY_PRINT), + 'config.json', + 'text/json' + ); + } +} diff --git a/src/l10n/fixtures/de.yaml b/src/l10n/fixtures/de.yaml index 18fd5b4..b9d24ad 100644 --- a/src/l10n/fixtures/de.yaml +++ b/src/l10n/fixtures/de.yaml @@ -59,3 +59,5 @@ "Use the avatar instead of the logo": "Verwenden Sie den Avatar anstelle des Logos" "You do not have permission to change the settings.": "Sie haben keine Berechtigung zum Ändern der Einstellungen." "Force this configuration to users": "Erzwingen Sie diese Konfiguration für Benutzer" +"Export the configuration": "Exportieren Sie die Konfiguration" +"Remove the cache": "Entfernen Sie den Cache" diff --git a/src/l10n/fixtures/fr.yaml b/src/l10n/fixtures/fr.yaml index 0f44280..5cceb82 100644 --- a/src/l10n/fixtures/fr.yaml +++ b/src/l10n/fixtures/fr.yaml @@ -59,3 +59,5 @@ "Use the avatar instead of the logo": "Utiliser l'avatar à la place du logo" "You do not have permission to change the settings.": "Vous n'avez pas la permission de changer les paramètres." "Force this configuration to users": "Forcer cette configuration aux utilisateurs" +"Export the configuration": "Exporter la configuration" +"Remove the cache": "Supprimer le cache" diff --git a/templates/settings/admin-form.php b/templates/settings/admin-form.php index 47bee62..4442104 100644 --- a/templates/settings/admin-form.php +++ b/templates/settings/admin-form.php @@ -16,9 +16,16 @@ * along with this program. If not, see . */ +use OCP\IURLGenerator; +use OCP\IConfig; +use OCA\SideMenu\AppInfo\Application; + script('side_menu', 'admin'); style('side_menu', 'admin'); +$urlGenerator = \OC::$server[IURLGenerator::class]; +$cacheSize = floor(mb_strlen(\OC::$server[IConfig::class]->getAppValue(Application::APP_ID, 'cache-categories', ''), '8bit') / 1024); + $choicesYesNo = [ 'No' => '0', 'Yes' => '1', @@ -30,6 +37,7 @@ $choicesSizes = [ 'Normal' => 'normal', 'Big' => 'big', ]; + ?>
@@ -488,9 +496,25 @@ $choicesSizes = [ - + +
+ +