Skip to content

Commit

Permalink
fix LimitToGroups not working and hide app toolbar icon for excluded …
Browse files Browse the repository at this point in the history
…users - #517
  • Loading branch information
SergeyMosin committed Jun 11, 2024
1 parent d8e825a commit d8e38b7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\Appointments\AppInfo;

use OCA\Appointments\Backend\BeforeTemplateRenderedListener;
use OCA\Appointments\Backend\DavListener;
use OCA\Appointments\Backend\RemoveScriptsMiddleware;
use OCA\DAV\Events\CalendarObjectMovedToTrashEvent;
Expand All @@ -11,6 +12,7 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;

class Application extends App implements IBootstrap
{
Expand All @@ -32,6 +34,8 @@ public function register(IRegistrationContext $context): void
return new RemoveScriptsMiddleware();
});
$context->registerMiddleware('ApptRemoveScriptsMiddleware');

$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
}

public function boot(IBootContext $context): void
Expand Down
56 changes: 56 additions & 0 deletions lib/Backend/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace OCA\Appointments\Backend;

use OCA\Appointments\AppInfo\Application;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

class BeforeTemplateRenderedListener implements IEventListener
{
public function handle(Event $event): void
{
if (!($event instanceof BeforeTemplateRenderedEvent)) {
return;
}
if ($event->isLoggedIn() && $event->getResponse()->getRenderAs() === TemplateResponse::RENDER_AS_USER) {

try {
$config = \OC::$server->get(IConfig::class);
$allowedGroups = $config->getAppValue(Application::APP_ID,
BackendUtils::KEY_LIMIT_TO_GROUPS);

if (!empty($allowedGroups)) {
$aga = json_decode($allowedGroups, true);
if ($aga !== null) {
$user = \OC::$server->get(IUserSession::class)->getUser();
if (!empty($user)) {
$userGroups = \OC::$server->get(IGroupManager::class)->getUserGroups($user);
$disable = true;
foreach ($aga as $ag) {
if (array_key_exists($ag, $userGroups)) {
$disable = false;
break;
}
}
if ($disable) {
\OC_Util::addStyle(Application::APP_ID, 'hide-app');
}
}
}
}
} catch (\Throwable $e) {
\OC::$server->get(LoggerInterface::class)->error('error: cannot hide appointments app icon', [
'app' => Application::APP_ID,
'exception' => $e,
]);
}
}
}
}
9 changes: 5 additions & 4 deletions lib/Backend/RemoveScriptsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class RemoveScriptsMiddleware extends Middleware
{

/** @type bool */
private $removeNcScripts;
private bool $removeNcScripts;

public function afterController($controller, $methodName, Response $response) {
public function afterController($controller, $methodName, Response $response)
{

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

public function beforeOutput($controller, $methodName, $output) {
public function beforeOutput($controller, $methodName, $output)
{
if ($this->removeNcScripts === true) {
return preg_replace('/<script nonce="[^"]*?" defer src="(?:\/dist\/core-common|\/dist\/core-main|\/apps\/files_pdfviewer\/js\/files_pdfviewer-public)\.js[^<]*?<\/script>/', '', $output, 3);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class PageController extends Controller
private IUserSession $userSession;

public function __construct(IRequest $request,
$UserId,
IConfig $c,
IMailer $mailer,
IL10N $l,
Expand All @@ -53,7 +52,6 @@ public function __construct(IRequest $request,
LoggerInterface $logger
) {
parent::__construct(Application::APP_ID, $request);
$this->userId = $UserId;
$this->c = $c;
$this->mailer = $mailer;
$this->l = $l;
Expand All @@ -62,6 +60,7 @@ public function __construct(IRequest $request,
$this->bc = $backendManager->getConnector();
$this->utils = $utils;
$this->logger = $logger;
$this->userId = $this->userSession->getUser()?->getUID();
}

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

$disable = false;
if (empty($this->userId)) {
if (!empty($this->userId)) {
$allowedGroups = $this->c->getAppValue($this->appName,
BackendUtils::KEY_LIMIT_TO_GROUPS);
if ($allowedGroups !== '') {
$aga = json_decode($allowedGroups, true);
if ($aga !== null) {
$userGroups = \OC::$server->get(IGroupManager::class)->getUserIdGroups($this->userId);
$user = $this->userSession->getUser();
$userGroups = \OC::$server->get(IGroupManager::class)->getUserGroups($user);
$disable = true;
foreach ($aga as $ag) {
if (array_key_exists($ag, $userGroups)) {
Expand Down
6 changes: 6 additions & 0 deletions scss/hide-app.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.app-menu-main .app-menu-entry[data-app-id="appointments"],
.app-menu-main .app-menu-entry__active[data-app-id="appointments"],
.action.app-menu-popover-entry .action-link[href="/index.php/apps/appointments/"],
.action.app-menu-popover-entry .action-link[href="/apps/appointments/"] {
display: none !important;
}
1 change: 1 addition & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
cncf: path.join(__dirname, 'src', 'cncf.js'),
form_css: path.join(scssDir, 'form.scss'),
style_css: path.join(scssDir, 'style.scss'),
hide_app_css: path.join(scssDir, 'hide-app.scss'),
},
output: {
path: path.resolve(__dirname, './js'),
Expand Down

0 comments on commit d8e38b7

Please sign in to comment.