Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make BeforeTemplateRenderedEvent aware of the actual response #23012

Merged
merged 2 commits into from Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -28,6 +28,7 @@
use OCA\UserStatus\AppInfo\Application;
use OCA\UserStatus\Service\JSDataService;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IInitialStateService;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function handle(Event $event): void {
return;
}

if (!$event->isLoggedIn()) {
if (!$event->isLoggedIn() || $event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_USER) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue I see here is that there will be a lot of places where we will never do this check.
But yeah I don't see any other solutiopn either as an extra event will also just have this loaded on each page.

return;
}

Expand Down
Expand Up @@ -71,7 +71,7 @@ public function afterController($controller, $methodName, Response $response): R
$isLoggedIn = false;
}

$this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($isLoggedIn));
$this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($isLoggedIn, $response));
}

return $response;
Expand Down
Expand Up @@ -27,6 +27,7 @@

namespace OCP\AppFramework\Http\Events;

use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\Event;

/**
Expand All @@ -38,14 +39,17 @@
class BeforeTemplateRenderedEvent extends Event {
/** @var bool */
private $loggedIn;
/** @var TemplateResponse */
private $response;

/**
* @since 20.0.0
*/
public function __construct(bool $loggedIn) {
public function __construct(bool $loggedIn, TemplateResponse $response) {
parent::__construct();

$this->loggedIn = $loggedIn;
$this->response = $response;
}

/**
Expand All @@ -54,4 +58,11 @@ public function __construct(bool $loggedIn) {
public function isLoggedIn(): bool {
return $this->loggedIn;
}

/**
* @since 20.0.0
*/
public function getResponse(): TemplateResponse {
return $this->response;
}
}