add admin section to select groups allowed to print
This commit is contained in:
parent
f854accbcc
commit
497a9f4892
|
@ -28,6 +28,10 @@
|
||||||
<bugs>https://github.com/e-alfred/nextcloud-printer/issues</bugs>
|
<bugs>https://github.com/e-alfred/nextcloud-printer/issues</bugs>
|
||||||
<screenshot>https://github.com/e-alfred/nextcloud-printer/raw/master/screenshots/printer.gif</screenshot>
|
<screenshot>https://github.com/e-alfred/nextcloud-printer/raw/master/screenshots/printer.gif</screenshot>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<nextcloud min-version="16" max-version="19" />
|
<nextcloud min-version="17" max-version="19" />
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
<settings>
|
||||||
|
<admin>OCA\Printer\Settings\Admin\AllowedGroups</admin>
|
||||||
|
<admin-section>OCA\Printer\Settings\Admin\Section</admin-section>
|
||||||
|
</settings>
|
||||||
</info>
|
</info>
|
||||||
|
|
3
css/admin.css
Normal file
3
css/admin.css
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#printer_admin input {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
65
js/admin.js
Normal file
65
js/admin.js
Normal file
|
@ -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)
|
||||||
|
});
|
||||||
|
});
|
66
lib/Config.php
Normal file
66
lib/Config.php
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\Printer;
|
||||||
|
|
||||||
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
|
use OCP\IConfig;
|
||||||
|
use OCP\IGroupManager;
|
||||||
|
use OCP\IUser;
|
||||||
|
use OCP\Security\ISecureRandom;
|
||||||
|
|
||||||
|
class Config
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IConfig
|
||||||
|
*/
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ITimeFactory
|
||||||
|
*/
|
||||||
|
protected $timeFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IGroupManager
|
||||||
|
*/
|
||||||
|
private $groupManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ISecureRandom
|
||||||
|
*/
|
||||||
|
private $secureRandom;
|
||||||
|
|
||||||
|
public function __construct(IConfig $config, ISecureRandom $secureRandom, IGroupManager $groupManager, ITimeFactory $timeFactory)
|
||||||
|
{
|
||||||
|
$this->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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,11 +2,12 @@
|
||||||
|
|
||||||
namespace OCA\Printer\Controller;
|
namespace OCA\Printer\Controller;
|
||||||
|
|
||||||
|
use OCA\Printer\Config;
|
||||||
|
use OCA\Printer\Service\Printer;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
use Symfony\Component\Process\Process;
|
use OCP\IUserSession;
|
||||||
use OCA\Printer\Service\Printer;
|
|
||||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||||
|
|
||||||
class PrinterController extends Controller
|
class PrinterController extends Controller
|
||||||
|
@ -21,12 +22,18 @@ class PrinterController extends Controller
|
||||||
*/
|
*/
|
||||||
protected $printer;
|
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);
|
parent::__construct($appName, $request);
|
||||||
|
|
||||||
$this->language = \OC::$server->getL10N('printer');
|
$this->language = \OC::$server->getL10N('printer');
|
||||||
$this->printer = $printer;
|
$this->printer = $printer;
|
||||||
|
$this->config = $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,6 +53,17 @@ class PrinterController extends Controller
|
||||||
'msg' => $this->language->t('Print failed'),
|
'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)) {
|
if (!$this->printer->isValidOrirentation($orientation)) {
|
||||||
return new JSONResponse($error);
|
return new JSONResponse($error);
|
||||||
}
|
}
|
||||||
|
|
63
lib/Settings/Admin/AllowedGroups.php
Normal file
63
lib/Settings/Admin/AllowedGroups.php
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\Printer\Settings\Admin;
|
||||||
|
|
||||||
|
use OCA\Printer\Config;
|
||||||
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
|
use OCP\IGroupManager;
|
||||||
|
use OCP\IInitialStateService;
|
||||||
|
use OCP\Settings\ISettings;
|
||||||
|
|
||||||
|
class AllowedGroups implements ISettings
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Config
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IInitialStateService
|
||||||
|
*/
|
||||||
|
private $initialStateService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IGroupManager
|
||||||
|
*/
|
||||||
|
private $groupManager;
|
||||||
|
|
||||||
|
public function __construct(Config $config, IInitialStateService $initialStateService, IGroupManager $groupManager)
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
48
lib/Settings/Admin/Section.php
Normal file
48
lib/Settings/Admin/Section.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\Printer\Settings\Admin;
|
||||||
|
|
||||||
|
use OCP\IL10N;
|
||||||
|
use OCP\IURLGenerator;
|
||||||
|
use OCP\Settings\IIconSection;
|
||||||
|
|
||||||
|
class Section implements IIconSection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var IL10N
|
||||||
|
*/
|
||||||
|
private $l;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IURLGenerator
|
||||||
|
*/
|
||||||
|
private $url;
|
||||||
|
|
||||||
|
public function __construct(IURLGenerator $url, IL10N $l)
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
38
templates/settings/admin/allowed-groups.php
Normal file
38
templates/settings/admin/allowed-groups.php
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
style('printer', 'admin');
|
||||||
|
script('printer', 'admin');
|
||||||
|
?>
|
||||||
|
<div id="printer_admin">
|
||||||
|
<div class="section">
|
||||||
|
<h2><?php p($l->t('Limit to groups')) ?></h2>
|
||||||
|
<p class="settings-hint">
|
||||||
|
<?php p($l->t('When at least one group is selected, only people of the listed groups can print.')); ?>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<ul class="printer-setting" data-name="allowed_groups" data-checkbox>
|
||||||
|
<?php foreach ($_['groups'] as $group): ?>
|
||||||
|
<li>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
id="allowed_group_<?php echo $group->getGid() ?>"
|
||||||
|
name="allowed_groups[]"
|
||||||
|
value="<?php p($group->getGid()) ?>"
|
||||||
|
<?php if (in_array($group->getGid(), $_['allowedGroups'])): ?>
|
||||||
|
checked
|
||||||
|
<?php endif ?>
|
||||||
|
>
|
||||||
|
|
||||||
|
<label for="allowed_group_<?php echo $group->getGid() ?>">
|
||||||
|
<?php p($group->getDisplayName()) ?>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<button id="printer-save" class="btn btn-info">
|
||||||
|
<?php p($l->t('Save')); ?>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<span id="printer-message" class="msg"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
Loading…
Reference in a new issue