Skip to content

Commit de66363

Browse files
authored
Merge pull request #43832 from nextcloud/backport/43428/stable27
2 parents 37f858a + 1b30da1 commit de66363

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
@@ -224,6 +224,7 @@ protected function formatShare(IShare $share, Node $recipientNode = null): array
224224

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

@@ -1668,12 +1669,14 @@ protected function canDeleteShareFromSelf(\OCP\Share\IShare $share): bool {
16681669
private function parseDate(string $expireDate): \DateTime {
16691670
try {
16701671
$date = new \DateTime(trim($expireDate, "\""), $this->dateTimeZone->getTimeZone());
1672+
// Make sure it expires at midnight in owner timezone
1673+
$date->setTime(0, 0, 0);
16711674
} catch (\Exception $e) {
16721675
throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
16731676
}
16741677

1678+
// Use server timezone to store the date
16751679
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
1676-
$date->setTime(0, 0, 0);
16771680

16781681
return $date;
16791682
}

apps/files_sharing/tests/ApiTest.php

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

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

10661067
$dateWithinRange = new \DateTime();
1067-
$dateWithinRange->setTime(0, 0, 0);
1068-
$dateWithinRange->add(new \DateInterval('P5D'));
1068+
$dateWithinRange->add(new \DateInterval('P6D'));
1069+
10691070
$dateOutOfRange = new \DateTime();
1070-
$dateOutOfRange->setTime(0, 0, 0);
10711071
$dateOutOfRange->add(new \DateInterval('P8D'));
10721072

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

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

10831085
// update expire date to a value out of range
@@ -1291,12 +1293,14 @@ public function testShareStorageMountPoint() {
12911293

12921294
public function datesProvider() {
12931295
$date = new \DateTime();
1296+
$date->setTime(0, 0);
12941297
$date->add(new \DateInterval('P5D'));
1298+
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
12951299

12961300
return [
1297-
[$date->format('Y-m-d'), true],
1301+
[$date->format('Y-m-d H:i:s'), true],
12981302
['abc', false],
1299-
[$date->format('Y-m-d') . 'xyz', false],
1303+
[$date->format('Y-m-d H:i:s') . 'xyz', false],
13001304
];
13011305
}
13021306

@@ -1322,15 +1326,15 @@ public function testPublicLinkExpireDate($date, $valid) {
13221326

13231327
$data = $result->getData();
13241328
$this->assertTrue(is_string($data['token']));
1325-
$this->assertEquals($date, substr($data['expiration'], 0, 10));
1329+
$this->assertEquals(substr($date, 0, 10), substr($data['expiration'], 0, 10));
13261330

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

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

1333-
$this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d'));
1337+
$this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d H:i:s'));
13341338

13351339
$this->shareManager->deleteShare($share);
13361340
}
@@ -1354,7 +1358,7 @@ public function testCreatePublicLinkExpireDateValid() {
13541358

13551359
$data = $result->getData();
13561360
$this->assertTrue(is_string($data['token']));
1357-
$this->assertEquals($date->format('Y-m-d') . ' 00:00:00', $data['expiration']);
1361+
$this->assertEquals($date->format('Y-m-d 00:00:00'), $data['expiration']);
13581362

13591363
// check for correct link
13601364
$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
@@ -837,6 +837,7 @@ public function testGetShare(\OCP\Share\IShare $share, array $result) {
837837
$this->groupManager->method('get')->willReturnMap([
838838
['group', $group],
839839
]);
840+
$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
840841

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

@@ -4605,6 +4606,7 @@ public function testFormatShare(array $expects, \OCP\Share\IShare $share, array
46054606
$this->rootFolder->method('getUserFolder')
46064607
->with($this->currentUser)
46074608
->willReturnSelf();
4609+
$this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC'));
46084610

46094611
if (!$exception) {
46104612
$this->rootFolder->method('getById')

lib/private/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,8 @@ public function __construct($webRoot, \OC\Config $config) {
13231323
$c->get(IEventDispatcher::class),
13241324
$c->get(IUserSession::class),
13251325
$c->get(KnownUserService::class),
1326-
$c->get(ShareDisableChecker::class)
1326+
$c->get(ShareDisableChecker::class),
1327+
$c->get(IDateTimeZone::class),
13271328
);
13281329

13291330
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', (string)$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)