Skip to content

Commit d8e38b7

Browse files
committedJun 11, 2024
fix LimitToGroups not working and hide app toolbar icon for excluded users - #517
1 parent d8e825a commit d8e38b7

6 files changed

+76
-8
lines changed
 

‎lib/AppInfo/Application.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OCA\Appointments\AppInfo;
44

5+
use OCA\Appointments\Backend\BeforeTemplateRenderedListener;
56
use OCA\Appointments\Backend\DavListener;
67
use OCA\Appointments\Backend\RemoveScriptsMiddleware;
78
use OCA\DAV\Events\CalendarObjectMovedToTrashEvent;
@@ -11,6 +12,7 @@
1112
use OCP\AppFramework\Bootstrap\IBootContext;
1213
use OCP\AppFramework\Bootstrap\IBootstrap;
1314
use OCP\AppFramework\Bootstrap\IRegistrationContext;
15+
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
1416

1517
class Application extends App implements IBootstrap
1618
{
@@ -32,6 +34,8 @@ public function register(IRegistrationContext $context): void
3234
return new RemoveScriptsMiddleware();
3335
});
3436
$context->registerMiddleware('ApptRemoveScriptsMiddleware');
37+
38+
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
3539
}
3640

3741
public function boot(IBootContext $context): void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace OCA\Appointments\Backend;
4+
5+
use OCA\Appointments\AppInfo\Application;
6+
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
7+
use OCP\AppFramework\Http\TemplateResponse;
8+
use OCP\EventDispatcher\Event;
9+
use OCP\EventDispatcher\IEventListener;
10+
use OCP\IConfig;
11+
use OCP\IGroupManager;
12+
use OCP\IUserSession;
13+
use Psr\Log\LoggerInterface;
14+
15+
class BeforeTemplateRenderedListener implements IEventListener
16+
{
17+
public function handle(Event $event): void
18+
{
19+
if (!($event instanceof BeforeTemplateRenderedEvent)) {
20+
return;
21+
}
22+
if ($event->isLoggedIn() && $event->getResponse()->getRenderAs() === TemplateResponse::RENDER_AS_USER) {
23+
24+
try {
25+
$config = \OC::$server->get(IConfig::class);
26+
$allowedGroups = $config->getAppValue(Application::APP_ID,
27+
BackendUtils::KEY_LIMIT_TO_GROUPS);
28+
29+
if (!empty($allowedGroups)) {
30+
$aga = json_decode($allowedGroups, true);
31+
if ($aga !== null) {
32+
$user = \OC::$server->get(IUserSession::class)->getUser();
33+
if (!empty($user)) {
34+
$userGroups = \OC::$server->get(IGroupManager::class)->getUserGroups($user);
35+
$disable = true;
36+
foreach ($aga as $ag) {
37+
if (array_key_exists($ag, $userGroups)) {
38+
$disable = false;
39+
break;
40+
}
41+
}
42+
if ($disable) {
43+
\OC_Util::addStyle(Application::APP_ID, 'hide-app');
44+
}
45+
}
46+
}
47+
}
48+
} catch (\Throwable $e) {
49+
\OC::$server->get(LoggerInterface::class)->error('error: cannot hide appointments app icon', [
50+
'app' => Application::APP_ID,
51+
'exception' => $e,
52+
]);
53+
}
54+
}
55+
}
56+
}

‎lib/Backend/RemoveScriptsMiddleware.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
class RemoveScriptsMiddleware extends Middleware
99
{
1010

11-
/** @type bool */
12-
private $removeNcScripts;
11+
private bool $removeNcScripts;
1312

14-
public function afterController($controller, $methodName, Response $response) {
13+
public function afterController($controller, $methodName, Response $response)
14+
{
1515

1616
if (isset($response->getHeaders()['X-Appointments'])) {
1717
$this->removeNcScripts = true;
@@ -22,7 +22,8 @@ public function afterController($controller, $methodName, Response $response) {
2222
return $response;
2323
}
2424

25-
public function beforeOutput($controller, $methodName, $output) {
25+
public function beforeOutput($controller, $methodName, $output)
26+
{
2627
if ($this->removeNcScripts === true) {
2728
return preg_replace('/<script nonce="[^"]*?" defer src="(?:\/dist\/core-common|\/dist\/core-main|\/apps\/files_pdfviewer\/js\/files_pdfviewer-public)\.js[^<]*?<\/script>/', '', $output, 3);
2829
}

‎lib/Controller/PageController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class PageController extends Controller
4343
private IUserSession $userSession;
4444

4545
public function __construct(IRequest $request,
46-
$UserId,
4746
IConfig $c,
4847
IMailer $mailer,
4948
IL10N $l,
@@ -53,7 +52,6 @@ public function __construct(IRequest $request,
5352
LoggerInterface $logger
5453
) {
5554
parent::__construct(Application::APP_ID, $request);
56-
$this->userId = $UserId;
5755
$this->c = $c;
5856
$this->mailer = $mailer;
5957
$this->l = $l;
@@ -62,6 +60,7 @@ public function __construct(IRequest $request,
6260
$this->bc = $backendManager->getConnector();
6361
$this->utils = $utils;
6462
$this->logger = $logger;
63+
$this->userId = $this->userSession->getUser()?->getUID();
6564
}
6665

6766
/**
@@ -79,13 +78,14 @@ public function index(): TemplateResponse
7978
$t = new TemplateResponse($this->appName, 'index');
8079

8180
$disable = false;
82-
if (empty($this->userId)) {
81+
if (!empty($this->userId)) {
8382
$allowedGroups = $this->c->getAppValue($this->appName,
8483
BackendUtils::KEY_LIMIT_TO_GROUPS);
8584
if ($allowedGroups !== '') {
8685
$aga = json_decode($allowedGroups, true);
8786
if ($aga !== null) {
88-
$userGroups = \OC::$server->get(IGroupManager::class)->getUserIdGroups($this->userId);
87+
$user = $this->userSession->getUser();
88+
$userGroups = \OC::$server->get(IGroupManager::class)->getUserGroups($user);
8989
$disable = true;
9090
foreach ($aga as $ag) {
9191
if (array_key_exists($ag, $userGroups)) {

‎scss/hide-app.scss

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.app-menu-main .app-menu-entry[data-app-id="appointments"],
2+
.app-menu-main .app-menu-entry__active[data-app-id="appointments"],
3+
.action.app-menu-popover-entry .action-link[href="/index.php/apps/appointments/"],
4+
.action.app-menu-popover-entry .action-link[href="/apps/appointments/"] {
5+
display: none !important;
6+
}

‎webpack.common.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
cncf: path.join(__dirname, 'src', 'cncf.js'),
1212
form_css: path.join(scssDir, 'form.scss'),
1313
style_css: path.join(scssDir, 'style.scss'),
14+
hide_app_css: path.join(scssDir, 'hide-app.scss'),
1415
},
1516
output: {
1617
path: path.resolve(__dirname, './js'),

0 commit comments

Comments
 (0)
Please sign in to comment.