Skip to content

Commit b90ed9f

Browse files
authored
Merge pull request #43831 from nextcloud/backport/43428/stable26
2 parents f9db8e4 + 5d1ca31 commit b90ed9f

File tree

7 files changed

+115
-43
lines changed

7 files changed

+115
-43
lines changed

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ protected function formatShare(IShare $share, Node $recipientNode = null): array
223223

224224
$expiration = $share->getExpirationDate();
225225
if ($expiration !== null) {
226+
$expiration->setTimezone($this->dateTimeZone->getTimeZone());
226227
$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
227228
}
228229

@@ -1662,12 +1663,14 @@ protected function canDeleteShareFromSelf(\OCP\Share\IShare $share): bool {
16621663
private function parseDate(string $expireDate): \DateTime {
16631664
try {
16641665
$date = new \DateTime(trim($expireDate, "\""), $this->dateTimeZone->getTimeZone());
1666+
// Make sure it expires at midnight in owner timezone
1667+
$date->setTime(0, 0, 0);
16651668
} catch (\Exception $e) {
16661669
throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
16671670
}
16681671

1672+
// Use server timezone to store the date
16691673
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
1670-
$date->setTime(0, 0, 0);
16711674

16721675
return $date;
16731676
}

apps/files_sharing/tests/ApiTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private function createOCS($userId) {
124124
$userStatusManager = $this->createMock(IUserStatusManager::class);
125125
$previewManager = $this->createMock(IPreview::class);
126126
$dateTimeZone = $this->createMock(IDateTimeZone::class);
127+
$dateTimeZone->method('getTimeZone')->willReturn(new \DateTimeZone(date_default_timezone_get()));
127128

128129
return new ShareAPIController(
129130
self::APP_NAME,
@@ -1063,10 +1064,9 @@ public function testUpdateShareExpireDate() {
10631064
$config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
10641065

10651066
$dateWithinRange = new \DateTime();
1066-
$dateWithinRange->setTime(0, 0, 0);
1067-
$dateWithinRange->add(new \DateInterval('P5D'));
1067+
$dateWithinRange->add(new \DateInterval('P6D'));
1068+
10681069
$dateOutOfRange = new \DateTime();
1069-
$dateOutOfRange->setTime(0, 0, 0);
10701070
$dateOutOfRange->add(new \DateInterval('P8D'));
10711071

10721072
// update expire date to a valid value
@@ -1077,6 +1077,8 @@ public function testUpdateShareExpireDate() {
10771077
$share1 = $this->shareManager->getShareById($share1->getFullId());
10781078

10791079
// date should be changed
1080+
$dateWithinRange->setTime(0, 0, 0);
1081+
$dateWithinRange->setTimezone(new \DateTimeZone(date_default_timezone_get()));
10801082
$this->assertEquals($dateWithinRange, $share1->getExpirationDate());
10811083

10821084
// update expire date to a value out of range
@@ -1290,12 +1292,14 @@ public function testShareStorageMountPoint() {
12901292

12911293
public function datesProvider() {
12921294
$date = new \DateTime();
1295+
$date->setTime(0, 0);
12931296
$date->add(new \DateInterval('P5D'));
1297+
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
12941298

12951299
return [
1296-
[$date->format('Y-m-d'), true],
1300+
[$date->format('Y-m-d H:i:s'), true],
12971301
['abc', false],
1298-
[$date->format('Y-m-d') . 'xyz', false],
1302+
[$date->format('Y-m-d H:i:s') . 'xyz', false],
12991303
];
13001304
}
13011305

@@ -1321,15 +1325,15 @@ public function testPublicLinkExpireDate($date, $valid) {
13211325

13221326
$data = $result->getData();
13231327
$this->assertTrue(is_string($data['token']));
1324-
$this->assertEquals($date, substr($data['expiration'], 0, 10));
1328+
$this->assertEquals(substr($date, 0, 10), substr($data['expiration'], 0, 10));
13251329

13261330
// check for correct link
13271331
$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
13281332
$this->assertEquals($url, $data['url']);
13291333

13301334
$share = $this->shareManager->getShareById('ocinternal:'.$data['id']);
13311335

1332-
$this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d'));
1336+
$this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d H:i:s'));
13331337

13341338
$this->shareManager->deleteShare($share);
13351339
}
@@ -1353,7 +1357,7 @@ public function testCreatePublicLinkExpireDateValid() {
13531357

13541358
$data = $result->getData();
13551359
$this->assertTrue(is_string($data['token']));
1356-
$this->assertEquals($date->format('Y-m-d') . ' 00:00:00', $data['expiration']);
1360+
$this->assertEquals($date->format('Y-m-d 00:00:00'), $data['expiration']);
13571361

13581362
// check for correct link
13591363
$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);

apps/files_sharing/tests/CapabilitiesTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\Files\IRootFolder;
3737
use OCP\Files\Mount\IMountManager;
3838
use OCP\IConfig;
39+
use OCP\IDateTimeZone;
3940
use OCP\IGroupManager;
4041
use OCP\IL10N;
4142
use OCP\IURLGenerator;
@@ -98,7 +99,8 @@ private function getResults(array $map) {
9899
$this->createMock(IEventDispatcher::class),
99100
$this->createMock(IUserSession::class),
100101
$this->createMock(KnownUserService::class),
101-
$this->createMock(ShareDisableChecker::class)
102+
$this->createMock(ShareDisableChecker::class),
103+
$this->createMock(IDateTimeZone::class),
102104
);
103105
$cap = new Capabilities($config, $shareManager);
104106
$result = $this->getFilesSharingPart($cap->getCapabilities());

apps/files_sharing/tests/Controller/ShareAPIControllerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ public function testGetShare(\OCP\Share\IShare $share, array $result) {
835835
$this->groupManager->method('get')->willReturnMap([
836836
['group', $group],
837837
]);
838+
$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
838839

839840
$d = $ocs->getShare($share->getId())->getData()[0];
840841

@@ -4603,6 +4604,7 @@ public function testFormatShare(array $expects, \OCP\Share\IShare $share, array
46034604
$this->rootFolder->method('getUserFolder')
46044605
->with($this->currentUser)
46054606
->willReturnSelf();
4607+
$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
46064608

46074609
if (!$exception) {
46084610
$this->rootFolder->method('getById')

lib/private/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,8 @@ public function __construct($webRoot, \OC\Config $config) {
13141314
$c->get(IEventDispatcher::class),
13151315
$c->get(IUserSession::class),
13161316
$c->get(KnownUserService::class),
1317-
$c->get(ShareDisableChecker::class)
1317+
$c->get(ShareDisableChecker::class),
1318+
$c->get(IDateTimeZone::class),
13181319
);
13191320

13201321
return $manager;

lib/private/Share20/Manager.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use OCP\Files\Node;
5555
use OCP\HintException;
5656
use OCP\IConfig;
57+
use OCP\IDateTimeZone;
5758
use OCP\IGroupManager;
5859
use OCP\IL10N;
5960
use OCP\IURLGenerator;
@@ -119,6 +120,7 @@ class Manager implements IManager {
119120
/** @var KnownUserService */
120121
private $knownUserService;
121122
private ShareDisableChecker $shareDisableChecker;
123+
private IDateTimeZone $dateTimeZone;
122124

123125
public function __construct(
124126
LoggerInterface $logger,
@@ -139,7 +141,8 @@ public function __construct(
139141
IEventDispatcher $dispatcher,
140142
IUserSession $userSession,
141143
KnownUserService $knownUserService,
142-
ShareDisableChecker $shareDisableChecker
144+
ShareDisableChecker $shareDisableChecker,
145+
IDateTimeZone $dateTimeZone,
143146
) {
144147
$this->logger = $logger;
145148
$this->config = $config;
@@ -163,6 +166,7 @@ public function __construct(
163166
$this->userSession = $userSession;
164167
$this->knownUserService = $knownUserService;
165168
$this->shareDisableChecker = $shareDisableChecker;
169+
$this->dateTimeZone = $dateTimeZone;
166170
}
167171

168172
/**
@@ -383,10 +387,10 @@ protected function validateExpirationDateInternal(IShare $share) {
383387
$expirationDate = $share->getExpirationDate();
384388

385389
if ($expirationDate !== null) {
386-
//Make sure the expiration date is a date
390+
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
387391
$expirationDate->setTime(0, 0, 0);
388392

389-
$date = new \DateTime();
393+
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
390394
$date->setTime(0, 0, 0);
391395
if ($date >= $expirationDate) {
392396
$message = $this->l->t('Expiration date is in the past');
@@ -414,9 +418,8 @@ protected function validateExpirationDateInternal(IShare $share) {
414418
$isEnforced = $this->shareApiInternalDefaultExpireDateEnforced();
415419
}
416420
if ($fullId === null && $expirationDate === null && $defaultExpireDate) {
417-
$expirationDate = new \DateTime();
421+
$expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone());
418422
$expirationDate->setTime(0, 0, 0);
419-
420423
$days = (int)$this->config->getAppValue('core', $configProp, (string)$defaultExpireDays);
421424
if ($days > $defaultExpireDays) {
422425
$days = $defaultExpireDays;
@@ -430,7 +433,7 @@ protected function validateExpirationDateInternal(IShare $share) {
430433
throw new \InvalidArgumentException('Expiration date is enforced');
431434
}
432435

433-
$date = new \DateTime();
436+
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
434437
$date->setTime(0, 0, 0);
435438
$date->add(new \DateInterval('P' . $defaultExpireDays . 'D'));
436439
if ($date < $expirationDate) {
@@ -470,10 +473,10 @@ protected function validateExpirationDateLink(IShare $share) {
470473
$expirationDate = $share->getExpirationDate();
471474

472475
if ($expirationDate !== null) {
473-
//Make sure the expiration date is a date
476+
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
474477
$expirationDate->setTime(0, 0, 0);
475478

476-
$date = new \DateTime();
479+
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
477480
$date->setTime(0, 0, 0);
478481
if ($date >= $expirationDate) {
479482
$message = $this->l->t('Expiration date is in the past');
@@ -490,7 +493,7 @@ protected function validateExpirationDateLink(IShare $share) {
490493
}
491494

492495
if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
493-
$expirationDate = new \DateTime();
496+
$expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone());
494497
$expirationDate->setTime(0, 0, 0);
495498

496499
$days = (int)$this->config->getAppValue('core', 'link_defaultExpDays', $this->shareApiLinkDefaultExpireDays());
@@ -506,7 +509,7 @@ protected function validateExpirationDateLink(IShare $share) {
506509
throw new \InvalidArgumentException('Expiration date is enforced');
507510
}
508511

509-
$date = new \DateTime();
512+
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
510513
$date->setTime(0, 0, 0);
511514
$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
512515
if ($date < $expirationDate) {
@@ -528,6 +531,9 @@ protected function validateExpirationDateLink(IShare $share) {
528531
throw new \Exception($message);
529532
}
530533

534+
if ($expirationDate instanceof \DateTime) {
535+
$expirationDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
536+
}
531537
$share->setExpirationDate($expirationDate);
532538

533539
return $share;

0 commit comments

Comments
 (0)