From 497a9f48928a0998cfb861aab5afed48a668d64b Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 20 Nov 2020 15:45:11 +0100 Subject: [PATCH] add admin section to select groups allowed to print --- appinfo/info.xml | 6 +- css/admin.css | 3 + js/admin.js | 65 ++++++++++++++++++++ lib/Config.php | 66 +++++++++++++++++++++ lib/Controller/PrinterController.php | 24 +++++++- lib/Settings/Admin/AllowedGroups.php | 63 ++++++++++++++++++++ lib/Settings/Admin/Section.php | 48 +++++++++++++++ templates/settings/admin/allowed-groups.php | 38 ++++++++++++ 8 files changed, 309 insertions(+), 4 deletions(-) create mode 100644 css/admin.css create mode 100644 js/admin.js create mode 100644 lib/Config.php create mode 100644 lib/Settings/Admin/AllowedGroups.php create mode 100644 lib/Settings/Admin/Section.php create mode 100644 templates/settings/admin/allowed-groups.php diff --git a/appinfo/info.xml b/appinfo/info.xml index c576f0e..95a29f3 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -28,6 +28,10 @@ https://github.com/e-alfred/nextcloud-printer/issues https://github.com/e-alfred/nextcloud-printer/raw/master/screenshots/printer.gif - + + + OCA\Printer\Settings\Admin\AllowedGroups + OCA\Printer\Settings\Admin\Section + diff --git a/css/admin.css b/css/admin.css new file mode 100644 index 0000000..d77db4d --- /dev/null +++ b/css/admin.css @@ -0,0 +1,3 @@ +#printer_admin input { + vertical-align: middle; +} diff --git a/js/admin.js b/js/admin.js new file mode 100644 index 0000000..901d7de --- /dev/null +++ b/js/admin.js @@ -0,0 +1,65 @@ +let elements = [] + +const selector = '#printer-message'; + +const appConfig = (name, value, callbacks) => { + OCP.AppConfig.setValue('printer', name, value, callbacks) +} + +const saveSettings = (key) => { + const element = elements.get(key) + let value + let name + + if (jQuery(element).is('[data-checkbox]')) { + name = jQuery(element).attr('data-name') + const inputs = jQuery('input[name="' + name + '[]"]:checked') + value = [] + + inputs.each((i, v) => { + value.push(v.value) + }) + + value = JSON.stringify(value) + } else { + name = jQuery(element).attr('name') + value = jQuery(element).val() + } + + const size = elements.length + + if (name === 'cache') { + ++value + } + + const callbacks = { + success: () => { + OC.msg.finishedSuccess( + selector, + t('printer', (key + 1) + '/' + size) + ) + + if (key < size - 1) { + saveSettings(++key) + } else { + OC.msg.finishedSuccess(selector, t('printer', 'Saved')) + } + }, + error: () => { + OC.msg.finishedError(selector, t('printer', 'Error while saving "' + element + '"')) + } + } + + appConfig(name, value, callbacks) +} + +jQuery(document).ready(() => { + elements = jQuery('.printer-setting') + + jQuery('#printer-save').on('click', (event) => { + event.preventDefault() + OC.msg.startSaving(selector) + + saveSettings(0) + }); +}); diff --git a/lib/Config.php b/lib/Config.php new file mode 100644 index 0000000..d103c92 --- /dev/null +++ b/lib/Config.php @@ -0,0 +1,66 @@ +config = $config; + $this->secureRandom = $secureRandom; + $this->groupManager = $groupManager; + $this->timeFactory = $timeFactory; + } + + /** + * @return string[] + */ + public function getAllowedGroupIds(): array + { + $groups = $this->config->getAppValue('printer', 'allowed_groups', '[]'); + $groups = json_decode($groups, true); + + return \is_array($groups) ? $groups : []; + } + + public function isDisabledForUser(IUser $user): bool + { + $allowedGroups = $this->getAllowedGroupIds(); + + if (empty($allowedGroups)) { + return false; + } + + $userGroups = $this->groupManager->getUserGroupIds($user); + + return empty(array_intersect($allowedGroups, $userGroups)); + } +} diff --git a/lib/Controller/PrinterController.php b/lib/Controller/PrinterController.php index 4bfd574..f827d27 100644 --- a/lib/Controller/PrinterController.php +++ b/lib/Controller/PrinterController.php @@ -2,11 +2,12 @@ namespace OCA\Printer\Controller; +use OCA\Printer\Config; +use OCA\Printer\Service\Printer; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; -use Symfony\Component\Process\Process; -use OCA\Printer\Service\Printer; +use OCP\IUserSession; use Symfony\Component\Process\Exception\ProcessFailedException; class PrinterController extends Controller @@ -21,12 +22,18 @@ class PrinterController extends Controller */ protected $printer; - public function __construct(string $appName, IRequest $request, Printer $printer) + /** + * @var Config + */ + protected $config; + + public function __construct(string $appName, IRequest $request, Printer $printer, Config $config) { parent::__construct($appName, $request); $this->language = \OC::$server->getL10N('printer'); $this->printer = $printer; + $this->config = $config; } /** @@ -46,6 +53,17 @@ class PrinterController extends Controller 'msg' => $this->language->t('Print failed'), ]; + $notAllowed = [ + 'response' => 'error', + 'msg' => $this->language->t('User not allowed'), + ]; + + $user = \OC::$server[IUserSession::class]->getUser(); + + if (!$user || $this->config->isDisabledForUser($user)) { + return new JSONResponse($notAllowed); + } + if (!$this->printer->isValidOrirentation($orientation)) { return new JSONResponse($error); } diff --git a/lib/Settings/Admin/AllowedGroups.php b/lib/Settings/Admin/AllowedGroups.php new file mode 100644 index 0000000..406f979 --- /dev/null +++ b/lib/Settings/Admin/AllowedGroups.php @@ -0,0 +1,63 @@ +config = $config; + $this->initialStateService = $initialStateService; + $this->groupManager = $groupManager; + } + + public function getForm(): TemplateResponse + { + $this->initialStateService->provideInitialState( + 'printer', + 'allowed_groups', + $this->config->getAllowedGroupIds() + ); + + $groups = $this->groupManager->search('', 100); + $allowedGroups = $this->config->getAllowedGroupIds(); + + return new TemplateResponse('printer', 'settings/admin/allowed-groups', [ + 'groups' => $groups, + 'allowedGroups' => $allowedGroups, + ], ''); + } + + public function getSection(): string + { + return 'printer'; + } + + public function getPriority(): int + { + return 10; + } +} diff --git a/lib/Settings/Admin/Section.php b/lib/Settings/Admin/Section.php new file mode 100644 index 0000000..ed5cb07 --- /dev/null +++ b/lib/Settings/Admin/Section.php @@ -0,0 +1,48 @@ +url = $url; + $this->l = $l; + } + + public function getIcon(): string + { + return $this->url->imagePath('spreed', 'app-dark.svg'); + } + + public function getID(): string + { + return 'printer'; + } + + public function getName(): string + { + return $this->l->t('Printer'); + } + + public function getPriority(): int + { + return 70; + } +} diff --git a/templates/settings/admin/allowed-groups.php b/templates/settings/admin/allowed-groups.php new file mode 100644 index 0000000..2553000 --- /dev/null +++ b/templates/settings/admin/allowed-groups.php @@ -0,0 +1,38 @@ + +
+
+

t('Limit to groups')) ?>

+

+ t('When at least one group is selected, only people of the listed groups can print.')); ?> +

+ +
    + +
  • + getGid(), $_['allowedGroups'])): ?> + checked + + > + + +
  • + +
+ + + + +
+