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
33 changes: 31 additions & 2 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace OCA\Files_Sharing\Controller;

use OCA\Files\Helper;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSException;
Expand Down Expand Up @@ -81,6 +82,8 @@ class ShareAPIController extends OCSController {
private $lockedNode;
/** @var IConfig */
private $config;
/** @var IAppManager */
private $appManager;

/**
* Share20OCS constructor.
Expand All @@ -95,6 +98,7 @@ class ShareAPIController extends OCSController {
* @param string $userId
* @param IL10N $l10n
* @param IConfig $config
* @param IAppManager $appManager
*/
public function __construct(
string $appName,
Expand All @@ -106,7 +110,8 @@ public function __construct(
IURLGenerator $urlGenerator,
string $userId,
IL10N $l10n,
IConfig $config
IConfig $config,
IAppManager $appManager
) {
parent::__construct($appName, $request);

Expand All @@ -119,6 +124,7 @@ public function __construct(
$this->currentUser = $userId;
$this->l = $l10n;
$this->config = $config;
$this->appManager = $appManager;
}

/**
Expand Down Expand Up @@ -206,6 +212,7 @@ protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = n
} else if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) {
$result['share_with'] = $share->getSharedWith();
$result['password'] = $share->getPassword();
$result['send_password_by_talk'] = $share->getSendPasswordByTalk();
$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
$result['token'] = $share->getToken();
} else if ($share->getShareType() === Share::SHARE_TYPE_CIRCLE) {
Expand Down Expand Up @@ -328,6 +335,7 @@ public function deleteShare(string $id): DataResponse {
* @param string $shareWith
* @param string $publicUpload
* @param string $password
* @param bool $sendPasswordByTalk
* @param string $expireDate
*
* @return DataResponse
Expand All @@ -345,6 +353,7 @@ public function createShare(
string $shareWith = null,
string $publicUpload = 'false',
string $password = '',
string $sendPasswordByTalk = null,
string $expireDate = ''
): DataResponse {
$share = $this->shareManager->newShare();
Expand Down Expand Up @@ -485,6 +494,14 @@ public function createShare(
$share->setPermissions($permissions);
}
$share->setSharedWith($shareWith);

if ($sendPasswordByTalk === 'true') {
if (!$this->appManager->isEnabledForUser('spreed')) {
throw new OCSForbiddenException($this->l->t('Sharing %s sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled', [$path->getPath()]));
}

$share->setSendPasswordByTalk(true);
}
} else if ($shareType === Share::SHARE_TYPE_CIRCLE) {
if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
Expand Down Expand Up @@ -697,6 +714,7 @@ public function getShares(
* @param string $id
* @param int $permissions
* @param string $password
* @param string $sendPasswordByTalk
* @param string $publicUpload
* @param string $expireDate
* @param string $note
Expand All @@ -711,6 +729,7 @@ public function updateShare(
string $id,
int $permissions = null,
string $password = null,
string $sendPasswordByTalk = null,
string $publicUpload = null,
string $expireDate = null,
string $note = null
Expand All @@ -727,7 +746,7 @@ public function updateShare(
throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
}

if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null && $note === null) {
if ($permissions === null && $password === null && $sendPasswordByTalk === null && $publicUpload === null && $expireDate === null && $note === null) {
throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
}

Expand Down Expand Up @@ -816,6 +835,16 @@ public function updateShare(
} else if ($password !== null) {
$share->setPassword($password);
}

if ($sendPasswordByTalk === 'true') {
if (!$this->appManager->isEnabledForUser('spreed')) {
throw new OCSForbiddenException($this->l->t('Sharing sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled'));
}

$share->setSendPasswordByTalk(true);
} else {
$share->setSendPasswordByTalk(false);
}
}

if ($expireDate === '') {
Expand Down
29 changes: 29 additions & 0 deletions apps/files_sharing/lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use OCP\Files\IRootFolder;
use OCP\Share\Exceptions\ShareNotFound;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCP\Share\IManager as ShareManager;

/**
Expand Down Expand Up @@ -143,6 +144,34 @@ public function __construct(string $appName,
$this->shareManager = $shareManager;
}

/**
* @PublicPage
* @NoCSRFRequired
*
* Show the authentication page
* The form has to submit to the authenticate method route
*/
public function showAuthenticate(): TemplateResponse {
$templateParameters = ['share' => $this->share];

$event = new GenericEvent(null, $templateParameters);
$this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event);

return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest');
}

/**
* The template to show when authentication failed
*/
protected function showAuthFailed(): TemplateResponse {
$templateParameters = ['share' => $this->share, 'wrongpw' => true];

$event = new GenericEvent(null, $templateParameters);
$this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event);

return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest');
}

protected function verifyPassword(string $password): bool {
return $this->shareManager->checkPassword($this->share, $password);
}
Expand Down
17 changes: 10 additions & 7 deletions apps/files_sharing/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

use OC\Files\Cache\Scanner;
use OCA\Files_Sharing\Controller\ShareAPIController;
use OCP\App\IAppManager;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSForbiddenException;
Expand Down Expand Up @@ -107,6 +108,7 @@ private function createOCS($userId) {
return vsprintf($text, $parameters);
}));
$config = $this->createMock(IConfig::class);
$appManager = $this->createMock(IAppManager::class);

return new ShareAPIController(
self::APP_NAME,
Expand All @@ -118,7 +120,8 @@ private function createOCS($userId) {
\OC::$server->getURLGenerator(),
$userId,
$l,
$config
$config,
$appManager
);
}

Expand Down Expand Up @@ -960,7 +963,7 @@ function testUpdateShareUpload() {

// update public upload
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
$ocs->updateShare($share1->getId(), null, null, 'true');
$ocs->updateShare($share1->getId(), null, null, null, 'true');
$ocs->cleanup();

$share1 = $this->shareManager->getShareById($share1->getFullId());
Expand Down Expand Up @@ -1003,7 +1006,7 @@ function testUpdateShareExpireDate() {

// update expire date to a valid value
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
$ocs->updateShare($share1->getId(), null, null, null, $dateWithinRange->format('Y-m-d'));
$ocs->updateShare($share1->getId(), null, null, null, null, $dateWithinRange->format('Y-m-d'));
$ocs->cleanup();

$share1 = $this->shareManager->getShareById($share1->getFullId());
Expand Down Expand Up @@ -1234,7 +1237,7 @@ public function testPublicLinkExpireDate($date, $valid) {
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);

try {
$result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date);
$result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', null, $date);
$this->assertTrue($valid);
} catch (OCSNotFoundException $e) {
$this->assertFalse($valid);
Expand Down Expand Up @@ -1270,7 +1273,7 @@ public function testCreatePublicLinkExpireDateValid() {
$date->add(new \DateInterval('P5D'));

$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
$result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date->format('Y-m-d'));
$result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
$ocs->cleanup();

$data = $result->getData();
Expand Down Expand Up @@ -1304,7 +1307,7 @@ public function testCreatePublicLinkExpireDateInvalidFuture() {
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);

try {
$ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date->format('Y-m-d'));
$ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
$this->fail();
} catch (OCSException $e) {
$this->assertEquals(404, $e->getCode());
Expand All @@ -1325,7 +1328,7 @@ public function XtestCreatePublicLinkExpireDateInvalidPast() {
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);

try {
$ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date->format('Y-m-d'));
$ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d'));
$this->fail();
} catch(OCSException $e) {
$this->assertEquals(404, $e->getCode());
Expand Down
Loading