Skip to content

Commit

Permalink
load widgets only of enabled apps
Browse files Browse the repository at this point in the history
- per design, all enabled apps have their registration run
- limitations, e.g. enabled by group, are not considered in that state,
  because we do not have a session (and might need apps?)
- before instantiation of widget it has to be checked whether the providing
  app is actually enabled for the logged in user.
- a public interface is being changed, but it is not meant to be
  implemented or used outside of the core handling. Therefore save to
  backport.

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Jun 22, 2022
1 parent b282fe1 commit 5c5c253
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/private/AppFramework/Bootstrap/Coordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private function registerApps(array $appIds): void {
*/
$this->registrationContext->delegateCapabilityRegistrations($apps);
$this->registrationContext->delegateCrashReporterRegistrations($apps, $this->registry);
$this->registrationContext->delegateDashboardPanelRegistrations($apps, $this->dashboardManager);
$this->registrationContext->delegateDashboardPanelRegistrations($this->dashboardManager);
$this->registrationContext->delegateEventListenerRegistrations($this->eventDispatcher);
$this->registrationContext->delegateContainerRegistrations($apps);
$this->registrationContext->delegateMiddlewareRegistrations($apps);
Expand Down
4 changes: 2 additions & 2 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,10 @@ public function delegateCrashReporterRegistrations(array $apps, Registry $regist
/**
* @param App[] $apps
*/
public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void {
public function delegateDashboardPanelRegistrations(IManager $dashboardManager): void {
while (($panel = array_shift($this->dashboardPanels)) !== null) {
try {
$dashboardManager->lazyRegisterWidget($panel->getService());
$dashboardManager->lazyRegisterWidget($panel->getService(), $panel->getAppId());
} catch (Throwable $e) {
$appId = $panel->getAppId();
$this->logger->error("Error during dashboard registration of $appId: " . $e->getMessage(), [
Expand Down
32 changes: 19 additions & 13 deletions lib/private/Dashboard/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
namespace OC\Dashboard;

use InvalidArgumentException;
use OCP\AppFramework\QueryException;
use OCP\App\IAppManager;
use OCP\Dashboard\IManager;
use OCP\Dashboard\IWidget;
use OCP\IServerContainer;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Throwable;
use Psr\Log\LoggerInterface;

Expand All @@ -42,11 +43,12 @@ class Manager implements IManager {
/** @var IWidget[] */
private $widgets = [];

/** @var IServerContainer */
private $serverContainer;
private ContainerInterface $serverContainer;
private IAppManager $appManager;

public function __construct(IServerContainer $serverContainer) {
public function __construct(ContainerInterface $serverContainer, IAppManager $appManager) {
$this->serverContainer = $serverContainer;
$this->appManager = $appManager;
}

private function registerWidget(IWidget $widget): void {
Expand All @@ -57,17 +59,21 @@ private function registerWidget(IWidget $widget): void {
$this->widgets[$widget->getId()] = $widget;
}

public function lazyRegisterWidget(string $widgetClass): void {
$this->lazyWidgets[] = $widgetClass;
public function lazyRegisterWidget(string $widgetClass, string $appId): void {
$this->lazyWidgets[] = ['class' => $widgetClass, 'appId' => $appId];
}

public function loadLazyPanels(): void {
$classes = $this->lazyWidgets;
foreach ($classes as $class) {
$services = $this->lazyWidgets;
foreach ($services as $service) {
try {
if (!$this->appManager->isEnabledForUser($service['appId'])) {
// all apps are registered, but some may not be enabled for the user
continue;
}
/** @var IWidget $widget */
$widget = $this->serverContainer->query($class);
} catch (QueryException $e) {
$widget = $this->serverContainer->get($service['class']);
} catch (ContainerExceptionInterface $e) {
/*
* There is a circular dependency between the logger and the registry, so
* we can not inject it. Thus the static call.
Expand All @@ -90,7 +96,7 @@ public function loadLazyPanels(): void {
*/
\OC::$server->get(LoggerInterface::class)->critical(
'Could not register lazy dashboard widget: ' . $e->getMessage(),
['excepiton' => $e]
['exception' => $e]
);
}

Expand All @@ -111,7 +117,7 @@ public function loadLazyPanels(): void {
} catch (Throwable $e) {
\OC::$server->get(LoggerInterface::class)->critical(
'Error during dashboard widget loading: ' . $e->getMessage(),
['excepiton' => $e]
['exception' => $e]
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Dashboard/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface IManager {
* @param string $widgetClass
* @since 20.0.0
*/
public function lazyRegisterWidget(string $widgetClass): void;
public function lazyRegisterWidget(string $widgetClass, string $appId): void;

/**
* @since 20.0.0
Expand Down

0 comments on commit 5c5c253

Please sign in to comment.