Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

class CurrentUser {

private bool $publicShareLoaded = false;
private ?IShare $publicShare = null;

public function __construct(
protected readonly IUserSession $userSession,
protected readonly IRequest $request,
Expand Down Expand Up @@ -107,6 +110,15 @@ protected function getCloudIDFromToken(): ?string {
}

protected function getPublicShare(): ?IShare {
if (!$this->publicShareLoaded) {
$this->publicShare = $this->loadPublicShare();
$this->publicShareLoaded = true;
}

return $this->publicShare;
}

private function loadPublicShare(): ?IShare {
if (basename($this->request->getScriptName()) !== 'public.php') {
return null;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/CurrentUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,34 @@ public function testGetCloudIDFromToken(array $server, array $shareData, ?string
$this->assertSame($expected, self::invokePrivate($instance, 'getCloudIDFromToken'));
}

public static function dataPublicShareIsLoadedOnce(): array {
return [
[IShare::TYPE_LINK, true],
[new ShareNotFound(), false],
];
}

#[DataProvider('dataPublicShareIsLoadedOnce')]
public function testPublicShareIsLoadedOnce(int|ShareNotFound $result, bool $expected): void {
$this->request->server = ['PHP_AUTH_USER' => 'token'];
$method = $this->shareManager->expects($this->once())
->method('getShareByToken')
->with('token');
if ($result instanceof ShareNotFound) {
$method->willThrowException($result);
} else {
$share = $this->createMock(IShare::class);
$share->method('getShareType')
->willReturn($result);
$method->willReturn($share);
}

$instance = $this->getInstance();
$this->assertSame($expected, $instance->isPublicShareToken());
$this->assertSame($expected, $instance->isPublicShareToken());
$this->assertNull(self::invokePrivate($instance, 'getCloudIDFromToken'));
}

public function testGetCloudIdFromUser(): void {
$user = $this->createMock(IUser::class);
$user->method('getCloudId')->willReturn('user@cloud.example.com');
Expand Down
34 changes: 34 additions & 0 deletions tests/benchmark/profile-prepend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* Loaded through auto_prepend_file: samples requests that carry an
* X-Bench-Profile header and writes the stacks in collapsed format,
* and clears the web server's APCu for requests with X-Bench-Clear-Cache.
*/

if (isset($_SERVER['HTTP_X_BENCH_CLEAR_CACHE']) && function_exists('apcu_clear_cache')) {
apcu_clear_cache();
}

if (isset($_SERVER['HTTP_X_BENCH_PROFILE']) && class_exists(ExcimerProfiler::class)) {
$benchProfiler = new ExcimerProfiler();
$benchProfiler->setPeriod(0.001);
$benchProfiler->setEventType(EXCIMER_REAL);
$benchProfiler->setMaxDepth(250);
$benchProfiler->start();

register_shutdown_function(static function () use ($benchProfiler): void {
$benchProfiler->stop();
$label = preg_replace('/[^A-Za-z0-9_.-]/', '', (string)$_SERVER['HTTP_X_BENCH_PROFILE']);
$dir = '/tmp/bench-profiles/' . $label;
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
file_put_contents($dir . '/' . uniqid('', true) . '.collapsed', $benchProfiler->getLog()->formatCollapsed());
});
}
Loading