Skip to content

Commit cac7062

Browse files
committed
Implement expiration date for federated shares
Add expiration date field in UI. Save expiration date when creating or updating federated share. Read expiration date from DB in federated share provider. Applies to both federated user and group shares. Signed-off-by: Vincent Petry <vincent@nextcloud.com>
1 parent 72e6201 commit cac7062

6 files changed

Lines changed: 50 additions & 24 deletions

File tree

apps/federatedfilesharing/lib/FederatedShareProvider.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public function create(IShare $share) {
173173
$permissions = $share->getPermissions();
174174
$sharedBy = $share->getSharedBy();
175175
$shareType = $share->getShareType();
176+
$expirationDate = $share->getExpirationDate();
176177

177178
if ($shareType === IShare::TYPE_REMOTE_GROUP &&
178179
!$this->isOutgoingServer2serverGroupShareEnabled()
@@ -219,7 +220,7 @@ public function create(IShare $share) {
219220
if ($remoteShare) {
220221
try {
221222
$ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
222-
$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType);
223+
$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType, $expirationDate);
223224
$share->setId($shareId);
224225
[$token, $remoteId] = $this->askOwnerToReShare($shareWith, $share, $shareId);
225226
// remote share was create successfully if we get a valid token as return
@@ -264,7 +265,8 @@ protected function createFederatedShare(IShare $share) {
264265
$share->getShareOwner(),
265266
$share->getPermissions(),
266267
$token,
267-
$share->getShareType()
268+
$share->getShareType(),
269+
$share->getExpirationDate()
268270
);
269271

270272
$failure = false;
@@ -370,9 +372,10 @@ protected function getShareFromExternalShareTable(IShare $share) {
370372
* @param int $permissions
371373
* @param string $token
372374
* @param int $shareType
375+
* @param \DateTime $expirationDate
373376
* @return int
374377
*/
375-
private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType) {
378+
private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType, $expirationDate) {
376379
$qb = $this->dbConnection->getQueryBuilder();
377380
$qb->insert('share')
378381
->setValue('share_type', $qb->createNamedParameter($shareType))
@@ -383,6 +386,7 @@ private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ui
383386
->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
384387
->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
385388
->setValue('permissions', $qb->createNamedParameter($permissions))
389+
->setValue('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
386390
->setValue('token', $qb->createNamedParameter($token))
387391
->setValue('stime', $qb->createNamedParameter(time()));
388392

@@ -412,6 +416,7 @@ public function update(IShare $share) {
412416
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
413417
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
414418
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
419+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
415420
->execute();
416421

417422
// send the updated permission to the owner/initiator, if they are not the same
@@ -910,6 +915,11 @@ private function createShareObject($data) {
910915

911916
$share->setProviderId($this->identifier());
912917

918+
if ($data['expiration'] !== null) {
919+
$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
920+
$share->setExpirationDate($expiration);
921+
}
922+
913923
return $share;
914924
}
915925

apps/files_sharing/js/dist/files_sharing_tab.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/files_sharing/js/dist/files_sharing_tab.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,39 @@ public function createShare(
587587
throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$path->getPath(), $shareType]));
588588
}
589589

590+
if ($shareWith === null) {
591+
throw new OCSNotFoundException($this->l->t('Please specify a valid federated user id'));
592+
}
593+
590594
$share->setSharedWith($shareWith);
591595
$share->setPermissions($permissions);
596+
if ($expireDate !== '') {
597+
try {
598+
$expireDate = $this->parseDate($expireDate);
599+
$share->setExpirationDate($expireDate);
600+
} catch (\Exception $e) {
601+
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
602+
}
603+
}
592604
} elseif ($shareType === IShare::TYPE_REMOTE_GROUP) {
593605
if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) {
594606
throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$path->getPath(), $shareType]));
595607
}
596608

609+
if ($shareWith === null) {
610+
throw new OCSNotFoundException($this->l->t('Please specify a valid federated group id'));
611+
}
612+
597613
$share->setSharedWith($shareWith);
598614
$share->setPermissions($permissions);
615+
if ($expireDate !== '') {
616+
try {
617+
$expireDate = $this->parseDate($expireDate);
618+
$share->setExpirationDate($expireDate);
619+
} catch (\Exception $e) {
620+
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
621+
}
622+
}
599623
} elseif ($shareType === IShare::TYPE_CIRCLE) {
600624
if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
601625
throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));

apps/files_sharing/src/components/SharingEntry.vue

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,14 @@
8484
</ActionCheckbox>
8585

8686
<!-- expiration date -->
87-
<ActionCheckbox
88-
v-if="canHaveExpirationDate"
89-
:checked.sync="hasExpirationDate"
87+
<ActionCheckbox :checked.sync="hasExpirationDate"
9088
:disabled="config.isDefaultInternalExpireDateEnforced || saving"
9189
@uncheck="onExpirationDisable">
9290
{{ config.isDefaultInternalExpireDateEnforced
9391
? t('files_sharing', 'Expiration date enforced')
9492
: t('files_sharing', 'Set expiration date') }}
9593
</ActionCheckbox>
96-
<ActionInput v-if="canHaveExpirationDate && hasExpirationDate"
94+
<ActionInput v-if="hasExpirationDate"
9795
ref="expireDate"
9896
v-tooltip.auto="{
9997
content: errors.expireDate,
@@ -224,16 +222,8 @@ export default {
224222
},
225223
226224
canHaveNote() {
227-
return !this.isRemoteShare
228-
},
229-
230-
canHaveExpirationDate() {
231-
return !this.isRemoteShare
232-
},
233-
234-
isRemoteShare() {
235-
return this.share.type === this.SHARE_TYPES.SHARE_TYPE_REMOTE
236-
|| this.share.type === this.SHARE_TYPES.SHARE_TYPE_REMOTE_GROUP
225+
return this.share.type !== this.SHARE_TYPES.SHARE_TYPE_REMOTE
226+
&& this.share.type !== this.SHARE_TYPES.SHARE_TYPE_REMOTE_GROUP
237227
},
238228
239229
/**

lib/private/Share20/Manager.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -986,15 +986,17 @@ public function updateShare(IShare $share) {
986986
$this->validateExpirationDate($share);
987987
$expirationDateUpdated = true;
988988
}
989-
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
989+
} elseif ($share->getShareType() === IShare::TYPE_LINK || $share->getShareType() === IShare::TYPE_REMOTE) {
990990
$this->linkCreateChecks($share);
991991

992-
$plainTextPassword = $share->getPassword();
992+
if ($share->getShareType() === IShare::TYPE_LINK) {
993+
$plainTextPassword = $share->getPassword();
993994

994-
$this->updateSharePasswordIfNeeded($share, $originalShare);
995+
$this->updateSharePasswordIfNeeded($share, $originalShare);
995996

996-
if (empty($plainTextPassword) && $share->getSendPasswordByTalk()) {
997-
throw new \InvalidArgumentException('Can’t enable sending the password by Talk with an empty password');
997+
if (empty($plainTextPassword) && $share->getSendPasswordByTalk()) {
998+
throw new \InvalidArgumentException('Can’t enable sending the password by Talk with an empty password');
999+
}
9981000
}
9991001

10001002
if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {

0 commit comments

Comments
 (0)