Browse Source

Merge pull request #8239 from nextcloud/followup/8233/disable-dashboard-widget-with-api

Disable the dashboard widget with proper API
pull/8265/head
Joas Schilling 3 years ago
committed by GitHub
parent
commit
1292604ab5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      lib/Dashboard/TalkWidget.php
  2. 37
      lib/Exceptions/NotAllowedToUseTalkException.php

23
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);
}

37
lib/Exceptions/NotAllowedToUseTalkException.php

@ -1,37 +0,0 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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 <http://www.gnu.org/licenses/>
*
*/
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);
}
}
Loading…
Cancel
Save