Skip to content

Commit

Permalink
fix: Cache if no share is found for current poll in Acl
Browse files Browse the repository at this point in the history
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 <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Apr 23, 2024
1 parent dae670c commit 07d8023
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/Model/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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');
}
Expand Down

0 comments on commit 07d8023

Please sign in to comment.