aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/lib/Listener/BeforeTemplateRenderedListener.php
blob: 18ab9392b975ebc914c2ba2a76eb8c99ebefc853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\Theming\Listener;

use OCA\Theming\AppInfo\Application;
use OCA\Theming\Service\JSDataService;
use OCA\Theming\Service\ThemeInjectionService;
use OCP\AppFramework\Http\Events\BeforeLoginTemplateRenderedEvent;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\IUserSession;
use OCP\Util;
use Psr\Container\ContainerInterface;

/** @template-implements IEventListener<BeforeTemplateRenderedEvent|BeforeLoginTemplateRenderedEvent> */
class BeforeTemplateRenderedListener implements IEventListener {

	public function __construct(
		private IInitialState $initialState,
		private ContainerInterface $container,
		private ThemeInjectionService $themeInjectionService,
		private IUserSession $userSession,
		private IConfig $config,
	) {
	}

	public function handle(Event $event): void {
		$this->initialState->provideLazyInitialState(
			'data',
			fn () => $this->container->get(JSDataService::class),
		);

		/** @var BeforeTemplateRenderedEvent|BeforeLoginTemplateRenderedEvent $event */
		if ($event->getResponse()->getRenderAs() === TemplateResponse::RENDER_AS_USER) {
			$this->initialState->provideLazyInitialState('shortcutsDisabled', function () {
				if ($this->userSession->getUser()) {
					$uid = $this->userSession->getUser()->getUID();
					return $this->config->getUserValue($uid, Application::APP_ID, 'shortcuts_disabled', 'no') === 'yes';
				}
				return false;
			});
		}

		$this->themeInjectionService->injectHeaders();

		// Making sure to inject just after core
		Util::addScript('theming', 'theming', 'core');
	}
}