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
6 changes: 6 additions & 0 deletions apps/dav/lib/Controller/DirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Security\ISecureRandom;
use OCP\Share\IManager;

class DirectController extends OCSController {

Expand All @@ -38,6 +39,7 @@ public function __construct(
private ITimeFactory $timeFactory,
private IURLGenerator $urlGenerator,
private IEventDispatcher $eventDispatcher,
private IManager $shareManager,
) {
parent::__construct($appName, $request);
}
Expand All @@ -56,6 +58,10 @@ public function __construct(
*/
#[NoAdminRequired]
public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
if (!$this->shareManager->shareApiAllowLinks()) {
throw new OCSForbiddenException('Creating direct links is disabled');
}

$userFolder = $this->rootFolder->getUserFolder($this->userId);

$file = $userFolder->getFirstNodeById($fileId);
Expand Down
35 changes: 34 additions & 1 deletion apps/dav/tests/unit/Controller/DirectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\DAV\Db\DirectMapper;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
Expand All @@ -22,6 +23,7 @@
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Security\ISecureRandom;
use OCP\Share\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

Expand All @@ -32,6 +34,7 @@ class DirectControllerTest extends TestCase {
private ITimeFactory&MockObject $timeFactory;
private IURLGenerator&MockObject $urlGenerator;
private IEventDispatcher&MockObject $eventDispatcher;
private IManager&MockObject $shareManager;

private DirectController $controller;

Expand All @@ -44,6 +47,7 @@ protected function setUp(): void {
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->shareManager = $this->createMock(IManager::class);

$this->controller = new DirectController(
'dav',
Expand All @@ -54,11 +58,15 @@ protected function setUp(): void {
$this->random,
$this->timeFactory,
$this->urlGenerator,
$this->eventDispatcher
$this->eventDispatcher,
$this->shareManager,
);
}

public function testGetUrlNonExistingFileId(): void {
$this->shareManager->method('shareApiAllowLinks')
->willReturn(true);

$userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')
->with('awesomeUser')
Expand All @@ -73,6 +81,9 @@ public function testGetUrlNonExistingFileId(): void {
}

public function testGetUrlForFolder(): void {
$this->shareManager->method('shareApiAllowLinks')
->willReturn(true);

$userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')
->with('awesomeUser')
Expand All @@ -89,6 +100,9 @@ public function testGetUrlForFolder(): void {
}

public function testGetUrlValid(): void {
$this->shareManager->method('shareApiAllowLinks')
->willReturn(true);

$userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')
->with('awesomeUser')
Expand Down Expand Up @@ -135,4 +149,23 @@ public function testGetUrlValid(): void {
'url' => 'https://my.nextcloud/remote.php/direct/superduperlongtoken',
], $result->getData());
}

public function testGetUrlNoLinkShares(): void {
$this->shareManager->method('shareApiAllowLinks')
->willReturn(false);

$userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')
->with('awesomeUser')
->willReturn($userFolder);

$file = $this->createMock(File::class);

$userFolder->method('getFirstNodeById')
->with(101)
->willReturn($file);

$this->expectException(OCSForbiddenException::class);
$this->controller->getUrl(101);
}
}
Loading