-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: respect advanced group sharing settings in frontend
Signed-off-by: Richard Steinmetz <[email protected]>
- Loading branch information
Showing
5 changed files
with
186 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Contacts\Service; | ||
|
||
use OCP\IConfig; | ||
use OCP\IGroup; | ||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
use OCP\Share\IManager as IShareManager; | ||
|
||
class GroupSharingService { | ||
|
||
public function __construct( | ||
private IConfig $config, | ||
private IGroupManager $groupManager, | ||
private IShareManager $shareManager, | ||
) { | ||
} | ||
|
||
public function isGroupSharingAllowed(IUser $user): bool { | ||
if (!$this->shareManager->allowGroupSharing()) { | ||
return false; | ||
} | ||
|
||
$userGroups = $this->groupManager->getUserGroups($user); | ||
$userGroupNames = array_map(static fn (IGroup $group) => $group->getGID(), $userGroups); | ||
|
||
$excludeGroupList = json_decode($this->config->getAppValue('core', 'shareapi_exclude_groups_list', '[]')); | ||
$excludeGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups'); | ||
|
||
// "no" => Allow sharing for everyone | ||
// "yes" => Exclude listed groups from sharing | ||
// "allow" => Limit sharing to listed groups | ||
return match ($excludeGroups) { | ||
'yes' => count(array_intersect($userGroupNames, $excludeGroupList)) === 0, | ||
'allow' => count(array_intersect($userGroupNames, $excludeGroupList)) > 0, | ||
default => true, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace unit\Service; | ||
|
||
use ChristophWurst\Nextcloud\Testing\TestCase; | ||
use OCA\Contacts\Service\GroupSharingService; | ||
use OCP\IConfig; | ||
use OCP\IGroup; | ||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
use OCP\Share\IManager as IShareManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class GroupSharingServiceTest extends TestCase { | ||
private GroupSharingService $service; | ||
private IConfig|MockObject $config; | ||
private IGroupManager|MockObject $groupManager; | ||
private IShareManager|MockObject $shareManager; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->config = $this->createMock(IConfig::class); | ||
$this->groupManager = $this->createMock(IGroupManager::class); | ||
$this->shareManager = $this->createMock(IShareManager::class); | ||
|
||
$this->service = new GroupSharingService( | ||
$this->config, | ||
$this->groupManager, | ||
$this->shareManager, | ||
); | ||
} | ||
|
||
public function provideTestIsGroupSharingAllowed(): array { | ||
return [ | ||
// Basic group sharing is forbidden (-> short circuit) | ||
[false, false, '["group1"]', 'no'], | ||
[false, false, '["group1"]', 'yes'], | ||
[false, false, '["group1"]', 'allow'], | ||
|
||
// Basic sharing is allowed and allow sharing with every group | ||
[true, true, '["group1"]', 'no'], | ||
[true, true, '[]', 'no'], | ||
[true, true, '["group99"]', 'no'], | ||
|
||
// Basic sharing is allowed and user's group is excluded | ||
[false, true, '["group1"]', 'yes'], | ||
[false, true, '["group2"]', 'yes'], | ||
[false, true, '["group2", "group3"]', 'yes'], | ||
|
||
// Basic sharing is allowed and user's group is not excluded | ||
[true, true, '["group3"]', 'yes'], | ||
[true, true, '[]', 'yes'], | ||
|
||
// Basic sharing is allowed and user's group is included | ||
[true, true, '["group1"]', 'allow'], | ||
[true, true, '["group2"]', 'allow'], | ||
[true, true, '["group2", "group3"]', 'allow'], | ||
|
||
// Basic sharing is allowed and user's group is not included | ||
[false, true, '["group3"]', 'allow'], | ||
[false, true, '[]', 'allow'], | ||
]; | ||
} | ||
|
||
/** @dataProvider provideTestIsGroupSharingAllowed */ | ||
public function testIsGroupSharingAllowed( | ||
bool $expected, | ||
bool $allowGroupSharing, | ||
string $excludeGroupList, | ||
string $excludeGroups, | ||
): void { | ||
$user = $this->createMock(IUser::class); | ||
$group1 = $this->createMock(IGroup::class); | ||
$group1->method('getGID') | ||
->willReturn('group1'); | ||
$group2 = $this->createMock(IGroup::class); | ||
$group2->method('getGID') | ||
->willReturn('group2'); | ||
|
||
$this->shareManager->expects(self::once()) | ||
->method('allowGroupSharing') | ||
->willReturn($allowGroupSharing); | ||
if ($allowGroupSharing) { | ||
$this->groupManager->expects(self::once()) | ||
->method('getUserGroups') | ||
->with($user) | ||
->willReturn([$group1, $group2]); | ||
$this->config->expects(self::exactly(2)) | ||
->method('getAppValue') | ||
->willReturnMap([ | ||
['core', 'shareapi_exclude_groups_list', '[]', $excludeGroupList], | ||
['core', 'shareapi_exclude_groups', '', $excludeGroups], | ||
]); | ||
} else { | ||
$this->groupManager->expects(self::never()) | ||
->method('getUserGroups'); | ||
$this->config->expects(self::never()) | ||
->method('getAppValue'); | ||
} | ||
|
||
$actual = $this->service->isGroupSharingAllowed($user); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
} |