Skip to content

Commit ddc976b

Browse files
authored
Merge pull request #4786 from nextcloud/backport/4785/stable28
[stable28] fix(Delegation): Fix checks
2 parents b5de0e7 + 0bf251b commit ddc976b

5 files changed

Lines changed: 67 additions & 14 deletions

β€Žlib/AuthorizedAdminSettingMiddleware.phpβ€Ž

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,25 @@
2121

2222
namespace OCA\GroupFolders;
2323

24-
use Exception;
24+
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
25+
use OCA\GroupFolders\Folder\FolderManager;
2526
use OCA\GroupFolders\Service\DelegationService;
2627
use OCP\AppFramework\Http\JSONResponse;
2728
use OCP\AppFramework\Http\Response;
2829
use OCP\AppFramework\Http\TemplateResponse;
2930
use OCP\AppFramework\Middleware;
3031
use OCP\AppFramework\Utility\IControllerMethodReflector;
3132
use OCP\IRequest;
33+
use OCP\IUserSession;
3234

3335
class AuthorizedAdminSettingMiddleware extends Middleware {
34-
private DelegationService $delegatedService;
35-
private IControllerMethodReflector $reflector;
36-
private IRequest $request;
37-
3836
public function __construct(
39-
DelegationService $delegatedService,
40-
IControllerMethodReflector $reflector,
41-
IRequest $request
37+
private DelegationService $delegatedService,
38+
private IControllerMethodReflector $reflector,
39+
private IRequest $request,
40+
private IUserSession $userSession,
41+
private FolderManager $folderManager,
4242
) {
43-
$this->delegatedService = $delegatedService;
44-
$this->reflector = $reflector;
45-
$this->request = $request;
4643
}
4744

4845
/**
@@ -54,11 +51,24 @@ public function __construct(
5451
*
5552
*/
5653
public function beforeController($controller, $methodName) {
57-
if ($this->reflector->hasAnnotation('RequireGroupFolderAdmin')) {
58-
if (!$this->delegatedService->hasApiAccess()) {
59-
throw new Exception('Logged in user must be an admin, a sub admin or gotten special right to access this setting');
54+
if (!$this->reflector->hasAnnotation('RequireGroupFolderAdmin')) {
55+
return;
56+
}
57+
58+
if ($this->delegatedService->isAdminNextcloud() || $this->delegatedService->isDelegatedAdmin()) {
59+
return;
60+
}
61+
62+
if ($this->delegatedService->hasOnlyApiAccess()
63+
&& ($user = $this->userSession->getUser()) !== null
64+
&& ($id = $this->request->getParam('id')) !== null) {
65+
/** @var string $id */
66+
if ($this->folderManager->canManageACL((int)$id, $user)) {
67+
return;
6068
}
6169
}
70+
71+
throw new NotAdminException('Logged in user must be an admin, a sub admin or gotten special right to access this setting');
6272
}
6373

6474
/**

β€Žpsalm.xmlβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
>
1212
<stubs>
1313
<file name="tests/stub.phpstub" preloadClasses="true"/>
14+
<file name="tests/stubs/oc_appframework_middleware_security_exceptions_notadminexception.php" />
15+
<file name="tests/stubs/oc_appframework_middleware_security_exceptions_securityexception.php" />
1416
</stubs>
1517
<projectFiles>
1618
<directory name="lib" />

β€Žtests/phpunit.xmlβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</coverage>
2020
<testsuite name="Nextcloud - Group folders App Tests">
2121
<directory suffix=".php">.</directory>
22+
<exclude>./stubs/</exclude>
2223
</testsuite>
2324
<logging/>
2425
</phpunit>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OC\AppFramework\Middleware\Security\Exceptions;
11+
12+
/**
13+
* Class NotAdminException is thrown when a resource has been requested by a
14+
* non-admin user that is not accessible to non-admin users.
15+
*
16+
* @package OC\AppFramework\Middleware\Security\Exceptions
17+
*/
18+
class NotAdminException extends SecurityException {
19+
public function __construct(string $message) {
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OC\AppFramework\Middleware\Security\Exceptions;
11+
12+
/**
13+
* Class SecurityException is the base class for security exceptions thrown by
14+
* the security middleware.
15+
*
16+
* @package OC\AppFramework\Middleware\Security\Exceptions
17+
*/
18+
class SecurityException extends \Exception {
19+
}

0 commit comments

Comments
Β (0)