Skip to content

Commit f25daed

Browse files
authored
Merge pull request #1215 from nextcloud/backport/1198/stable4.7
[stable4.7] #1197: improve performance issue (N+1)
2 parents e468036 + 9a53144 commit f25daed

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

lib/Checker.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
class Checker {
2222
private array $termsCache = [];
23+
private ?bool $currentUserHasSignedCache = null;
2324

2425
public function __construct(
2526
private readonly IRequest $request,
@@ -44,24 +45,29 @@ public function getForbiddenMessage(): string {
4445
* for the login action
4546
*/
4647
public function currentUserHasSigned(): bool {
48+
// return cached result if this request is already computed.
49+
if ($this->currentUserHasSignedCache !== null) {
50+
return $this->currentUserHasSignedCache;
51+
}
52+
4753
$uuid = $this->appConfig->getAppValueString('term_uuid');
4854
$user = $this->userSession->getUser();
4955
if ($user === null) {
5056
if (!$this->appConfig->getAppValueBool('tos_on_public_shares')) {
51-
return true;
57+
return $this->currentUserHasSignedCache = true;
5258
}
5359
} elseif (!$this->appConfig->getAppValueBool('tos_for_users', true)) {
54-
return true;
60+
return $this->currentUserHasSignedCache = true;
5561
}
5662

5763
if ($this->isAllowedRequest()) {
5864
// Services such as Collabora doing requests for the user
59-
return true;
65+
return $this->currentUserHasSignedCache = true;
6066
}
6167

6268
// sign_public only for guests stores acceptance in session, logged in users must have a row in sigs.
6369
if ($user === null && $this->session->get('term_uuid') === $uuid) {
64-
return true;
70+
return $this->currentUserHasSignedCache = true;
6571
}
6672

6773
$countryCode = $this->countryDetector->getCountry();
@@ -70,24 +76,24 @@ public function currentUserHasSigned(): bool {
7076
}
7177
if (empty($this->termsCache[$countryCode])) {
7278
// No terms that would need accepting
73-
return true;
79+
return $this->currentUserHasSignedCache = true;
7480
}
7581

7682
if (!$user instanceof IUser) {
77-
return false;
83+
return $this->currentUserHasSignedCache = false;
7884
}
7985

8086
$signatories = $this->signatoryMapper->getSignatoriesByUser($user);
8187
foreach ($signatories as $signatory) {
8288
foreach ($this->termsCache[$countryCode] as $term) {
8389
if ($term->getId() === $signatory->getTermsId()) {
8490
$this->session->set('term_uuid', $uuid);
85-
return true;
91+
return $this->currentUserHasSignedCache = true;
8692
}
8793
}
8894
}
8995

90-
return false;
96+
return $this->currentUserHasSignedCache = false;
9197
}
9298

9399
protected function isAllowedRequest(): bool {

0 commit comments

Comments
 (0)