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
17 changes: 11 additions & 6 deletions lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
Expand Down Expand Up @@ -89,16 +90,18 @@ public function createCollectiveShare(int $collectiveId, ?string $password = nul
* @param string $token Token of the share
* @param bool $editable Whether share has edit permissions
* @param string|null $password Optional password for the share
* @param string|null $expirationDate Expiration date (YYYY-MM-DD), empty string to remove it, null to keep it unchanged
*
* @return DataResponse<Http::STATUS_OK, CollectivesCollectiveShare, array{}>
* @throws OCSForbiddenException Not permitted
* @throws OCSNotFoundException Collective, page or share not found
* @throws OCSBadRequestException Invalid expiration date
*
* 200: Share updated
*/
#[NoAdminRequired]
public function updateCollectiveShare(int $collectiveId, string $token, bool $editable, ?string $password = null): DataResponse {
return $this->updatePageShare($collectiveId, 0, $token, $editable, $password);
public function updateCollectiveShare(int $collectiveId, string $token, bool $editable, ?string $password = null, ?string $expirationDate = null): DataResponse {
return $this->updatePageShare($collectiveId, 0, $token, $editable, $password, $expirationDate);
}

/**
Expand Down Expand Up @@ -152,24 +155,26 @@ public function createPageShare(int $collectiveId, int $pageId = 0, ?string $pas
* @param int $pageId ID of the page
* @param string $token Token of the share
* @param bool $editable Whether share has edit permissions
* @param ?string $password Optional password for the share
* @param string|null $password Optional password for the share
* @param string|null $expirationDate Expiration date (YYYY-MM-DD), empty string to remove it, null to keep it unchanged
*
* @return DataResponse<Http::STATUS_OK, CollectivesCollectiveShare, array{}>
* @throws OCSForbiddenException Not permitted
* @throws OCSNotFoundException Collective, page or share not found
* @throws OCSBadRequestException Invalid expiration date
*
* 200: Share updated
*/
#[NoAdminRequired]
public function updatePageShare(int $collectiveId, int $pageId, string $token, bool $editable, ?string $password = null): DataResponse {
$share = $this->handleErrorResponse(function () use ($collectiveId, $pageId, $token, $editable, $password): CollectiveShare {
public function updatePageShare(int $collectiveId, int $pageId, string $token, bool $editable, ?string $password = null, ?string $expirationDate = null): DataResponse {
$share = $this->handleErrorResponse(function () use ($collectiveId, $pageId, $token, $editable, $password, $expirationDate): CollectiveShare {
$uid = $this->getUid();
$collective = $this->collectiveService->getCollective($collectiveId, $uid);
$pageInfo = null;
if ($pageId !== 0) {
$pageInfo = $this->pageService->findByFileId($collectiveId, $pageId, $uid);
}
return $this->shareService->updateShare($uid, $collective, $pageInfo, $token, $editable, $password);
return $this->shareService->updateShare($uid, $collective, $pageInfo, $token, $editable, $password, $expirationDate);
}, $this->logger);
return new DataResponse($share);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Db/CollectiveShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CollectiveShare extends Entity implements JsonSerializable {
/** transient attributes, not persisted in database */
protected bool $editable = false;
protected string $password = '';
protected ?\DateTime $expirationDate = null;

public function getEditable(): bool {
return $this->editable;
Expand All @@ -50,6 +51,14 @@ public function setPassword(string $password): void {
$this->password = $password;
}

public function getExpirationDate(): ?\DateTime {
return $this->expirationDate;
}

public function setExpirationDate(?\DateTime $expirationDate): void {
$this->expirationDate = $expirationDate;
}

public function jsonSerialize(): array {
return [
'id' => $this->id,
Expand All @@ -59,6 +68,7 @@ public function jsonSerialize(): array {
'owner' => $this->owner,
'editable' => $this->editable,
'hasPassword' => (bool)$this->password,
'expirationDate' => $this->expirationDate?->format('Y-m-d'),
];
}
}
1 change: 1 addition & 0 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* owner: string,
* editable: bool,
* hasPassword: bool,
* expirationDate: ?string,
* }
*
* @psalm-type CollectivesPageInfo = array{
Expand Down
80 changes: 56 additions & 24 deletions lib/Service/CollectiveShareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OCP\DB\Exception;
use OCP\Files\Folder;
use OCP\Files\NotFoundException as FilesNotFoundException;
use OCP\IDateTimeZone;
use OCP\IL10N;
use OCP\ISession;
use OCP\Lock\ILockingProvider;
Expand All @@ -42,6 +43,7 @@ public function __construct(
private readonly PageService $pageService,
private readonly IL10N $l10n,
private readonly ISession $session,
private readonly IDateTimeZone $dateTimeZone,
) {
}

Expand Down Expand Up @@ -126,10 +128,7 @@ public function findShare(string $userId, int $collectiveId, int $pageId): ?Coll
return null;
}

$collectiveShare->setEditable($this->isShareEditable($folderShare, $collectiveShare));
if ($folderShare->getPassword()) {
$collectiveShare->setPassword($folderShare->getPassword());
}
$this->setFolderShareAttributes($collectiveShare, $folderShare);
return $collectiveShare;
}

Expand All @@ -152,34 +151,61 @@ public function findShareByToken(string $token, bool $getPermissionInfo = true):
return null;
}

$collectiveShare->setEditable($this->isShareEditable($folderShare, $collectiveShare));
if ($folderShare->getPassword()) {
$collectiveShare->setPassword($folderShare->getPassword());
}
$this->setFolderShareAttributes($collectiveShare, $folderShare);
return $collectiveShare;
}

public function getSharesByCollectiveAndUser(string $userId, int $collectiveId): array {
$collectiveShares = $this->collectiveShareMapper->findByCollectiveIdAndUser($collectiveId, $userId);

$shares = [];
foreach ($collectiveShares as $share) {
foreach ($collectiveShares as $collectiveShare) {
try {
$folderShare = $this->shareManager->getShareByToken($share->getToken());
$folderShare = $this->shareManager->getShareByToken($collectiveShare->getToken());
} catch (ShareNotFound) {
// Corresponding folder share not found, delete the collective share as well.
$this->collectiveShareMapper->delete($share);
}
$share->setEditable($this->isShareEditable($folderShare, $share));
if ($folderShare->getPassword()) {
$share->setPassword($folderShare->getPassword());
$this->collectiveShareMapper->delete($collectiveShare);
continue;
}
$shares[] = $share;
$this->setFolderShareAttributes($collectiveShare, $folderShare);
$shares[] = $collectiveShare;
}

return $shares;
}

private function setFolderShareAttributes(CollectiveShare $collectiveShare, IShare $folderShare): void {
$collectiveShare->setEditable($this->isShareEditable($folderShare, $collectiveShare));
if ($folderShare->getPassword()) {
$collectiveShare->setPassword($folderShare->getPassword());
}
$collectiveShare->setExpirationDate($this->getExpirationDate($folderShare));
}

/**
* Get the expiration date of a folder share in the user's timezone
*
* The share provider returns it in the server's default timezone.
*/
private function getExpirationDate(IShare $folderShare): ?\DateTime {
$expirationDate = $folderShare->getExpirationDate();
if ($expirationDate === null) {
return null;
}
return (clone $expirationDate)->setTimezone($this->dateTimeZone->getTimeZone());
}

/**
* @throws UnprocessableEntityException
*/
private function parseExpirationDate(string $date): \DateTime {
$expirationDate = \DateTime::createFromFormat('!Y-m-d', $date, $this->dateTimeZone->getTimeZone());
if ($expirationDate === false) {
throw new UnprocessableEntityException($this->l10n->t('Invalid date. Format must be YYYY-MM-DD'));
}
return $expirationDate;
}

private function isShareEditable(IShare $folderShare, CollectiveShare $collectiveShare): bool {
if (($folderShare->getPermissions() & Collective::editPermissions) !== Collective::editPermissions) {
return false;
Expand Down Expand Up @@ -213,9 +239,7 @@ public function createShare(string $userId, Collective $collective, ?PageInfo $p
throw new NotPermittedException('Failed to create collective/page share for ' . $collective->getName(), 0, $e);
}

if ($password) {
$collectiveShare->setPassword($folderShare->getPassword());
}
$this->setFolderShareAttributes($collectiveShare, $folderShare);
return $collectiveShare;
}

Expand All @@ -225,8 +249,9 @@ public function createShare(string $userId, Collective $collective, ?PageInfo $p
* @throws MultipleObjectsReturnedException
* @throws NotFoundException
* @throws NotPermittedException
* @throws UnprocessableEntityException
*/
public function updateShare(string $userId, Collective $collective, ?PageInfo $pageInfo, string $token, bool $editable, ?string $password = null): CollectiveShare {
public function updateShare(string $userId, Collective $collective, ?PageInfo $pageInfo, string $token, bool $editable, ?string $password = null, ?string $expirationDate = null): CollectiveShare {
if (!$collective->canShare()) {
throw new NotPermittedException($this->l10n->t('You are not allowed to share %s', $collective->getName()));
}
Expand Down Expand Up @@ -267,12 +292,19 @@ public function updateShare(string $userId, Collective $collective, ?PageInfo $p
}
}

$this->shareManager->updateShare($folderShare);
if ($expirationDate !== null) {
$folderShare->setExpirationDate($expirationDate === '' ? null : $this->parseExpirationDate($expirationDate));
}

$share->setEditable($this->isShareEditable($folderShare, $share));
if ($folderShare->getPassword()) {
$share->setPassword($folderShare->getPassword());
try {
$this->shareManager->updateShare($folderShare);
} catch (GenericShareException $e) {
throw new UnprocessableEntityException($e->getHint(), 0, $e);
} catch (\InvalidArgumentException $e) {
throw new UnprocessableEntityException($e->getMessage(), 0, $e);
}

$this->setFolderShareAttributes($share, $folderShare);
return $share;
}

Expand Down
75 changes: 74 additions & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@
"token",
"owner",
"editable",
"hasPassword"
"hasPassword",
"expirationDate"
],
"properties": {
"id": {
Expand All @@ -154,6 +155,10 @@
},
"hasPassword": {
"type": "boolean"
},
"expirationDate": {
"type": "string",
"nullable": true
}
}
},
Expand Down Expand Up @@ -2807,6 +2812,12 @@
"nullable": true,
"default": null,
"description": "Optional password for the share"
},
"expirationDate": {
"type": "string",
"nullable": true,
"default": null,
"description": "Expiration date (YYYY-MM-DD), empty string to remove it, null to keep it unchanged"
}
}
}
Expand Down Expand Up @@ -2953,6 +2964,34 @@
}
}
},
"400": {
"description": "Invalid expiration date",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
},
"401": {
"description": "Current user is not logged in",
"content": {
Expand Down Expand Up @@ -3200,6 +3239,12 @@
"nullable": true,
"default": null,
"description": "Optional password for the share"
},
"expirationDate": {
"type": "string",
"nullable": true,
"default": null,
"description": "Expiration date (YYYY-MM-DD), empty string to remove it, null to keep it unchanged"
}
}
}
Expand Down Expand Up @@ -3336,6 +3381,34 @@
}
}
},
"400": {
"description": "Invalid expiration date",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
},
"401": {
"description": "Current user is not logged in",
"content": {
Expand Down
Loading
Loading