Skip to content

Commit ab2f770

Browse files
authored
Merge pull request #42786 from nextcloud/backport/42660/stable28
[stable28] fix(share): use user timezone to parse share expiration date
2 parents 8733579 + 596f0c1 commit ab2f770

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
use OCP\Files\Node;
7171
use OCP\Files\NotFoundException;
7272
use OCP\IConfig;
73+
use OCP\IDateTimeZone;
7374
use OCP\IGroupManager;
7475
use OCP\IL10N;
7576
use OCP\IPreview;
@@ -124,20 +125,6 @@ class ShareAPIController extends OCSController {
124125

125126
/**
126127
* Share20OCS constructor.
127-
*
128-
* @param string $appName
129-
* @param IRequest $request
130-
* @param IManager $shareManager
131-
* @param IGroupManager $groupManager
132-
* @param IUserManager $userManager
133-
* @param IRootFolder $rootFolder
134-
* @param IURLGenerator $urlGenerator
135-
* @param string $userId
136-
* @param IL10N $l10n
137-
* @param IConfig $config
138-
* @param IAppManager $appManager
139-
* @param IServerContainer $serverContainer
140-
* @param IUserStatusManager $userStatusManager
141128
*/
142129
public function __construct(
143130
string $appName,
@@ -153,7 +140,8 @@ public function __construct(
153140
IAppManager $appManager,
154141
IServerContainer $serverContainer,
155142
IUserStatusManager $userStatusManager,
156-
IPreview $previewManager
143+
IPreview $previewManager,
144+
private IDateTimeZone $dateTimeZone,
157145
) {
158146
parent::__construct($appName, $request);
159147

@@ -597,7 +585,7 @@ public function deleteShare(string $id): DataResponse {
597585
* @param string $publicUpload If public uploading is allowed
598586
* @param string $password Password for the share
599587
* @param string|null $sendPasswordByTalk Send the password for the share over Talk
600-
* @param string $expireDate Expiry date of the share
588+
* @param string $expireDate Expiry date of the share using user timezone at 00:00. It means date in UTC timezone will be used.
601589
* @param string $note Note for the share
602590
* @param string $label Label for the share (only used in link and email)
603591
* @param string|null $attributes Additional attributes for the share
@@ -1706,11 +1694,12 @@ protected function canDeleteShareFromSelf(\OCP\Share\IShare $share): bool {
17061694
*/
17071695
private function parseDate(string $expireDate): \DateTime {
17081696
try {
1709-
$date = new \DateTime(trim($expireDate, "\""));
1697+
$date = new \DateTime(trim($expireDate, "\""), $this->dateTimeZone->getTimeZone());
17101698
} catch (\Exception $e) {
17111699
throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
17121700
}
17131701

1702+
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
17141703
$date->setTime(0, 0, 0);
17151704

17161705
return $date;

apps/files_sharing/openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@
18951895
{
18961896
"name": "expireDate",
18971897
"in": "query",
1898-
"description": "Expiry date of the share",
1898+
"description": "Expiry date of the share using user timezone at 00:00. It means date in UTC timezone will be used.",
18991899
"schema": {
19001900
"type": "string",
19011901
"default": ""

apps/files_sharing/tests/ApiTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCP\AppFramework\OCS\OCSForbiddenException;
4545
use OCP\AppFramework\OCS\OCSNotFoundException;
4646
use OCP\IConfig;
47+
use OCP\IDateTimeZone;
4748
use OCP\IL10N;
4849
use OCP\IPreview;
4950
use OCP\IRequest;
@@ -122,6 +123,7 @@ private function createOCS($userId) {
122123
$serverContainer = $this->createMock(IServerContainer::class);
123124
$userStatusManager = $this->createMock(IUserStatusManager::class);
124125
$previewManager = $this->createMock(IPreview::class);
126+
$dateTimeZone = $this->createMock(IDateTimeZone::class);
125127

126128
return new ShareAPIController(
127129
self::APP_NAME,
@@ -137,7 +139,8 @@ private function createOCS($userId) {
137139
$appManager,
138140
$serverContainer,
139141
$userStatusManager,
140-
$previewManager
142+
$previewManager,
143+
$dateTimeZone,
141144
);
142145
}
143146

apps/files_sharing/tests/Controller/ShareAPIControllerTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use OCP\Files\NotFoundException;
4848
use OCP\Files\Storage;
4949
use OCP\IConfig;
50+
use OCP\IDateTimeZone;
5051
use OCP\IGroup;
5152
use OCP\IGroupManager;
5253
use OCP\IL10N;
@@ -117,6 +118,9 @@ class ShareAPIControllerTest extends TestCase {
117118
/** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
118119
private $previewManager;
119120

121+
/** @var IDateTimeZone|\PHPUnit\Framework\MockObject\MockObject */
122+
private $dateTimeZone;
123+
120124
protected function setUp(): void {
121125
$this->shareManager = $this->createMock(IManager::class);
122126
$this->shareManager
@@ -147,6 +151,7 @@ protected function setUp(): void {
147151
->willReturnCallback(function ($fileInfo) {
148152
return $fileInfo->getMimeType() === 'mimeWithPreview';
149153
});
154+
$this->dateTimeZone = $this->createMock(IDateTimeZone::class);
150155

151156
$this->ocs = new ShareAPIController(
152157
$this->appName,
@@ -162,7 +167,8 @@ protected function setUp(): void {
162167
$this->appManager,
163168
$this->serverContainer,
164169
$this->userStatusManager,
165-
$this->previewManager
170+
$this->previewManager,
171+
$this->dateTimeZone,
166172
);
167173
}
168174

@@ -186,6 +192,7 @@ private function mockFormatShare() {
186192
$this->serverContainer,
187193
$this->userStatusManager,
188194
$this->previewManager,
195+
$this->dateTimeZone,
189196
])->setMethods(['formatShare'])
190197
->getMock();
191198
}
@@ -783,6 +790,7 @@ public function testGetShare(\OCP\Share\IShare $share, array $result) {
783790
$this->serverContainer,
784791
$this->userStatusManager,
785792
$this->previewManager,
793+
$this->dateTimeZone,
786794
])->setMethods(['canAccessShare'])
787795
->getMock();
788796

@@ -1407,6 +1415,7 @@ public function testGetShares(array $getSharesParameters, array $shares, array $
14071415
$this->serverContainer,
14081416
$this->userStatusManager,
14091417
$this->previewManager,
1418+
$this->dateTimeZone,
14101419
])->setMethods(['formatShare'])
14111420
->getMock();
14121421

@@ -1746,6 +1755,7 @@ public function testCreateShareUser() {
17461755
$this->serverContainer,
17471756
$this->userStatusManager,
17481757
$this->previewManager,
1758+
$this->dateTimeZone,
17491759
])->setMethods(['formatShare'])
17501760
->getMock();
17511761

@@ -1840,6 +1850,7 @@ public function testCreateShareGroup() {
18401850
$this->serverContainer,
18411851
$this->userStatusManager,
18421852
$this->previewManager,
1853+
$this->dateTimeZone,
18431854
])->setMethods(['formatShare'])
18441855
->getMock();
18451856

@@ -2249,6 +2260,7 @@ public function testCreateShareRemote() {
22492260
$this->serverContainer,
22502261
$this->userStatusManager,
22512262
$this->previewManager,
2263+
$this->dateTimeZone,
22522264
])->setMethods(['formatShare'])
22532265
->getMock();
22542266

@@ -2315,6 +2327,7 @@ public function testCreateShareRemoteGroup() {
23152327
$this->serverContainer,
23162328
$this->userStatusManager,
23172329
$this->previewManager,
2330+
$this->dateTimeZone,
23182331
])->setMethods(['formatShare'])
23192332
->getMock();
23202333

@@ -2554,6 +2567,7 @@ public function testCreateReshareOfFederatedMountNoDeletePermissions() {
25542567
$this->serverContainer,
25552568
$this->userStatusManager,
25562569
$this->previewManager,
2570+
$this->dateTimeZone,
25572571
])->setMethods(['formatShare'])
25582572
->getMock();
25592573

0 commit comments

Comments
 (0)