Skip to content

Commit f4875d7

Browse files
committed
Fix tests and add a new one to test the new feature
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent cb4d0c9 commit f4875d7

File tree

8 files changed

+64
-12
lines changed

8 files changed

+64
-12
lines changed

apps/files_sharing/tests/CapabilitiesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function testOnlyLinkSharing() {
139139
$map = [
140140
['core', 'shareapi_enabled', 'yes', 'yes'],
141141
['core', 'shareapi_allow_links', 'yes', 'yes'],
142+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
142143
];
143144
$result = $this->getResults($map);
144145
$this->assertIsArray($result['public']);
@@ -149,6 +150,7 @@ public function testLinkPassword() {
149150
$map = [
150151
['core', 'shareapi_enabled', 'yes', 'yes'],
151152
['core', 'shareapi_allow_links', 'yes', 'yes'],
153+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
152154
['core', 'shareapi_enforce_links_password', 'no', 'yes'],
153155
];
154156
$result = $this->getResults($map);
@@ -161,6 +163,7 @@ public function testLinkNoPassword() {
161163
$map = [
162164
['core', 'shareapi_enabled', 'yes', 'yes'],
163165
['core', 'shareapi_allow_links', 'yes', 'yes'],
166+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
164167
['core', 'shareapi_enforce_links_password', 'no', 'no'],
165168
];
166169
$result = $this->getResults($map);
@@ -174,6 +177,7 @@ public function testLinkNoExpireDate() {
174177
['core', 'shareapi_enabled', 'yes', 'yes'],
175178
['core', 'shareapi_allow_links', 'yes', 'yes'],
176179
['core', 'shareapi_default_expire_date', 'no', 'no'],
180+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
177181
];
178182
$result = $this->getResults($map);
179183
$this->assertArrayHasKey('expire_date', $result['public']);
@@ -188,6 +192,7 @@ public function testLinkExpireDate() {
188192
['core', 'shareapi_default_expire_date', 'no', 'yes'],
189193
['core', 'shareapi_expire_after_n_days', '7', '7'],
190194
['core', 'shareapi_enforce_expire_date', 'no', 'no'],
195+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
191196
];
192197
$result = $this->getResults($map);
193198
$this->assertArrayHasKey('expire_date', $result['public']);
@@ -203,6 +208,7 @@ public function testLinkExpireDateEnforced() {
203208
['core', 'shareapi_allow_links', 'yes', 'yes'],
204209
['core', 'shareapi_default_expire_date', 'no', 'yes'],
205210
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
211+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
206212
];
207213
$result = $this->getResults($map);
208214
$this->assertArrayHasKey('expire_date', $result['public']);
@@ -215,6 +221,7 @@ public function testLinkSendMail() {
215221
['core', 'shareapi_enabled', 'yes', 'yes'],
216222
['core', 'shareapi_allow_links', 'yes', 'yes'],
217223
['core', 'shareapi_allow_public_notification', 'no', 'yes'],
224+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
218225
];
219226
$result = $this->getResults($map);
220227
$this->assertTrue($result['public']['send_mail']);
@@ -225,6 +232,7 @@ public function testLinkNoSendMail() {
225232
['core', 'shareapi_enabled', 'yes', 'yes'],
226233
['core', 'shareapi_allow_links', 'yes', 'yes'],
227234
['core', 'shareapi_allow_public_notification', 'no', 'no'],
235+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
228236
];
229237
$result = $this->getResults($map);
230238
$this->assertFalse($result['public']['send_mail']);
@@ -234,6 +242,7 @@ public function testResharing() {
234242
$map = [
235243
['core', 'shareapi_enabled', 'yes', 'yes'],
236244
['core', 'shareapi_allow_resharing', 'yes', 'yes'],
245+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
237246
];
238247
$result = $this->getResults($map);
239248
$this->assertTrue($result['resharing']);
@@ -243,6 +252,7 @@ public function testNoResharing() {
243252
$map = [
244253
['core', 'shareapi_enabled', 'yes', 'yes'],
245254
['core', 'shareapi_allow_resharing', 'yes', 'no'],
255+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
246256
];
247257
$result = $this->getResults($map);
248258
$this->assertFalse($result['resharing']);
@@ -253,6 +263,7 @@ public function testLinkPublicUpload() {
253263
['core', 'shareapi_enabled', 'yes', 'yes'],
254264
['core', 'shareapi_allow_links', 'yes', 'yes'],
255265
['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
266+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
256267
];
257268
$result = $this->getResults($map);
258269
$this->assertTrue($result['public']['upload']);
@@ -264,6 +275,7 @@ public function testLinkNoPublicUpload() {
264275
['core', 'shareapi_enabled', 'yes', 'yes'],
265276
['core', 'shareapi_allow_links', 'yes', 'yes'],
266277
['core', 'shareapi_allow_public_upload', 'yes', 'no'],
278+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
267279
];
268280
$result = $this->getResults($map);
269281
$this->assertFalse($result['public']['upload']);

apps/settings/js/admin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ window.addEventListener('DOMContentLoaded', function(){
9494
$("#setDefaultRemoteExpireDate").toggleClass('hidden', !this.checked);
9595
});
9696

97+
$('#enforceLinkPassword').change(function() {
98+
$("#selectPasswordsExcludedGroups").toggleClass('hidden', !this.checked);
99+
});
100+
97101
$('#publicShareDisclaimer').change(function() {
98102
$("#publicShareDisclaimerText").toggleClass('hidden', !this.checked);
99103
if(!this.checked) {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function getForm() {
7272
$linksExcludeGroupsList = !is_null(json_decode($linksExcludedGroups))
7373
? implode('|', json_decode($linksExcludedGroups, true)) : '';
7474

75+
$excludedPasswordGroups = $this->config->getAppValue('core', 'shareapi_enforce_links_password_excluded_groups', '');
76+
$excludedPasswordGroupsList = !is_null(json_decode($excludedPasswordGroups))
77+
? implode('|', json_decode($excludedPasswordGroups, true)) : '';
78+
79+
7580
$parameters = [
7681
// Built-In Sharing
7782
'sharingAppEnabled' => $this->appManager->isEnabledForUser('files_sharing'),
@@ -84,7 +89,9 @@ public function getForm() {
8489
'restrictUserEnumerationToGroup' => $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no'),
8590
'restrictUserEnumerationToPhone' => $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no'),
8691
'restrictUserEnumerationFullMatch' => $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes'),
87-
'enforceLinkPassword' => Util::isPublicLinkPasswordRequired(),
92+
'enforceLinkPassword' => Util::isPublicLinkPasswordRequired(false),
93+
'passwordExcludedGroups' => $excludedPasswordGroupsList,
94+
'passwordExcludedGroupsFeatureEnabled' => $this->config->getSystemValueBool('allow_disabled_password_enforcement_groups', false),
8895
'onlyShareWithGroupMembers' => $this->shareManager->shareWithGroupMembersOnly(),
8996
'shareAPIEnabled' => $this->config->getAppValue('core', 'shareapi_enabled', 'yes'),
9097
'shareDefaultExpireDateSet' => $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no'),

apps/settings/templates/settings/admin/sharing.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@
120120
} ?> />
121121
<label for="enforceLinkPassword"><?php p($l->t('Enforce password protection'));?></label><br/>
122122

123-
<p id="selectPasswordsExcludedGroups" class="indent <?php if (!$_['enforceLinkPassword']) { p('hidden'); } ?>">
124-
<input name="shareapi_enforce_links_password_excluded_groups" id="passwordsExcludedGroups" value="<?php p($_['allowLinksExcludeGroups']) ?>" style="width: 400px" class="noJSAutoUpdate"/>
125-
</p>
123+
<?php if ($_['passwordExcludedGroupsFeatureEnabled']) { ?>
124+
<div id="selectPasswordsExcludedGroups" class="indent <?php if (!$_['enforceLinkPassword']) { p('hidden'); } ?>">
125+
<div class="indent">
126+
<label for="shareapi_enforce_links_password_excluded_groups"><?php p($l->t('Exclude groups from password requirements:'));?>
127+
<br />
128+
<input name="shareapi_enforce_links_password_excluded_groups" id="passwordsExcludedGroups" value="<?php p($_['passwordExcludedGroups']) ?>" style="width: 400px" class="noJSAutoUpdate"/>
129+
</div>
130+
</div>
131+
<?php } ?>
126132

127133
<input type="checkbox" name="shareapi_default_expire_date" id="shareapiDefaultExpireDate" class="checkbox" value="1" <?php if ($_['shareDefaultExpireDateSet'] === 'yes') { print_unescaped('checked="checked"'); } ?> />
128134

lib/private/Share20/Manager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,11 +1784,12 @@ public function shareApiAllowLinks() {
17841784
/**
17851785
* Is password on public link requires
17861786
*
1787+
* @param bool Check group membership exclusion
17871788
* @return bool
17881789
*/
1789-
public function shareApiLinkEnforcePassword() {
1790+
public function shareApiLinkEnforcePassword(bool $checkGroupMembership = true) {
17901791
$excludedGroups = $this->config->getAppValue('core', 'shareapi_enforce_links_password_excluded_groups', '');
1791-
if ($excludedGroups !== '') {
1792+
if ($excludedGroups !== '' && $checkGroupMembership) {
17921793
$excludedGroups = json_decode($excludedGroups);
17931794
$user = $this->userSession->getUser();
17941795
$userGroups = $this->groupManager->getUserGroupIds($user);

lib/private/legacy/OC_Util.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,16 @@ public static function setupFS(?string $user = '') {
348348
}
349349

350350
/**
351-
* check if a password is required for each public link
351+
* Check if a password is required for each public link
352352
*
353+
* @param bool $checkGroupMembership Check group membership exclusion
353354
* @return boolean
354355
* @suppress PhanDeprecatedFunction
355356
*/
356-
public static function isPublicLinkPasswordRequired() {
357+
public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) {
357358
/** @var IManager $shareManager */
358359
$shareManager = \OC::$server->get(IManager::class);
359-
return $shareManager->shareApiLinkEnforcePassword();
360+
return $shareManager->shareApiLinkEnforcePassword($checkGroupMembership);
360361
}
361362

362363
/**

lib/public/Util.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,14 @@ public static function naturalSortCompare($a, $b) {
542542
}
543543

544544
/**
545-
* check if a password is required for each public link
545+
* Check if a password is required for each public link
546+
*
547+
* @param bool $checkGroupMembership Check group membership exclusion
546548
* @return boolean
547549
* @since 7.0.0
548550
*/
549-
public static function isPublicLinkPasswordRequired() {
550-
return \OC_Util::isPublicLinkPasswordRequired();
551+
public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) {
552+
return \OC_Util::isPublicLinkPasswordRequired($checkGroupMembership);
551553
}
552554

553555
/**

tests/lib/Share20/ManagerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,31 @@ public function testVerifyPasswordNullButEnforced() {
505505
$this->expectExceptionMessage('Passwords are enforced for link and mail shares');
506506

507507
$this->config->method('getAppValue')->willReturnMap([
508+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
508509
['core', 'shareapi_enforce_links_password', 'no', 'yes'],
509510
]);
510511

511512
self::invokePrivate($this->manager, 'verifyPassword', [null]);
512513
}
513514

515+
public function testVerifyPasswordNotEnforcedGroup() {
516+
$this->config->method('getAppValue')->willReturnMap([
517+
['core', 'shareapi_enforce_links_password_excluded_groups', '', '["admin"]'],
518+
['core', 'shareapi_enforce_links_password', 'no', 'yes'],
519+
]);
520+
521+
// Create admin user
522+
$user = $this->createMock(IUser::class);
523+
$this->userSession->method('getUser')->willReturn($user);
524+
$this->groupManager->method('getUserGroupIds')->with($user)->willReturn(['admin']);
525+
526+
$result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
527+
$this->assertNull($result);
528+
}
529+
514530
public function testVerifyPasswordNull() {
515531
$this->config->method('getAppValue')->willReturnMap([
532+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
516533
['core', 'shareapi_enforce_links_password', 'no', 'no'],
517534
]);
518535

@@ -522,6 +539,7 @@ public function testVerifyPasswordNull() {
522539

523540
public function testVerifyPasswordHook() {
524541
$this->config->method('getAppValue')->willReturnMap([
542+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
525543
['core', 'shareapi_enforce_links_password', 'no', 'no'],
526544
]);
527545

@@ -543,6 +561,7 @@ public function testVerifyPasswordHookFails() {
543561
$this->expectExceptionMessage('password not accepted');
544562

545563
$this->config->method('getAppValue')->willReturnMap([
564+
['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
546565
['core', 'shareapi_enforce_links_password', 'no', 'no'],
547566
]);
548567

0 commit comments

Comments
 (0)