Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 21169384 authored by Georg Ehrke's avatar Georg Ehrke
Browse files

Add Event to allow other apps to register custom scripts

parent 9594a304
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -23,9 +23,11 @@ declare(strict_types=1);
 */
namespace OCA\Calendar\Controller;

use OCA\Calendar\Event\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
@@ -48,6 +50,11 @@ class PublicViewController extends Controller {
	 */
	private $initialStateService;

	/**
	 * @var IEventDispatcher
	 */
	private $dispatcher;

	/**
	 * @var IURLGenerator
	 */
@@ -58,16 +65,19 @@ class PublicViewController extends Controller {
	 * @param IRequest $request an instance of the request
	 * @param IConfig $config
	 * @param IInitialStateService $initialStateService
	 * @param IEventDispatcher $dispatcher
	 * @param IURLGenerator $urlGenerator
	 */
	public function __construct(string $appName,
								IRequest $request,
								IConfig $config,
								IInitialStateService $initialStateService,
								IEventDispatcher $dispatcher,
								IURLGenerator $urlGenerator) {
		parent::__construct($appName, $request);
		$this->config = $config;
		$this->initialStateService = $initialStateService;
		$this->dispatcher = $dispatcher;
		$this->urlGenerator = $urlGenerator;
	}

@@ -82,6 +92,8 @@ class PublicViewController extends Controller {
	 * @return TemplateResponse
	 */
	public function publicIndexWithBranding(string $token):TemplateResponse {
		$this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent(true, false, $token));

		return $this->publicIndex($token, 'public');
	}

@@ -96,6 +108,8 @@ class PublicViewController extends Controller {
	 * @return TemplateResponse
	 */
	public function publicIndexForEmbedding(string $token):TemplateResponse {
		$this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent(true, true, $token));

		$response = $this->publicIndex($token, 'base');
		$response->addHeader('X-Frame-Options', 'ALLOW');

+13 −1
Original line number Diff line number Diff line
@@ -23,9 +23,11 @@ declare(strict_types=1);
 */
namespace OCA\Calendar\Controller;

use OCA\Calendar\Event\BeforeTemplateRenderedEvent;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
@@ -52,6 +54,11 @@ class ViewController extends Controller {
	 */
	private $initialStateService;

	/**
	 * @var IEventDispatcher
	 */
	private $dispatcher;

	/**
	 * @var IAppManager
	 */
@@ -62,19 +69,22 @@ class ViewController extends Controller {
	 * @param IRequest $request an instance of the request
	 * @param IConfig $config
	 * @param IInitialStateService $initialStateService
	 * @param IEventDispatcher $dispatcher
	 * @param IAppManager $appManager
	 * @param string $userId
	 * @param string|null $userId
	 */
	public function __construct(string $appName,
								IRequest $request,
								IConfig $config,
								IInitialStateService $initialStateService,
								IEventDispatcher $dispatcher,
								IAppManager $appManager,
								?string $userId) {
		parent::__construct($appName, $request);
		$this->config = $config;
		$this->userId = $userId;
		$this->initialStateService = $initialStateService;
		$this->dispatcher = $dispatcher;
		$this->appManager = $appManager;
	}

@@ -123,6 +133,8 @@ class ViewController extends Controller {
		$this->initialStateService->provideInitialState($this->appName, 'show_tasks', $showTasks);
		$this->initialStateService->provideInitialState($this->appName, 'tasks_enabled', $tasksEnabled);

		$this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent(false, false, null));

		return new TemplateResponse($this->appName, 'main');
	}

+83 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);
/**
 * Calendar App
 *
 * @author Georg Ehrke
 * @copyright 2020 Georg Ehrke <oc.list@georgehrke.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OCA\Calendar\Event;

use OCP\EventDispatcher\Event;

/**
 * Emitted before the rendering step of the public share page happens. The event
 * holds a flag that specifies if it is the authentication page of a public share.
 *
 * @since 20.0.0
 */
class BeforeTemplateRenderedEvent extends Event {

	/** @var bool */
	private $isPublic;

	/** @var bool */
	private $isEmbedded;

	/** @var string|null */
	private $tokens;

	/**
	 * @param bool $isPublic
	 * @param bool $isEmbedded
	 * @param string|null $tokens
	 *
	 * @since 20.0.0
	 */
	public function __construct(bool $isPublic, bool $isEmbedded, ?string $tokens) {
		parent::__construct();

		$this->isPublic = $isPublic;
		$this->isEmbedded = $isEmbedded;
		$this->tokens = $tokens;
	}

	/**
	 * @since 20.0.0
	 * @return bool
	 */
	public function isPublic(): bool {
		return $this->isPublic;
	}

	/**
	 * @since 20.0.0
	 * @return bool
	 */
	public function isEmbedded(): bool {
		return $this->isEmbedded;
	}

	/**
	 * @since 20.0.0
	 * @return string|null
	 */
	public function getTokens(): ?string {
		return $this->tokens;
	}
}
+33 −3
Original line number Diff line number Diff line
@@ -23,7 +23,9 @@ declare(strict_types=1);
 */
namespace OCA\Calendar\Controller;

use OCA\Calendar\Event\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
@@ -44,6 +46,9 @@ class PublicViewControllerTest extends TestCase {
	/** @var IInitialStateService|\PHPUnit_Framework_MockObject_MockObject */
	private $initialStateService;

	/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
	private $dispatcher;

	/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
	private $urlGenerator;

@@ -55,10 +60,11 @@ class PublicViewControllerTest extends TestCase {
		$this->request = $this->createMock(IRequest::class);
		$this->config = $this->createMock(IConfig::class);
		$this->initialStateService = $this->createMock(IInitialStateService::class);
		$this->dispatcher = $this->createMock(IEventDispatcher::class);
		$this->urlGenerator = $this->createMock(IURLGenerator::class);

		$this->controller = new PublicViewController($this->appName, $this->request,
			$this->config, $this->initialStateService, $this->urlGenerator);
			$this->config, $this->initialStateService, $this->dispatcher, $this->urlGenerator);
	}

	public function testPublicIndexWithBranding():void {
@@ -158,7 +164,19 @@ class PublicViewControllerTest extends TestCase {
			->method('provideInitialState')
			->with('calendar', 'tasks_enabled', false);

		$response = $this->controller->publicIndexWithBranding('');
		$this->dispatcher->expects($this->once())
			->method('dispatchTyped')
			->with($this->callback(static function ($event) {
				if (!($event instanceof BeforeTemplateRenderedEvent)) {
					return false;
				}

				return $event->isPublic() === true
					&& $event->isEmbedded() === false
					&& $event->getTokens() === 'token123';
			}));

		$response = $this->controller->publicIndexWithBranding('token123');

		$this->assertInstanceOf(TemplateResponse::class, $response);
		$this->assertEquals([
@@ -266,7 +284,19 @@ class PublicViewControllerTest extends TestCase {
			->method('provideInitialState')
			->with('calendar', 'tasks_enabled', false);

		$response = $this->controller->publicIndexForEmbedding('');
		$this->dispatcher->expects($this->once())
			->method('dispatchTyped')
			->with($this->callback(static function ($event) {
				if (!($event instanceof BeforeTemplateRenderedEvent)) {
					return false;
				}

				return $event->isPublic() === true
					&& $event->isEmbedded() === true
					&& $event->getTokens() === 'token123';
			}));

		$response = $this->controller->publicIndexForEmbedding('token123');

		$this->assertInstanceOf(TemplateResponse::class, $response);
		$this->assertEquals([
+31 −1
Original line number Diff line number Diff line
@@ -23,8 +23,10 @@ declare(strict_types=1);
 */
namespace OCA\Calendar\Controller;

use OCA\Calendar\Event\BeforeTemplateRenderedEvent;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
@@ -47,6 +49,9 @@ class ViewControllerTest extends TestCase {
	/** @var IInitialStateService|\PHPUnit_Framework_MockObject_MockObject */
	private $initialStateService;

	/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
	private $dispatcher;

	/** @var string */
	private $userId;

@@ -59,10 +64,11 @@ class ViewControllerTest extends TestCase {
		$this->appManager = $this->createMock(IAppManager::class);
		$this->config = $this->createMock(IConfig::class);
		$this->initialStateService = $this->createMock(IInitialStateService::class);
		$this->dispatcher = $this->createMock(IEventDispatcher::class);
		$this->userId = 'user123';

		$this->controller = new ViewController($this->appName, $this->request,
			$this->config, $this->initialStateService, $this->appManager, $this->userId);
			$this->config, $this->initialStateService, $this->dispatcher, $this->appManager, $this->userId);
	}

	public function testIndex():void {
@@ -184,6 +190,18 @@ class ViewControllerTest extends TestCase {
			->method('provideInitialState')
			->with('calendar', 'tasks_enabled', true);

		$this->dispatcher->expects($this->once())
			->method('dispatchTyped')
			->with($this->callback(static function ($event) {
				if (!($event instanceof BeforeTemplateRenderedEvent)) {
					return false;
				}

				return $event->isPublic() === false
					&& $event->isEmbedded() === false
					&& $event->getTokens() === null;
			}));

		$response = $this->controller->index();

		$this->assertInstanceOf(TemplateResponse::class, $response);
@@ -317,6 +335,18 @@ class ViewControllerTest extends TestCase {
			->method('provideInitialState')
			->with('calendar', 'tasks_enabled', false);

		$this->dispatcher->expects($this->once())
			->method('dispatchTyped')
			->with($this->callback(static function ($event) {
				if (!($event instanceof BeforeTemplateRenderedEvent)) {
					return false;
				}

				return $event->isPublic() === false
					&& $event->isEmbedded() === false
					&& $event->getTokens() === null;
			}));

		$response = $this->controller->index();

		$this->assertInstanceOf(TemplateResponse::class, $response);
Loading