Skip to content

Commit ba30695

Browse files
authored
Merge pull request #46097 from nextcloud/artonge/fix/dont_override_expiration_date
fix(files_sharing): Also set the expiration date timezone during validation
2 parents 9b05759 + f5fd6c8 commit ba30695

3 files changed

Lines changed: 29 additions & 23 deletions

File tree

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,9 +1615,6 @@ private function parseDate(string $expireDate): \DateTime {
16151615
throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
16161616
}
16171617

1618-
// Use server timezone to store the date
1619-
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
1620-
16211618
return $date;
16221619
}
16231620

lib/private/Share20/DefaultShareProvider.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,28 @@ public function create(\OCP\Share\IShare $share) {
112112
$qb->insert('share');
113113
$qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
114114

115+
$expirationDate = $share->getExpirationDate();
116+
if ($expirationDate !== null) {
117+
$expirationDate = clone $expirationDate;
118+
$expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
119+
}
120+
115121
if ($share->getShareType() === IShare::TYPE_USER) {
116122
//Set the UID of the user we share with
117123
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
118124
$qb->setValue('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING));
119125

120126
//If an expiration date is set store it
121-
if ($share->getExpirationDate() !== null) {
122-
$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
127+
if ($expirationDate !== null) {
128+
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
123129
}
124130
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
125131
//Set the GID of the group we share with
126132
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
127133

128134
//If an expiration date is set store it
129-
if ($share->getExpirationDate() !== null) {
130-
$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
135+
if ($expirationDate !== null) {
136+
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
131137
}
132138
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
133139
//set label for public link
@@ -143,8 +149,8 @@ public function create(\OCP\Share\IShare $share) {
143149
$qb->setValue('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL));
144150

145151
//If an expiration date is set store it
146-
if ($share->getExpirationDate() !== null) {
147-
$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
152+
if ($expirationDate !== null) {
153+
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
148154
}
149155

150156
if (method_exists($share, 'getParent')) {
@@ -224,6 +230,12 @@ public function update(\OCP\Share\IShare $share) {
224230

225231
$shareAttributes = $this->formatShareAttributes($share->getAttributes());
226232

233+
$expirationDate = $share->getExpirationDate();
234+
if ($expirationDate !== null) {
235+
$expirationDate = clone $expirationDate;
236+
$expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
237+
}
238+
227239
if ($share->getShareType() === IShare::TYPE_USER) {
228240
/*
229241
* We allow updating the recipient on user shares.
@@ -238,7 +250,7 @@ public function update(\OCP\Share\IShare $share) {
238250
->set('attributes', $qb->createNamedParameter($shareAttributes))
239251
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
240252
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
241-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
253+
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
242254
->set('note', $qb->createNamedParameter($share->getNote()))
243255
->set('accepted', $qb->createNamedParameter($share->getStatus()))
244256
->execute();
@@ -252,7 +264,7 @@ public function update(\OCP\Share\IShare $share) {
252264
->set('attributes', $qb->createNamedParameter($shareAttributes))
253265
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
254266
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
255-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
267+
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
256268
->set('note', $qb->createNamedParameter($share->getNote()))
257269
->execute();
258270

@@ -267,7 +279,7 @@ public function update(\OCP\Share\IShare $share) {
267279
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
268280
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
269281
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
270-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
282+
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
271283
->set('note', $qb->createNamedParameter($share->getNote()))
272284
->execute();
273285

@@ -294,7 +306,7 @@ public function update(\OCP\Share\IShare $share) {
294306
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
295307
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
296308
->set('token', $qb->createNamedParameter($share->getToken()))
297-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
309+
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
298310
->set('note', $qb->createNamedParameter($share->getNote()))
299311
->set('label', $qb->createNamedParameter($share->getLabel()))
300312
->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)

lib/private/Share20/Manager.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ protected function validateExpirationDateInternal(IShare $share) {
276276

277277
// If $expirationDate is falsy, noExpirationDate is true and expiration not enforced
278278
// Then skip expiration date validation as null is accepted
279-
if(!($share->getNoExpirationDate() && !$isEnforced)) {
280-
if ($expirationDate != null) {
279+
if(!$share->getNoExpirationDate() || $isEnforced) {
280+
if ($expirationDate !== null) {
281281
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
282282
$expirationDate->setTime(0, 0, 0);
283283

@@ -360,7 +360,7 @@ protected function validateExpirationDateLink(IShare $share) {
360360
if ($expirationDate !== null) {
361361
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
362362
$expirationDate->setTime(0, 0, 0);
363-
363+
364364
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
365365
$date->setTime(0, 0, 0);
366366
if ($date >= $expirationDate) {
@@ -376,24 +376,24 @@ protected function validateExpirationDateLink(IShare $share) {
376376
} catch (\UnexpectedValueException $e) {
377377
// This is a new share
378378
}
379-
379+
380380
if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
381381
$expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone());
382382
$expirationDate->setTime(0, 0, 0);
383-
383+
384384
$days = (int)$this->config->getAppValue('core', 'link_defaultExpDays', (string)$this->shareApiLinkDefaultExpireDays());
385385
if ($days > $this->shareApiLinkDefaultExpireDays()) {
386386
$days = $this->shareApiLinkDefaultExpireDays();
387387
}
388388
$expirationDate->add(new \DateInterval('P' . $days . 'D'));
389389
}
390-
390+
391391
// If we enforce the expiration date check that is does not exceed
392392
if ($isEnforced) {
393393
if (empty($expirationDate)) {
394394
throw new \InvalidArgumentException('Expiration date is enforced');
395395
}
396-
396+
397397
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
398398
$date->setTime(0, 0, 0);
399399
$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
@@ -418,9 +418,6 @@ protected function validateExpirationDateLink(IShare $share) {
418418
throw new \Exception($message);
419419
}
420420

421-
if ($expirationDate instanceof \DateTime) {
422-
$expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
423-
}
424421
$share->setExpirationDate($expirationDate);
425422

426423
return $share;

0 commit comments

Comments
 (0)