From 07d802377dc2de2f607538886a0fa202f61f37fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 23 Apr 2024 16:08:48 +0200 Subject: [PATCH] fix: Cache if no share is found for current poll in Acl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Not pretty but avoids a few DB calls. Ideally we should not do these requests at all, I do not really understand this share part in Acl. Signed-off-by: Côme Chilliet --- lib/Model/Acl.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index 6b09bcc1d..07bba7a59 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -70,6 +70,7 @@ class Acl implements JsonSerializable { public const PERMISSION_ALL_ACCESS = 'allAccess'; private ?int $pollId = null; private ?UserBase $currentUser = null; + private bool $noShare = false; /** @@ -162,6 +163,7 @@ public function setPollId(?int $pollId = null, string $permission = self::PERMIS public function setPoll(Poll $poll, string $permission = self::PERMISSION_POLL_VIEW): static { $this->pollId = $poll->getId(); $this->poll = $poll; + $this->noShare = false; $this->request($permission); return $this; @@ -199,6 +201,7 @@ private function loadPoll(): void { try { // otherwise load poll from db $this->poll = $this->pollMapper->find((int) $this->pollId); + $this->noShare = false; } catch (DoesNotExistException $e) { throw new NotFoundException('Error loading poll with id ' . $this->pollId); } @@ -211,16 +214,24 @@ private function loadPoll(): void { * and the pollId will get set to the share's pollId */ private function loadShare(): void { + if ($this->noShare) { + throw new ShareNotFoundException('No token was set for ACL'); + } + // no token in session, try to find a user, who matches if (!$this->getToken()) { if ($this->getCurrentUser()->getIsLoggedIn()) { // search for logged in user's share, load it and return + $this->noShare = true; $this->share = $this->shareMapper->findByPollAndUser($this->getPollId(), $this->getUserId()); + /* If previous call did not throw, there is a share */ + $this->noShare = false; // store share in session for further validations // $this->session->set(AppConstants::SESSION_KEY_SHARE_TOKEN, $this->share->getToken()); return; } else { $this->share = new Share(); + $this->noShare = true; // must fail, if no token is present and not logged in throw new ShareNotFoundException('No token was set for ACL'); }