Skip to content

Commit

Permalink
Merge pull request #46097 from nextcloud/artonge/fix/dont_override_ex…
Browse files Browse the repository at this point in the history
…piration_date

fix(files_sharing): Also set the expiration date timezone during validation
  • Loading branch information
artonge authored Jun 26, 2024
2 parents 9b05759 + f5fd6c8 commit ba30695
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
3 changes: 0 additions & 3 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1615,9 +1615,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;
}

Expand Down
32 changes: 22 additions & 10 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,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
Expand All @@ -143,8 +149,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')) {
Expand Down Expand Up @@ -224,6 +230,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.
Expand All @@ -238,7 +250,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();
Expand All @@ -252,7 +264,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();

Expand All @@ -267,7 +279,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();

Expand All @@ -294,7 +306,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)
Expand Down
17 changes: 7 additions & 10 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,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);

Expand Down Expand Up @@ -360,7 +360,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) {
Expand All @@ -376,24 +376,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'));
Expand All @@ -418,9 +418,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;
Expand Down

0 comments on commit ba30695

Please sign in to comment.