Skip to content
Draft
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
14 changes: 13 additions & 1 deletion apps/files_external/ajax/applicable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@
\OC_JSON::checkAppEnabled('files_external');
\OC_JSON::callCheck();

\OC_JSON::checkAdminUser();
$currentUser = \OC::$server->getUserSession()->getUser();
if ($currentUser === null) {
\OC_JSON::error(['message' => 'Not logged in']);
exit();
}
$groupManager = \OC::$server->getGroupManager();

Check failure on line 20 in apps/files_external/ajax/applicable.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

apps/files_external/ajax/applicable.php:20:31: DeprecatedMethod: The method OC\Server::getGroupManager has been marked as deprecated (see https://psalm.dev/001)
$authorizedGroupMapper = \OC::$server->get(\OC\Settings\AuthorizedGroupMapper::class);
$isAdmin = $groupManager->isAdmin($currentUser->getUID());
$isDelegated = in_array(\OCA\Files_External\Settings\Admin::class, $authorizedGroupMapper->findAllClassesForUser($currentUser), true);
if (!$isAdmin && !$isDelegated) {
\OC_JSON::error(['message' => 'Not authorized']);

Check failure on line 25 in apps/files_external/ajax/applicable.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

apps/files_external/ajax/applicable.php:25:2: DeprecatedMethod: The method OC_JSON::error has been marked as deprecated (see https://psalm.dev/001)
exit();
}

$pattern = '';
$limit = null;
Expand All @@ -39,4 +51,4 @@

$results = ['groups' => $groups, 'users' => $users];

\OC_JSON::success($results);

Check failure on line 54 in apps/files_external/ajax/applicable.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

apps/files_external/ajax/applicable.php:54:1: DeprecatedMethod: The method OC_JSON::success has been marked as deprecated (see https://psalm.dev/001)
6 changes: 5 additions & 1 deletion apps/files_external/lib/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
*/
namespace OCA\Files_External\Controller;

use OC\Settings\AuthorizedGroupMapper;
use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
use OCA\Files_External\Lib\Auth\PublicKey\RSA;
use OCA\Files_External\Settings\Admin;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand Down Expand Up @@ -36,6 +38,7 @@ public function __construct(
private IUserSession $userSession,
private IGroupManager $groupManager,
private IL10N $l10n,
private AuthorizedGroupMapper $authorizedGroupMapper,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -87,9 +90,10 @@ public function saveGlobalCredentials($uid, $user, $password): JSONResponse {
}

// Non-admins can only edit their own credentials
// Admin can edit global credentials
// Admin or delegated admin can edit global credentials
$allowedToEdit = $uid === ''
? $this->groupManager->isAdmin($currentUser->getUID())
|| in_array(Admin::class, $this->authorizedGroupMapper->findAllClassesForUser($currentUser), true)
: $currentUser->getUID() === $uid;

if ($allowedToEdit) {
Expand Down
20 changes: 20 additions & 0 deletions apps/files_external/lib/Controller/GlobalStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\GlobalStoragesService;
use OCA\Files_External\Settings\Admin;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
Expand Down Expand Up @@ -71,6 +73,7 @@ public function __construct(
*
* @return DataResponse
*/
#[AuthorizedAdminSetting(settings: Admin::class)]
#[PasswordConfirmationRequired(strict: true)]
public function create(
$mountPoint,
Expand Down Expand Up @@ -136,6 +139,7 @@ public function create(
*
* @return DataResponse
*/
#[AuthorizedAdminSetting(settings: Admin::class)]
#[PasswordConfirmationRequired(strict: true)]
public function update(
$id,
Expand Down Expand Up @@ -186,4 +190,20 @@ public function update(
Http::STATUS_OK
);
}

#[AuthorizedAdminSetting(settings: Admin::class)]
public function index() {
return parent::index();
}

#[AuthorizedAdminSetting(settings: Admin::class)]
public function show(int $id, $testOnly = true) {
return parent::show($id, $testOnly);
}

#[AuthorizedAdminSetting(settings: Admin::class)]
#[PasswordConfirmationRequired(strict: true)]
public function destroy(int $id) {
return parent::destroy($id);
}
}
15 changes: 12 additions & 3 deletions apps/files_external/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Encryption\IManager;
use OCP\Settings\ISettings;

class Admin implements ISettings {
use OCP\IL10N;
use OCP\Settings\IDelegatedSettings;

class Admin implements IDelegatedSettings {
public function __construct(
private IManager $encryptionManager,
private GlobalStoragesService $globalStoragesService,
private BackendService $backendService,
private GlobalAuth $globalAuth,
private IL10N $l10n,
) {
}

Expand Down Expand Up @@ -60,4 +61,12 @@ public function getSection() {
public function getPriority() {
return 40;
}

public function getName(): string {
return $this->l10n->t('External storage');
}

public function getAuthorizedAppConfig(): array {
return [];
}
}
72 changes: 72 additions & 0 deletions apps/files_external/tests/Controller/AjaxControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
*/
namespace OCA\Files_External\Tests\Controller;

use OC\Settings\AuthorizedGroupMapper;
use OCA\Files_External\Controller\AjaxController;
use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
use OCA\Files_External\Lib\Auth\PublicKey\RSA;
use OCA\Files_External\Settings\Admin;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IGroupManager;
use OCP\IL10N;
Expand All @@ -26,6 +28,7 @@ class AjaxControllerTest extends TestCase {
private IUserSession&MockObject $userSession;
private IGroupManager&MockObject $groupManager;
private IL10N&MockObject $l10n;
private AuthorizedGroupMapper&MockObject $authorizedGroupMapper;
private AjaxController $ajaxController;

protected function setUp(): void {
Expand All @@ -35,6 +38,7 @@ protected function setUp(): void {
$this->userSession = $this->createMock(IUserSession::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->l10n = $this->createMock(IL10N::class);
$this->authorizedGroupMapper = $this->createMock(AuthorizedGroupMapper::class);

$this->ajaxController = new AjaxController(
'files_external',
Expand All @@ -44,6 +48,7 @@ protected function setUp(): void {
$this->userSession,
$this->groupManager,
$this->l10n,
$this->authorizedGroupMapper,
);

$this->l10n->expects($this->any())
Expand Down Expand Up @@ -149,4 +154,71 @@ public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void {
$this->assertSame($response->getStatus(), 403);
$this->assertSame('Permission denied', $response->getData()['message']);
}

public function testSaveGlobalCredentialsAsAdminForGlobal(): void {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('MyAdminUid');
$this->userSession->method('getUser')->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('MyAdminUid')
->willReturn(true);
$this->authorizedGroupMapper
->expects($this->never())
->method('findAllClassesForUser');
$this->globalAuth
->expects($this->once())
->method('saveAuth')
->with('', 'test', 'password');

$response = $this->ajaxController->saveGlobalCredentials('', 'test', 'password');
$this->assertSame(200, $response->getStatus());
}

public function testSaveGlobalCredentialsAsDelegatedAdminForGlobal(): void {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('DelegatedUid');
$this->userSession->method('getUser')->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('DelegatedUid')
->willReturn(false);
$this->authorizedGroupMapper
->expects($this->once())
->method('findAllClassesForUser')
->with($user)
->willReturn([Admin::class]);
$this->globalAuth
->expects($this->once())
->method('saveAuth')
->with('', 'test', 'password');

$response = $this->ajaxController->saveGlobalCredentials('', 'test', 'password');
$this->assertSame(200, $response->getStatus());
}

public function testSaveGlobalCredentialsAsNormalUserForGlobal(): void {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('NormalUid');
$this->userSession->method('getUser')->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('NormalUid')
->willReturn(false);
$this->authorizedGroupMapper
->expects($this->once())
->method('findAllClassesForUser')
->with($user)
->willReturn([]);
$this->globalAuth
->expects($this->never())
->method('saveAuth');

$response = $this->ajaxController->saveGlobalCredentials('', 'test', 'password');
$this->assertSame(403, $response->getStatus());
$this->assertSame('Permission denied', $response->getData()['message']);
}
}
27 changes: 26 additions & 1 deletion apps/files_external/tests/Settings/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Files_External\Settings\Admin;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Encryption\IManager;
use OCP\IL10N;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

Expand All @@ -22,6 +23,7 @@ class AdminTest extends TestCase {
private GlobalStoragesService&MockObject $globalStoragesService;
private BackendService&MockObject $backendService;
private GlobalAuth&MockObject $globalAuth;
private IL10N&MockObject $l10n;
private Admin $admin;

protected function setUp(): void {
Expand All @@ -30,12 +32,17 @@ protected function setUp(): void {
$this->globalStoragesService = $this->createMock(GlobalStoragesService::class);
$this->backendService = $this->createMock(BackendService::class);
$this->globalAuth = $this->createMock(GlobalAuth::class);
$this->l10n = $this->createMock(IL10N::class);
$this->l10n->method('t')->willReturnCallback(function ($text) {
return $text;
});

$this->admin = new Admin(
$this->encryptionManager,
$this->globalStoragesService,
$this->backendService,
$this->globalAuth
$this->globalAuth,
$this->l10n
);
}

Expand Down Expand Up @@ -91,4 +98,22 @@ public function testGetSection(): void {
public function testGetPriority(): void {
$this->assertSame(40, $this->admin->getPriority());
}

public function testGetName(): void {
$this->l10n->expects($this->once())
->method('t')
->with('External storage')
->willReturn('External storage');

$this->assertSame('External storage', $this->admin->getName());
}

public function testGetAuthorizedAppConfig(): void {
$this->assertSame([], $this->admin->getAuthorizedAppConfig());
}

public function testImplementsIDelegatedSettings(): void {
$this->assertInstanceOf(\OCP\Settings\IDelegatedSettings::class, $this->admin);
$this->assertInstanceOf(\OCP\Settings\ISettings::class, $this->admin);
}
}
Loading