Skip to content

Commit 66cc7b7

Browse files
Merge pull request #54408 from nextcloud/feat/noid/preset-on-share-link-expire-date
feat(preset): share link expiration date
2 parents 8e89cc5 + 6076b67 commit 66cc7b7

File tree

8 files changed

+100
-36
lines changed

8 files changed

+100
-36
lines changed

apps/files_sharing/tests/CapabilitiesTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,16 @@ public function testLinkExpireDate(): void {
195195
$map = [
196196
['core', 'shareapi_enabled', 'yes', 'yes'],
197197
['core', 'shareapi_allow_links', 'yes', 'yes'],
198-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
199198
['core', 'shareapi_expire_after_n_days', '7', '7'],
200-
['core', 'shareapi_enforce_expire_date', 'no', 'no'],
201199
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
202200
];
203-
$result = $this->getResults($map);
201+
202+
$typedMap = [
203+
['core', 'shareapi_default_expire_date', true],
204+
['core', 'shareapi_enforce_expire_date', false],
205+
];
206+
207+
$result = $this->getResults($map, $typedMap);
204208
$this->assertArrayHasKey('expire_date', $result['public']);
205209
$this->assertIsArray($result['public']['expire_date']);
206210
$this->assertTrue($result['public']['expire_date']['enabled']);
@@ -212,11 +216,15 @@ public function testLinkExpireDateEnforced(): void {
212216
$map = [
213217
['core', 'shareapi_enabled', 'yes', 'yes'],
214218
['core', 'shareapi_allow_links', 'yes', 'yes'],
215-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
216-
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
217219
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
218220
];
219-
$result = $this->getResults($map);
221+
222+
$typedMap = [
223+
['core', 'shareapi_default_expire_date', true],
224+
['core', 'shareapi_enforce_expire_date', true],
225+
];
226+
227+
$result = $this->getResults($map, $typedMap);
220228
$this->assertArrayHasKey('expire_date', $result['public']);
221229
$this->assertIsArray($result['public']['expire_date']);
222230
$this->assertTrue($result['public']['expire_date']['enforced']);

apps/settings/lib/Settings/Admin/Sharing.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function getForm() {
6262
'enforceLinksPasswordExcludedGroupsEnabled' => $this->config->getSystemValueBool('sharing.allow_disabled_password_enforcement_groups', false),
6363
'onlyShareWithGroupMembers' => $this->shareManager->shareWithGroupMembersOnly(),
6464
'onlyShareWithGroupMembersExcludeGroupList' => json_decode($onlyShareWithGroupMembersExcludeGroupList) ?? [],
65-
'defaultExpireDate' => $this->getHumanBooleanConfig('core', 'shareapi_default_expire_date'),
65+
'defaultExpireDate' => $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_DEFAULT),
6666
'expireAfterNDays' => $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'),
67-
'enforceExpireDate' => $this->getHumanBooleanConfig('core', 'shareapi_enforce_expire_date'),
67+
'enforceExpireDate' => $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_ENFORCED),
6868
'excludeGroups' => $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no'),
6969
'excludeGroupsList' => json_decode($excludedGroups, true) ?? [],
7070
'publicShareDisclaimerText' => $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext'),

apps/settings/tests/Settings/Admin/SharingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function testGetFormWithoutExcludedGroups(): void {
5959
->willReturnMap([
6060
['core', 'shareapi_allow_federation_on_public_shares', true],
6161
['core', 'shareapi_enable_link_password_by_default', true],
62+
['core', 'shareapi_default_expire_date', false],
63+
['core', 'shareapi_enforce_expire_date', false],
6264
]);
6365

6466
$this->config
@@ -78,9 +80,7 @@ public function testGetFormWithoutExcludedGroups(): void {
7880
['core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes', 'yes'],
7981
['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no', 'no'],
8082
['core', 'shareapi_enabled', 'yes', 'yes'],
81-
['core', 'shareapi_default_expire_date', 'no', 'no'],
8283
['core', 'shareapi_expire_after_n_days', '7', '7'],
83-
['core', 'shareapi_enforce_expire_date', 'no', 'no'],
8484
['core', 'shareapi_exclude_groups', 'no', 'no'],
8585
['core', 'shareapi_public_link_disclaimertext', '', 'Lorem ipsum'],
8686
['core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL, Constants::PERMISSION_ALL],

core/AppInfo/ConfigLexicon.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class ConfigLexicon implements ILexicon {
2424
public const SHARE_CUSTOM_TOKEN = 'shareapi_allow_custom_tokens';
2525
public const SHARE_LINK_PASSWORD_DEFAULT = 'shareapi_enable_link_password_by_default';
2626
public const SHARE_LINK_PASSWORD_ENFORCED = 'shareapi_enforce_links_password';
27-
27+
public const SHARE_LINK_EXPIRE_DATE_DEFAULT = 'shareapi_default_expire_date';
28+
public const SHARE_LINK_EXPIRE_DATE_ENFORCED = 'shareapi_enforce_expire_date';
2829
public const USER_LANGUAGE = 'lang';
2930
public const USER_LOCALE = 'locale';
3031
public const USER_TIMEZONE = 'timezone';
@@ -65,6 +66,24 @@ public function getAppConfigs(): array {
6566
},
6667
definition: 'Enforce password protection when sharing document'
6768
),
69+
new Entry(
70+
key: self::SHARE_LINK_EXPIRE_DATE_DEFAULT,
71+
type: ValueType::BOOL,
72+
defaultRaw: fn (Preset $p): bool => match ($p) {
73+
Preset::SHARED, Preset::SMALL, Preset::MEDIUM, Preset::LARGE => true,
74+
default => false,
75+
},
76+
definition: 'Set default expiration date for shares via link or mail'
77+
),
78+
new Entry(
79+
key: self::SHARE_LINK_EXPIRE_DATE_ENFORCED,
80+
type: ValueType::BOOL,
81+
defaultRaw: fn (Preset $p): bool => match ($p) {
82+
Preset::SHARED, Preset::SMALL, Preset::MEDIUM, Preset::LARGE => true,
83+
default => false,
84+
},
85+
definition: 'Enforce expiration date for shares via link or mail'
86+
),
6887
new Entry(self::LASTCRON_TIMESTAMP, ValueType::INT, 0, 'timestamp of last cron execution'),
6988
];
7089
}

lib/private/Share/Helper.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77
*/
88
namespace OC\Share;
99

10+
use OC\Core\AppInfo\ConfigLexicon;
11+
use OCP\IAppConfig;
12+
use OCP\Server;
13+
1014
class Helper extends \OC\Share\Constants {
1115
/**
1216
* get default expire settings defined by the admin
1317
* @return array contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
1418
*/
1519
public static function getDefaultExpireSetting() {
1620
$config = \OC::$server->getConfig();
21+
$appConfig = Server::get(IAppConfig::class);
1722

1823
$defaultExpireSettings = ['defaultExpireDateSet' => false];
1924

2025
// get default expire settings
21-
$defaultExpireDate = $config->getAppValue('core', 'shareapi_default_expire_date', 'no');
22-
if ($defaultExpireDate === 'yes') {
23-
$enforceExpireDate = $config->getAppValue('core', 'shareapi_enforce_expire_date', 'no');
26+
if ($appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_DEFAULT)) {
2427
$defaultExpireSettings['defaultExpireDateSet'] = true;
2528
$defaultExpireSettings['expireAfterDays'] = (int)$config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
26-
$defaultExpireSettings['enforceExpireDate'] = $enforceExpireDate === 'yes';
29+
$defaultExpireSettings['enforceExpireDate'] = $appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_ENFORCED);
2730
}
2831

2932
return $defaultExpireSettings;
@@ -128,7 +131,7 @@ public static function isSameUserOnSameServer($user1, $server1, $user2, $server2
128131
}
129132

130133
public static function getTokenLength(): int {
131-
$config = \OCP\Server::get(\OCP\IAppConfig::class);
134+
$config = Server::get(\OCP\IAppConfig::class);
132135
$tokenLength = $config->getValueInt('core', 'shareapi_token_length', self::DEFAULT_TOKEN_LENGTH);
133136
$tokenLength = $tokenLength ?: self::DEFAULT_TOKEN_LENGTH;
134137

lib/private/Share20/Manager.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,18 +1788,17 @@ public function shareApiLinkEnforcePassword(bool $checkGroupMembership = true) {
17881788
* @return bool
17891789
*/
17901790
public function shareApiLinkDefaultExpireDate() {
1791-
return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
1791+
return $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_DEFAULT);
17921792
}
17931793

17941794
/**
17951795
* Is default link expire date enforced
1796-
*`
17971796
*
17981797
* @return bool
17991798
*/
18001799
public function shareApiLinkDefaultExpireDateEnforced() {
18011800
return $this->shareApiLinkDefaultExpireDate()
1802-
&& $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
1801+
&& $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_ENFORCED);
18031802
}
18041803

18051804

lib/private/Template/JSConfigHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ public function getConfig(): string {
9898
}
9999

100100
$enableLinkPasswordByDefault = $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_PASSWORD_DEFAULT);
101-
$defaultExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
101+
$defaultExpireDateEnabled = $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_DEFAULT);
102102
$defaultExpireDate = $enforceDefaultExpireDate = null;
103103
if ($defaultExpireDateEnabled) {
104104
$defaultExpireDate = (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
105-
$enforceDefaultExpireDate = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
105+
$enforceDefaultExpireDate = $this->appConfig->getValueBool('core', ConfigLexicon::SHARE_LINK_EXPIRE_DATE_ENFORCED);
106106
}
107107
$outgoingServer2serverShareEnabled = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
108108

tests/lib/Share20/ManagerTest.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,10 +1489,10 @@ public function testValidateExpirationDateEnforceButNotSet(): void {
14891489
$share = $this->manager->newShare();
14901490
$share->setProviderId('foo')->setId('bar');
14911491

1492-
$this->config->method('getAppValue')
1492+
$this->appConfig->method('getValueBool')
14931493
->willReturnMap([
1494-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
1495-
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
1494+
['core', 'shareapi_default_expire_date', true],
1495+
['core', 'shareapi_enforce_expire_date', true],
14961496
]);
14971497

14981498
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
@@ -1517,12 +1517,16 @@ public function testValidateExpirationDateEnforceButNotSetNewShare(): void {
15171517

15181518
$this->config->method('getAppValue')
15191519
->willReturnMap([
1520-
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
15211520
['core', 'shareapi_expire_after_n_days', '7', '3'],
1522-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
15231521
['core', 'link_defaultExpDays', '3', '3'],
15241522
]);
15251523

1524+
$this->appConfig->method('getValueBool')
1525+
->willReturnMap([
1526+
['core', 'shareapi_default_expire_date', true],
1527+
['core', 'shareapi_enforce_expire_date', true],
1528+
]);
1529+
15261530
$expected = new \DateTime('now', $this->timezone);
15271531
$expected->setTime(0, 0, 0);
15281532
$expected->add(new \DateInterval('P3D'));
@@ -1538,12 +1542,16 @@ public function testValidateExpirationDateEnforceRelaxedDefaultButNotSetNewShare
15381542

15391543
$this->config->method('getAppValue')
15401544
->willReturnMap([
1541-
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
15421545
['core', 'shareapi_expire_after_n_days', '7', '3'],
1543-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
15441546
['core', 'link_defaultExpDays', '3', '1'],
15451547
]);
15461548

1549+
$this->appConfig->method('getValueBool')
1550+
->willReturnMap([
1551+
['core', 'shareapi_default_expire_date', true],
1552+
['core', 'shareapi_enforce_expire_date', true],
1553+
]);
1554+
15471555
$expected = new \DateTime('now', $this->timezone);
15481556
$expected->setTime(0, 0, 0);
15491557
$expected->add(new \DateInterval('P1D'));
@@ -1566,9 +1574,13 @@ public function testValidateExpirationDateEnforceTooFarIntoFuture(): void {
15661574

15671575
$this->config->method('getAppValue')
15681576
->willReturnMap([
1569-
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
15701577
['core', 'shareapi_expire_after_n_days', '7', '3'],
1571-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
1578+
]);
1579+
1580+
$this->appConfig->method('getValueBool')
1581+
->willReturnMap([
1582+
['core', 'shareapi_default_expire_date', true],
1583+
['core', 'shareapi_enforce_expire_date', true],
15721584
]);
15731585

15741586
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
@@ -1587,9 +1599,13 @@ public function testValidateExpirationDateEnforceValid(): void {
15871599

15881600
$this->config->method('getAppValue')
15891601
->willReturnMap([
1590-
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
15911602
['core', 'shareapi_expire_after_n_days', '7', '3'],
1592-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
1603+
]);
1604+
1605+
$this->appConfig->method('getValueBool')
1606+
->willReturnMap([
1607+
['core', 'shareapi_default_expire_date', true],
1608+
['core', 'shareapi_enforce_expire_date', true],
15931609
]);
15941610

15951611
$hookListener = $this->createMock(DummyShareManagerListener::class);
@@ -1651,11 +1667,16 @@ public function testValidateExpirationDateNoDateDefault(): void {
16511667

16521668
$this->config->method('getAppValue')
16531669
->willReturnMap([
1654-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
16551670
['core', 'shareapi_expire_after_n_days', '7', '3'],
16561671
['core', 'link_defaultExpDays', '3', '3'],
16571672
]);
16581673

1674+
$this->appConfig->method('getValueBool')
1675+
->willReturnMap([
1676+
['core', 'shareapi_default_expire_date', true],
1677+
['core', 'shareapi_enforce_expire_date', false],
1678+
]);
1679+
16591680
$hookListener = $this->createMock(DummyShareManagerListener::class);
16601681
Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
16611682
$hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
@@ -1681,11 +1702,16 @@ public function testValidateExpirationDateDefault(): void {
16811702

16821703
$this->config->method('getAppValue')
16831704
->willReturnMap([
1684-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
16851705
['core', 'shareapi_expire_after_n_days', '7', '3'],
16861706
['core', 'link_defaultExpDays', '3', '1'],
16871707
]);
16881708

1709+
$this->appConfig->method('getValueBool')
1710+
->willReturnMap([
1711+
['core', 'shareapi_default_expire_date', true],
1712+
['core', 'shareapi_enforce_expire_date', false],
1713+
]);
1714+
16891715
$hookListener = $this->createMock(DummyShareManagerListener::class);
16901716
Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
16911717
$hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
@@ -1712,11 +1738,16 @@ public function testValidateExpirationNegativeOffsetTimezone(): void {
17121738

17131739
$this->config->method('getAppValue')
17141740
->willReturnMap([
1715-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
17161741
['core', 'shareapi_expire_after_n_days', '7', '3'],
17171742
['core', 'link_defaultExpDays', '3', '1'],
17181743
]);
17191744

1745+
$this->appConfig->method('getValueBool')
1746+
->willReturnMap([
1747+
['core', 'shareapi_default_expire_date', true],
1748+
['core', 'shareapi_enforce_expire_date', false],
1749+
]);
1750+
17201751
$hookListener = $this->createMock(DummyShareManagerListener::class);
17211752
Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
17221753
$hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
@@ -1779,10 +1810,14 @@ public function testValidateExpirationDateExistingShareNoDefault(): void {
17791810

17801811
$this->config->method('getAppValue')
17811812
->willReturnMap([
1782-
['core', 'shareapi_default_expire_date', 'no', 'yes'],
17831813
['core', 'shareapi_expire_after_n_days', '7', '6'],
17841814
]);
17851815

1816+
$this->appConfig->method('getValueBool')
1817+
->willReturnMap([
1818+
['core', 'shareapi_default_expire_date', true],
1819+
['core', 'shareapi_enforce_expire_date', false],
1820+
]);
17861821
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
17871822

17881823
$this->assertEquals(null, $share->getExpirationDate());

0 commit comments

Comments
 (0)