Skip to content

Commit

Permalink
feat(sharing): Display password protected shares
Browse files Browse the repository at this point in the history
Fixes: #505

Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed Apr 23, 2024
1 parent 4ffc783 commit 076ba64
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 89 deletions.
4 changes: 3 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@
['name' => 'start#index', 'url' => '/', 'verb' => 'GET'],

// Vue.js router public route (Vue.js frontend)
['name' => 'publicStart#publicIndex', 'url' => '/p/{token}/{path}', 'verb' => 'GET',
['name' => 'publicStart#showAuthenticate', 'url' => '/p/{token}/authenticate/{redirect}', 'verb' => 'GET'],
['name' => 'publicStart#authenticate', 'url' => '/p/{token}/authenticate/{redirect}', 'verb' => 'POST'],
['name' => 'publicStart#showShare', 'url' => '/p/{token}/{path}', 'verb' => 'GET',
'requirements' => ['path' => '.*'], 'defaults' => ['path' => '']],

// Vue.js router route (Vue.js frontend)
Expand Down
24 changes: 20 additions & 4 deletions lib/Controller/PublicCollectiveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,38 @@
use OCP\AppFramework\PublicShareController;
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

class PublicCollectiveController extends PublicShareController {
use ErrorHelper;

private ?IShare $share = null;

public function __construct(string $AppName,
IRequest $request,
private ShareManager $shareManager,
private CollectiveShareMapper $collectiveShareMapper,
private CollectiveService $service,
ISession $session,
private LoggerInterface $logger) {
parent::__construct($AppName, $request, $session);
}

/**
* @throws ShareNotFound
*/
protected function getShare(): IShare {
if ($this->share === null) {
$this->share = $this->shareManager->getShareByToken($this->getToken());
}
return $this->share;
}

protected function getPasswordHash(): string {
return '';
return $this->getShare()->getPassword();
}

public function isValidToken(): bool {
Expand All @@ -45,7 +61,7 @@ public function isValidToken(): bool {
}

protected function isPasswordProtected(): bool {
return false;
return $this->getShare()->getPassword() !== null;
}

private function prepareResponse(Closure $callback) : DataResponse {
Expand All @@ -55,8 +71,8 @@ private function prepareResponse(Closure $callback) : DataResponse {
/**
* @PublicPage
*/
public function get(int $pageId = 0): DataResponse {
return $this->prepareResponse(function () use ($pageId): array {
public function get(): DataResponse {
return $this->prepareResponse(function (): array {
try {
$share = $this->collectiveShareMapper->findOneByToken($this->getToken());
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
Expand Down
113 changes: 64 additions & 49 deletions lib/Controller/PublicPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
use OCP\AppFramework\PublicShareController;
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

class PublicPageController extends PublicShareController {
private ?CollectiveShare $share = null;
private ?IShare $share = null;
private ?CollectiveShare $collectiveShare = null;

use ErrorHelper;

public function __construct(string $appName,
IRequest $request,
private ShareManager $shareManager,
private CollectiveShareMapper $collectiveShareMapper,
private CollectiveShareService $collectiveShareService,
private PageService $service,
Expand All @@ -36,8 +41,33 @@ public function __construct(string $appName,
parent::__construct($appName, $request, $session);
}

/**
* @throws ShareNotFound
*/
protected function getShare(): IShare {
if ($this->share === null) {
$this->share = $this->shareManager->getShareByToken($this->getToken());
}
return $this->share;
}

/**
* @throws NotFoundException
*/
private function getCollectiveShare(): CollectiveShare {
if ($this->collectiveShare === null) {
$this->collectiveShare = $this->collectiveShareService->findShareByToken($this->getToken());

if ($this->collectiveShare === null) {
throw new NotFoundException('Failed to get shared collective');
}
}

return $this->collectiveShare;
}

protected function getPasswordHash(): string {
return '';
return $this->getShare()->getPassword();
}

public function isValidToken(): bool {
Expand All @@ -51,30 +81,15 @@ public function isValidToken(): bool {
}

protected function isPasswordProtected(): bool {
return false;
}

/**
* @throws NotFoundException
*/
private function getShare(): CollectiveShare {
if ($this->share === null) {
$this->share = $this->collectiveShareService->findShareByToken($this->getToken());

if ($this->share === null) {
throw new NotFoundException('Failed to get shared collective');
}
}

return $this->share;
return $this->getShare()->getPassword() !== null;
}

/**
* @throws NotFoundException
* @throws NotPermittedException
*/
private function checkEditPermissions(): void {
if (!$this->getShare()->getEditable()) {
if (!$this->getCollectiveShare()->getEditable()) {
throw new NotPermittedException('Not permitted to edit shared collective');
}
}
Expand Down Expand Up @@ -110,9 +125,9 @@ private function decoratePageInfo(int $collectiveId, int $sharePageId, string $o
*/
public function index(): DataResponse {
return $this->handleErrorResponse(function (): array {
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
$sharePageId = $this->getShare()->getPageId();
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
$sharePageId = $this->getCollectiveShare()->getPageId();
if ($sharePageId === 0) {
$pageInfos = $this->service->findAll($collectiveId, $owner);
} else {
Expand All @@ -132,9 +147,9 @@ public function index(): DataResponse {
*/
public function get(int $id): DataResponse {
return $this->handleErrorResponse(function () use ($id): array {
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
}
$pageInfo = $this->service->find($collectiveId, $id, $owner);
Expand All @@ -151,9 +166,9 @@ public function get(int $id): DataResponse {
public function create(int $parentId, string $title): DataResponse {
return $this->handleErrorResponse(function () use ($parentId, $title): array {
$this->checkEditPermissions();
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $parentId, $owner);
}
$pageInfo = $this->service->create($collectiveId, $parentId, $title, $owner);
Expand All @@ -170,9 +185,9 @@ public function create(int $parentId, string $title): DataResponse {
public function touch(int $id): DataResponse {
return $this->handleErrorResponse(function () use ($id): array {
$this->checkEditPermissions();
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
}
$pageInfo = $this->service->touch($collectiveId, $id, $owner);
Expand All @@ -189,9 +204,9 @@ public function touch(int $id): DataResponse {
public function moveOrCopy(int $id, ?int $parentId, ?string $title = null, ?int $index = 0, bool $copy = false): DataResponse {
return $this->handleErrorResponse(function () use ($id, $parentId, $title, $index, $copy): array {
$this->checkEditPermissions();
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
if ($parentId) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $parentId, $owner);
Expand All @@ -213,9 +228,9 @@ public function moveOrCopy(int $id, ?int $parentId, ?string $title = null, ?int
public function setEmoji(int $id, ?string $emoji = null): DataResponse {
return $this->handleErrorResponse(function () use ($id, $emoji): array {
$this->checkEditPermissions();
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
}
$pageInfo = $this->service->setEmoji($collectiveId, $id, $emoji, $owner);
Expand All @@ -232,9 +247,9 @@ public function setEmoji(int $id, ?string $emoji = null): DataResponse {
public function setSubpageOrder(int $id, ?string $subpageOrder = null): DataResponse {
return $this->handleErrorResponse(function () use ($id, $subpageOrder): array {
$this->checkEditPermissions();
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
}
$pageInfo = $this->service->setSubpageOrder($collectiveId, $id, $subpageOrder, $owner);
Expand All @@ -251,11 +266,11 @@ public function setSubpageOrder(int $id, ?string $subpageOrder = null): DataResp
public function trash(int $id): DataResponse {
return $this->handleErrorResponse(function () use ($id): array {
$this->checkEditPermissions();
if ($this->getShare()->getPageId()) {
if ($this->getCollectiveShare()->getPageId()) {
throw new NotPermittedException('Not permitted to trash page from page share');
}
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
$pageInfo = $this->service->trash($collectiveId, $id, $owner);
$this->decoratePageInfo($collectiveId, 0, $owner, $pageInfo);
return [
Expand All @@ -269,9 +284,9 @@ public function trash(int $id): DataResponse {
*/
public function getAttachments(int $id): DataResponse {
return $this->handleErrorResponse(function () use ($id): array {
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
}
$attachments = $this->attachmentService->getAttachments($collectiveId, $id, $owner);
Expand All @@ -286,9 +301,9 @@ public function getAttachments(int $id): DataResponse {
*/
public function getBacklinks(int $id): DataResponse {
return $this->handleErrorResponse(function () use ($id): array {
$owner = $this->getShare()->getOwner();
$collectiveId = $this->getShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getShare()->getPageId()) {
$owner = $this->getCollectiveShare()->getOwner();
$collectiveId = $this->getCollectiveShare()->getCollectiveId();
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
}
$backlinks = $this->service->getBacklinks($collectiveId, $id, $owner);
Expand Down
Loading

0 comments on commit 076ba64

Please sign in to comment.