Skip to content

Commit 09f9a10

Browse files
committed
feat: Add expiry date support for collective shares in backend
Add ability to view and manage expiration dates on collective shares. Shares now inherit and respect Nextcloud's default expiry date policy for file shares. - Add expirationDate field to CollectiveShare entity (transient) - Extract expiry dates from underlying IShare objects in service layer - Add helper methods for expiry configuration (hasDefaultExpireDate, isExpireDateEnforced, getDefaultExpireDays) - Update API endpoints to accept and return expiry date information - Support setting, updating, and removing expiry dates via updateShare - Return both share data and global expiry config in getCollectiveShares Signed-off-by: Silver <s.szmajduch@posteo.de>
1 parent e3257c7 commit 09f9a10

4 files changed

Lines changed: 78 additions & 8 deletions

File tree

lib/Controller/ShareController.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,18 @@ public function __construct(
5858
public function getCollectiveShares(int $collectiveId): DataResponse {
5959
$share = $this->handleErrorResponse(function () use ($collectiveId): array {
6060
$userId = $this->userId;
61-
$collective = $this->collectiveService->getCollective($collectiveId, $userId);
62-
return $this->shareService->getSharesByCollectiveAndUser($userId, $collective->getId());
61+
$this->collectiveService->getCollective($collectiveId, $userId);
62+
$shares = $this->shareService->getSharesByCollectiveAndUser($userId, $collectiveId);
63+
64+
// Get global expiry configuration
65+
return [
66+
'shares' => $shares,
67+
'expiryConfig' => [
68+
'hasDefaultExpireDate' => $this->shareService->hasDefaultExpireDate(),
69+
'isExpireDateEnforced' => $this->shareService->isExpireDateEnforced(),
70+
'defaultExpireDays' => $this->shareService->getDefaultExpireDays(),
71+
],
72+
];
6373
}, $this->logger);
6474
return new DataResponse($share);
6575
}
@@ -96,8 +106,8 @@ public function createCollectiveShare(int $collectiveId, string $password = ''):
96106
* 200: Share updated
97107
*/
98108
#[NoAdminRequired]
99-
public function updateCollectiveShare(int $collectiveId, string $token, bool $editable, string $password = ''): DataResponse {
100-
return $this->updatePageShare($collectiveId, 0, $token, $editable, $password);
109+
public function updateCollectiveShare(int $collectiveId, string $token, bool $editable, string $password = '', ?string $expiryDate = null): DataResponse {
110+
return $this->updatePageShare($collectiveId, 0, $token, $editable, $password, $expiryDate);
101111
}
102112

103113
/**
@@ -159,15 +169,15 @@ public function createPageShare(int $collectiveId, int $pageId = 0, string $pass
159169
* 200: Share updated
160170
*/
161171
#[NoAdminRequired]
162-
public function updatePageShare(int $collectiveId, int $pageId, string $token, bool $editable, ?string $password = null): DataResponse {
163-
$share = $this->handleErrorResponse(function () use ($collectiveId, $pageId, $token, $editable, $password): CollectiveShare {
172+
public function updatePageShare(int $collectiveId, int $pageId, string $token, bool $editable, ?string $password = null, ?string $expiryDate = null): DataResponse {
173+
$share = $this->handleErrorResponse(function () use ($collectiveId, $pageId, $token, $editable, $password, $expiryDate): CollectiveShare {
164174
$userId = $this->userId;
165175
$collective = $this->collectiveService->getCollective($collectiveId, $userId);
166176
$pageInfo = null;
167177
if ($pageId !== 0) {
168178
$pageInfo = $this->pageService->findByFileId($collectiveId, $pageId, $userId);
169179
}
170-
return $this->shareService->updateShare($userId, $collective, $pageInfo, $token, $editable, $password);
180+
return $this->shareService->updateShare($userId, $collective, $pageInfo, $token, $editable, $password, $expiryDate);
171181
}, $this->logger);
172182
return new DataResponse($share);
173183
}

lib/Db/CollectiveShare.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CollectiveShare extends Entity implements JsonSerializable {
3434
/** transient attributes, not persisted in database */
3535
protected bool $editable = false;
3636
protected string $password = '';
37+
protected ?\DateTime $expirationDate = null;
3738

3839
public function getEditable(): bool {
3940
return $this->editable;
@@ -51,6 +52,14 @@ public function setPassword(string $password): void {
5152
$this->password = $password;
5253
}
5354

55+
public function getExpirationDate(): ?\DateTime {
56+
return $this->expirationDate;
57+
}
58+
59+
public function setExpirationDate(?\DateTime $expirationDate): void {
60+
$this->expirationDate = $expirationDate;
61+
}
62+
5463
public function jsonSerialize(): array {
5564
return [
5665
'id' => $this->id,
@@ -60,6 +69,7 @@ public function jsonSerialize(): array {
6069
'owner' => $this->owner,
6170
'editable' => $this->editable,
6271
'password' => $this->password,
72+
'expirationDate' => $this->expirationDate?->format(\DateTime::ATOM),
6373
];
6474
}
6575
}

lib/ResponseDefinitions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* owner: string,
4242
* editable: bool,
4343
* password: string,
44+
* expirationDate: string|null,
4445
* }
4546
*
4647
* @psalm-type CollectivesPageInfo = array{

lib/Service/CollectiveShareService.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ public function findShare(string $userId, int $collectiveId, int $pageId): ?Coll
124124
if ($folderShare->getPassword()) {
125125
$collectiveShare->setPassword($folderShare->getPassword());
126126
}
127+
128+
if ($folderShare->getExpirationDate()) {
129+
$collectiveShare->setExpirationDate($folderShare->getExpirationDate());
130+
} else {
131+
$collectiveShare->setExpirationDate(null);
132+
}
127133
return $collectiveShare;
128134
}
129135

@@ -150,6 +156,12 @@ public function findShareByToken(string $token, bool $getPermissionInfo = true):
150156
if ($folderShare->getPassword()) {
151157
$collectiveShare->setPassword($folderShare->getPassword());
152158
}
159+
160+
if ($folderShare->getExpirationDate()) {
161+
$collectiveShare->setExpirationDate($folderShare->getExpirationDate());
162+
} else {
163+
$collectiveShare->setExpirationDate(null);
164+
}
153165
return $collectiveShare;
154166
}
155167

@@ -168,6 +180,13 @@ public function getSharesByCollectiveAndUser(string $userId, int $collectiveId):
168180
if ($folderShare->getPassword()) {
169181
$share->setPassword($folderShare->getPassword());
170182
}
183+
184+
$expirationDate = $folderShare->getExpirationDate();
185+
if ($expirationDate) {
186+
$share->setExpirationDate($expirationDate);
187+
} else {
188+
$share->setExpirationDate(null);
189+
}
171190
$shares[] = $share;
172191
}
173192

@@ -179,6 +198,18 @@ private function isShareEditable(IShare $folderShare): bool {
179198
return ($folderShare->getPermissions() & Collective::editPermissions) === Collective::editPermissions;
180199
}
181200

201+
public function isExpireDateEnforced(): bool {
202+
return $this->shareManager->shareApiLinkDefaultExpireDateEnforced();
203+
}
204+
205+
public function getDefaultExpireDays(): int {
206+
return $this->shareManager->shareApiLinkDefaultExpireDays();
207+
}
208+
209+
public function hasDefaultExpireDate(): bool {
210+
return $this->shareManager->shareApiLinkDefaultExpireDate();
211+
}
212+
182213
/**
183214
* @throws NotFoundException
184215
* @throws NotPermittedException
@@ -217,7 +248,7 @@ public function createShare(string $userId, Collective $collective, ?PageInfo $p
217248
* @throws NotFoundException
218249
* @throws NotPermittedException
219250
*/
220-
public function updateShare(string $userId, Collective $collective, ?PageInfo $pageInfo, string $token, bool $editable, ?string $password): CollectiveShare {
251+
public function updateShare(string $userId, Collective $collective, ?PageInfo $pageInfo, string $token, bool $editable, ?string $password, ?string $expiryDate = null): CollectiveShare {
221252
if (!$collective->canShare()) {
222253
throw new NotPermittedException($this->l10n->t('You are not allowed to share %s', $collective->getName()));
223254
}
@@ -250,10 +281,28 @@ public function updateShare(string $userId, Collective $collective, ?PageInfo $p
250281
$password = null;
251282
}
252283
$folderShare->setPassword($password);
284+
285+
if ($expiryDate !== null) {
286+
if ($expiryDate === '') {
287+
// Empty string means remove expiration date
288+
$folderShare->setExpirationDate(null);
289+
} else {
290+
$folderShare->setExpirationDate(new \DateTime($expiryDate));
291+
}
292+
}
293+
// If $expiryDate === null, don't change existing expiry date
294+
253295
$this->shareManager->updateShare($folderShare);
254296

255297
$share->setEditable($this->isShareEditable($folderShare));
256298
$share->setPassword($folderShare->getPassword() ?? '');
299+
300+
if ($folderShare->getExpirationDate()) {
301+
$share->setExpirationDate($folderShare->getExpirationDate());
302+
} else {
303+
$share->setExpirationDate(null);
304+
}
305+
257306
return $share;
258307
}
259308

0 commit comments

Comments
 (0)