From 8680bafc5c791153488dd32108f4a7d0969edb8c Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Mon, 25 Jan 2021 15:26:16 +0100 Subject: [PATCH] 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 --- .../lib/FederatedShareProvider.php | 16 ++++++++++--- .../lib/Controller/ShareAPIController.php | 24 +++++++++++++++++++ .../src/components/SharingEntry.vue | 18 ++++---------- lib/private/Share20/Manager.php | 11 ++++++++- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index 47d0d84fb4c..045bfecd5a2 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -173,6 +173,7 @@ class FederatedShareProvider implements IShareProvider { $permissions = $share->getPermissions(); $sharedBy = $share->getSharedBy(); $shareType = $share->getShareType(); + $expirationDate = $share->getExpirationDate(); if ($shareType === IShare::TYPE_REMOTE_GROUP && !$this->isOutgoingServer2serverGroupShareEnabled() @@ -219,7 +220,7 @@ class FederatedShareProvider implements IShareProvider { if ($remoteShare) { try { $ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']); - $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType); + $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType, $expirationDate); $share->setId($shareId); [$token, $remoteId] = $this->askOwnerToReShare($shareWith, $share, $shareId); // remote share was create successfully if we get a valid token as return @@ -264,7 +265,8 @@ class FederatedShareProvider implements IShareProvider { $share->getShareOwner(), $share->getPermissions(), $token, - $share->getShareType() + $share->getShareType(), + $share->getExpirationDate() ); $failure = false; @@ -370,9 +372,10 @@ class FederatedShareProvider implements IShareProvider { * @param int $permissions * @param string $token * @param int $shareType + * @param \DateTime $expirationDate * @return int */ - private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType) { + private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType, $expirationDate) { $qb = $this->dbConnection->getQueryBuilder(); $qb->insert('share') ->setValue('share_type', $qb->createNamedParameter($shareType)) @@ -383,6 +386,7 @@ class FederatedShareProvider implements IShareProvider { ->setValue('uid_owner', $qb->createNamedParameter($uidOwner)) ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy)) ->setValue('permissions', $qb->createNamedParameter($permissions)) + ->setValue('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE)) ->setValue('token', $qb->createNamedParameter($token)) ->setValue('stime', $qb->createNamedParameter(time())); @@ -412,6 +416,7 @@ class FederatedShareProvider implements IShareProvider { ->set('permissions', $qb->createNamedParameter($share->getPermissions())) ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) ->execute(); // send the updated permission to the owner/initiator, if they are not the same @@ -910,6 +915,11 @@ class FederatedShareProvider implements IShareProvider { $share->setProviderId($this->identifier()); + if ($data['expiration'] !== null) { + $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); + $share->setExpirationDate($expiration); + } + return $share; } diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index 922623aa46f..f3b0467fa14 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -587,15 +587,39 @@ class ShareAPIController extends OCSController { 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])); } + if ($shareWith === null) { + throw new OCSNotFoundException($this->l->t('Please specify a valid federated user id')); + } + $share->setSharedWith($shareWith); $share->setPermissions($permissions); + if ($expireDate !== '') { + try { + $expireDate = $this->parseDate($expireDate); + $share->setExpirationDate($expireDate); + } catch (\Exception $e) { + throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD')); + } + } } elseif ($shareType === IShare::TYPE_REMOTE_GROUP) { if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { 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])); } + if ($shareWith === null) { + throw new OCSNotFoundException($this->l->t('Please specify a valid federated group id')); + } + $share->setSharedWith($shareWith); $share->setPermissions($permissions); + if ($expireDate !== '') { + try { + $expireDate = $this->parseDate($expireDate); + $share->setExpirationDate($expireDate); + } catch (\Exception $e) { + throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD')); + } + } } elseif ($shareType === IShare::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')); diff --git a/apps/files_sharing/src/components/SharingEntry.vue b/apps/files_sharing/src/components/SharingEntry.vue index d31c7ef6e3f..0e6975d9d57 100644 --- a/apps/files_sharing/src/components/SharingEntry.vue +++ b/apps/files_sharing/src/components/SharingEntry.vue @@ -84,16 +84,14 @@ - {{ config.isDefaultInternalExpireDateEnforced ? t('files_sharing', 'Expiration date enforced') : t('files_sharing', 'Set expiration date') }} -