Skip to content

Commit f766153

Browse files
committed
fixup! fix(dav): Limit share/unshare requests per user
1 parent b76a3ba commit f766153

2 files changed

Lines changed: 21 additions & 29 deletions

File tree

apps/dav/lib/DAV/Security/RateLimiting.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,24 @@
1111

1212
use OCA\DAV\Connector\Sabre\Exception\TooManyRequests;
1313
use OCP\IAppConfig;
14-
use OCP\IUserManager;
14+
use OCP\IUserSession;
1515
use OCP\Security\RateLimiting\ILimiter;
1616
use OCP\Security\RateLimiting\IRateLimitExceededException;
1717

1818
class RateLimiting {
1919

2020
public function __construct(
21-
private readonly ILimiter $limiter,
22-
private readonly IUserManager $userManager,
21+
private readonly IUserSession $userSession,
2322
private readonly IAppConfig $config,
24-
private readonly ?string $userId,
23+
private readonly ILimiter $limiter,
2524
) {
2625
}
2726

2827
/**
2928
* @throws TooManyRequests
3029
*/
3130
public function check(): void {
32-
if ($this->userId === null) {
33-
return;
34-
}
35-
36-
$user = $this->userManager->get($this->userId);
31+
$user = $this->userSession->getUser();
3732
if ($user === null) {
3833
return;
3934
}

apps/dav/tests/unit/DAV/Security/RateLimitingTest.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,47 @@
1313
use OCA\DAV\DAV\Security\RateLimiting;
1414
use OCP\IAppConfig;
1515
use OCP\IUser;
16-
use OCP\IUserManager;
16+
use OCP\IUserSession;
1717
use OCP\Security\RateLimiting\ILimiter;
1818
use OCP\Security\RateLimiting\IRateLimitExceededException;
1919
use PHPUnit\Framework\MockObject\MockObject;
2020
use Test\TestCase;
2121

2222
class RateLimitingTest extends TestCase {
23-
private ILimiter&MockObject $limiter;
24-
private IUserManager&MockObject $userManager;
23+
private IUserSession $userSession;
2524
private IAppConfig&MockObject $config;
25+
private ILimiter&MockObject $limiter;
2626
private RateLimiting $rateLimiting;
2727
private string $userId = 'user123';
2828

2929
protected function setUp(): void {
3030
parent::setUp();
3131

32-
$this->limiter = $this->createMock(ILimiter::class);
33-
$this->userManager = $this->createMock(IUserManager::class);
32+
$this->userSession = $this->createMock(IUserSession::class);
3433
$this->config = $this->createMock(IAppConfig::class);
34+
$this->limiter = $this->createMock(ILimiter::class);
35+
3536
$this->rateLimiting = new RateLimiting(
36-
$this->limiter,
37-
$this->userManager,
37+
$this->userSession,
3838
$this->config,
39-
$this->userId,
39+
$this->limiter,
4040
);
4141
}
4242

4343
public function testNoUserObject(): void {
44-
$this->userManager->expects(self::once())
45-
->method('get')
46-
->with($this->userId)
44+
$this->userSession->expects($this->once())
45+
->method('getUser')
4746
->willReturn(null);
48-
$this->limiter->expects(self::never())
47+
$this->limiter->expects($this->never())
4948
->method('registerUserRequest');
5049

5150
$this->rateLimiting->check();
5251
}
5352

5453
public function testRegisterShareRequest(): void {
5554
$user = $this->createMock(IUser::class);
56-
$this->userManager->expects(self::once())
57-
->method('get')
58-
->with($this->userId)
55+
$this->userSession->expects($this->once())
56+
->method('getUser')
5957
->willReturn($user);
6058
$this->config->method('getValueInt')
6159
->willReturnCallback(static function (string $app, string $key, int $default): int {
@@ -65,7 +63,7 @@ public function testRegisterShareRequest(): void {
6563
default => $default,
6664
};
6765
});
68-
$this->limiter->expects(self::once())
66+
$this->limiter->expects($this->once())
6967
->method('registerUserRequest')
7068
->with(
7169
'share-addressbook-or-calendar',
@@ -79,13 +77,12 @@ public function testRegisterShareRequest(): void {
7977

8078
public function testShareRequestRateLimitExceeded(): void {
8179
$user = $this->createMock(IUser::class);
82-
$this->userManager->expects(self::once())
83-
->method('get')
84-
->with($this->userId)
80+
$this->userSession->expects($this->once())
81+
->method('getUser')
8582
->willReturn($user);
8683
$this->config->method('getValueInt')
8784
->willReturnArgument(2);
88-
$this->limiter->expects(self::once())
85+
$this->limiter->expects($this->once())
8986
->method('registerUserRequest')
9087
->with(
9188
'share-addressbook-or-calendar',

0 commit comments

Comments
 (0)