From d7093ed24b8efc2512e4f73d485c05340d88d789 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Wed, 26 Jun 2024 17:18:47 +0200 Subject: [PATCH] fix(files_sharing): Store the expiration date relative to the server's timezone This is needed as we want to store the difference between the server's and the user's timezones. Signed-off-by: Louis Chemineau --- .../lib/Controller/ShareAPIController.php | 3 -- lib/private/Share20/DefaultShareProvider.php | 32 +++++++++++++------ lib/private/Share20/Manager.php | 19 +++++------ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index cab989aeb4330..bd95c41283350 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -1656,9 +1656,6 @@ private function parseDate(string $expireDate): \DateTime { throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); } - // Use server timezone to store the date - $date->setTimezone(new \DateTimeZone(date_default_timezone_get())); - return $date; } diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php index 55ac3eda644e1..fd22095b420eb 100644 --- a/lib/private/Share20/DefaultShareProvider.php +++ b/lib/private/Share20/DefaultShareProvider.php @@ -137,22 +137,28 @@ public function create(\OCP\Share\IShare $share) { $qb->insert('share'); $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); + $expirationDate = $share->getExpirationDate(); + if ($expirationDate !== null) { + $expirationDate = clone $expirationDate; + $expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get())); + } + if ($share->getShareType() === IShare::TYPE_USER) { //Set the UID of the user we share with $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); $qb->setValue('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING)); //If an expiration date is set store it - if ($share->getExpirationDate() !== null) { - $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); + if ($expirationDate !== null) { + $qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime')); } } elseif ($share->getShareType() === IShare::TYPE_GROUP) { //Set the GID of the group we share with $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); //If an expiration date is set store it - if ($share->getExpirationDate() !== null) { - $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); + if ($expirationDate !== null) { + $qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime')); } } elseif ($share->getShareType() === IShare::TYPE_LINK) { //set label for public link @@ -168,8 +174,8 @@ public function create(\OCP\Share\IShare $share) { $qb->setValue('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL)); //If an expiration date is set store it - if ($share->getExpirationDate() !== null) { - $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); + if ($expirationDate !== null) { + $qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime')); } if (method_exists($share, 'getParent')) { @@ -249,6 +255,12 @@ public function update(\OCP\Share\IShare $share) { $shareAttributes = $this->formatShareAttributes($share->getAttributes()); + $expirationDate = $share->getExpirationDate(); + if ($expirationDate !== null) { + $expirationDate = clone $expirationDate; + $expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get())); + } + if ($share->getShareType() === IShare::TYPE_USER) { /* * We allow updating the recipient on user shares. @@ -263,7 +275,7 @@ public function update(\OCP\Share\IShare $share) { ->set('attributes', $qb->createNamedParameter($shareAttributes)) ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) + ->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE)) ->set('note', $qb->createNamedParameter($share->getNote())) ->set('accepted', $qb->createNamedParameter($share->getStatus())) ->execute(); @@ -277,7 +289,7 @@ public function update(\OCP\Share\IShare $share) { ->set('attributes', $qb->createNamedParameter($shareAttributes)) ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) + ->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE)) ->set('note', $qb->createNamedParameter($share->getNote())) ->execute(); @@ -292,7 +304,7 @@ public function update(\OCP\Share\IShare $share) { ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) + ->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE)) ->set('note', $qb->createNamedParameter($share->getNote())) ->execute(); @@ -319,7 +331,7 @@ public function update(\OCP\Share\IShare $share) { ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) ->set('token', $qb->createNamedParameter($share->getToken())) - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) + ->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE)) ->set('note', $qb->createNamedParameter($share->getNote())) ->set('label', $qb->createNamedParameter($share->getLabel())) ->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT) diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index b797b0b56988c..af31226cf08e7 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -310,8 +310,8 @@ protected function validateExpirationDateInternal(IShare $share) { // If $expirationDate is falsy, noExpirationDate is true and expiration not enforced // Then skip expiration date validation as null is accepted - if (!($share->getNoExpirationDate() && !$isEnforced)) { - if ($expirationDate != null) { + if (!$share->getNoExpirationDate() || $isEnforced) { + if ($expirationDate !== null) { $expirationDate->setTimezone($this->dateTimeZone->getTimeZone()); $expirationDate->setTime(0, 0, 0); @@ -394,7 +394,7 @@ protected function validateExpirationDateLink(IShare $share) { if ($expirationDate !== null) { $expirationDate->setTimezone($this->dateTimeZone->getTimeZone()); $expirationDate->setTime(0, 0, 0); - + $date = new \DateTime('now', $this->dateTimeZone->getTimeZone()); $date->setTime(0, 0, 0); if ($date >= $expirationDate) { @@ -410,24 +410,24 @@ protected function validateExpirationDateLink(IShare $share) { } catch (\UnexpectedValueException $e) { // This is a new share } - + if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { $expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone()); $expirationDate->setTime(0, 0, 0); - + $days = (int)$this->config->getAppValue('core', 'link_defaultExpDays', (string)$this->shareApiLinkDefaultExpireDays()); if ($days > $this->shareApiLinkDefaultExpireDays()) { $days = $this->shareApiLinkDefaultExpireDays(); } $expirationDate->add(new \DateInterval('P' . $days . 'D')); } - + // If we enforce the expiration date check that is does not exceed if ($isEnforced) { if (empty($expirationDate)) { throw new \InvalidArgumentException('Expiration date is enforced'); } - + $date = new \DateTime('now', $this->dateTimeZone->getTimeZone()); $date->setTime(0, 0, 0); $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); @@ -452,9 +452,6 @@ protected function validateExpirationDateLink(IShare $share) { throw new \Exception($message); } - if ($expirationDate instanceof \DateTime) { - $expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get())); - } $share->setExpirationDate($expirationDate); return $share; @@ -812,7 +809,7 @@ protected function sendMailNotification(IL10N $l, $link, $initiator, $shareWith, - \DateTime $expiration = null, + ?\DateTime $expiration = null, $note = '') { $initiatorUser = $this->userManager->get($initiator); $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;