From 9006e988c46b65527765f1653177edb2f934375b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 28 Oct 2022 09:22:56 +0200 Subject: [PATCH] Disable the dashboard widget with proper API Signed-off-by: Joas Schilling --- lib/Dashboard/TalkWidget.php | 23 +++++++----- .../NotAllowedToUseTalkException.php | 37 ------------------- 2 files changed, 14 insertions(+), 46 deletions(-) delete mode 100644 lib/Exceptions/NotAllowedToUseTalkException.php diff --git a/lib/Dashboard/TalkWidget.php b/lib/Dashboard/TalkWidget.php index 951329195d..3ca280fc98 100644 --- a/lib/Dashboard/TalkWidget.php +++ b/lib/Dashboard/TalkWidget.php @@ -27,7 +27,6 @@ namespace OCA\Talk\Dashboard; use OCA\Talk\Chat\MessageParser; use OCA\Talk\Config; -use OCA\Talk\Exceptions\NotAllowedToUseTalkException; use OCA\Talk\Manager; use OCA\Talk\Participant; use OCA\Talk\Room; @@ -35,6 +34,7 @@ use OCA\Talk\Service\ParticipantService; use OCP\Comments\IComment; use OCP\Dashboard\IAPIWidget; use OCP\Dashboard\IButtonWidget; +use OCP\Dashboard\IConditionalWidget; use OCP\Dashboard\IIconWidget; use OCP\Dashboard\IOptionWidget; use OCP\Dashboard\Model\WidgetButton; @@ -46,7 +46,9 @@ use OCP\IUser; use OCP\IUserSession; use OCP\Util; -class TalkWidget implements IAPIWidget, IIconWidget, IButtonWidget, IOptionWidget { +class TalkWidget implements IAPIWidget, IIconWidget, IButtonWidget, IOptionWidget, IConditionalWidget { + protected IUserSession $userSession; + protected Config $talkConfig; protected IURLGenerator $url; protected IL10N $l10n; protected Manager $manager; @@ -62,13 +64,8 @@ class TalkWidget implements IAPIWidget, IIconWidget, IButtonWidget, IOptionWidge ParticipantService $participantService, MessageParser $messageParser ) { - $user = $userSession->getUser(); - if ($user instanceof IUser && $talkConfig->isDisabledForUser($user)) { - // This is dirty and will log everytime a user opens the dashboard or requests the api, - // so we should look for a different solution in the server. - throw new NotAllowedToUseTalkException(); - } - + $this->userSession = $userSession; + $this->talkConfig = $talkConfig; $this->url = $url; $this->l10n = $l10n; $this->manager = $manager; @@ -104,6 +101,14 @@ class TalkWidget implements IAPIWidget, IIconWidget, IButtonWidget, IOptionWidge return 'dashboard-talk-icon'; } + /** + * @inheritDoc + */ + public function isEnabled(): bool { + $user = $this->userSession->getUser(); + return !($user instanceof IUser && $this->talkConfig->isDisabledForUser($user)); + } + public function getWidgetOptions(): WidgetOptions { return new WidgetOptions(true); } diff --git a/lib/Exceptions/NotAllowedToUseTalkException.php b/lib/Exceptions/NotAllowedToUseTalkException.php deleted file mode 100644 index 57af9f8e7c..0000000000 --- a/lib/Exceptions/NotAllowedToUseTalkException.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * @author Joas Schilling - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ -namespace OCA\Talk\Exceptions; - -use OCP\AppFramework\QueryException; -use Psr\Container\ContainerExceptionInterface; - -/** - * An exception thrown to bail out of registering Talk modules such as the dashboard widget - */ -class NotAllowedToUseTalkException extends QueryException implements ContainerExceptionInterface { - public function __construct(int $code = 0, \Throwable $parent = null) { - parent::__construct('Can not use talk', $code, $parent); - } -}