Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShareesAPIController should respect groups disallowed from sharing #25744

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion apps/files_sharing/lib/Controller/ShareesAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Share\IShare;
use OCP\Share\IManager;
use function usort;
Expand Down Expand Up @@ -103,6 +105,12 @@ class ShareesAPIController extends OCSController {
/** @var ISearch */
private $collaboratorSearch;

/** @var IGroupManager */
private $groupManager;

/** @var IUserManager */
private $userManager;

/**
* @param string $UserId
* @param string $appName
Expand All @@ -119,14 +127,20 @@ public function __construct(
IConfig $config,
IURLGenerator $urlGenerator,
IManager $shareManager,
ISearch $collaboratorSearch
ISearch $collaboratorSearch,
IGroupManager $groupManager,
IUserManager $userManager

) {
parent::__construct($appName, $request);
$this->userId = $UserId;
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->shareManager = $shareManager;
$this->collaboratorSearch = $collaboratorSearch;
$this->groupManager = $groupManager;
$this->userManager = $userManager;

}

/**
Expand All @@ -143,6 +157,18 @@ public function __construct(
*/
public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = true): DataResponse {

// if some groups are excluded, check the user is allowed to share
if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::getAppValue has been marked as deprecated
$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
Tetrachloromethane250 marked this conversation as resolved.
Show resolved Hide resolved
$excludedGroupsArray = !is_null(json_decode($excludedGroups))
? json_decode($excludedGroups, true) : '';
Tetrachloromethane250 marked this conversation as resolved.
Show resolved Hide resolved
$usersGroups = $this->groupManager->getUserGroupIds($this->userManager->get($this->userId));

Check notice

Code scanning / Psalm

PossiblyNullArgument Note

Argument 1 of OCP\IGroupManager::getUserGroupIds cannot be null, possibly null value provided
if (array_intersect($usersGroups, $excludedGroupsArray) == $usersGroups) {
Tetrachloromethane250 marked this conversation as resolved.
Show resolved Hide resolved
Tetrachloromethane250 marked this conversation as resolved.
Show resolved Hide resolved
return new DataResponse($this->result);
}
}


// only search for string larger than a given threshold
$threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
if (strlen($search) < $threshold) {
Expand Down
26 changes: 23 additions & 3 deletions apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Share\IShare;
use OCP\Share\IManager;

Expand All @@ -65,6 +67,12 @@ class ShareesAPIControllerTest extends TestCase {
/** @var ISearch|\PHPUnit\Framework\MockObject\MockObject */
protected $collaboratorSearch;

/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
private $groupManager;

/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
private $userManager;

protected function setUp(): void {
parent::setUp();

Expand All @@ -80,14 +88,20 @@ protected function setUp(): void {

$this->collaboratorSearch = $this->createMock(ISearch::class);

$this->groupManager = $this->createMock(IGroupManager::class);

$this->userManager = $this->createMock(IUserManager::class);

$this->sharees = new ShareesAPIController(
$this->uid,
'files_sharing',
$this->request,
$configMock,
$urlGeneratorMock,
$this->shareManager,
$this->collaboratorSearch
$this->collaboratorSearch,
$this->groupManager,
$this->userManager
);
}

Expand Down Expand Up @@ -239,6 +253,8 @@ public function testSearch($getData, $apiSetting, $enumSetting, $remoteSharingEn

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */
$config = $this->createMock(IConfig::class);

// TODO: Add exclude groups config request
$config->expects($this->exactly(1))
->method('getAppValue')
->with($this->anything(), $this->anything(), $this->anything())
Expand Down Expand Up @@ -266,7 +282,9 @@ public function testSearch($getData, $apiSetting, $enumSetting, $remoteSharingEn
$config,
$urlGenerator,
$this->shareManager,
$this->collaboratorSearch
$this->collaboratorSearch,
$this->groupManager,
$this->userManager
])
->setMethods(['isRemoteSharingAllowed', 'shareProviderExists', 'isRemoteGroupSharingAllowed'])
->getMock();
Expand Down Expand Up @@ -361,7 +379,9 @@ public function testSearchInvalid($getData, $message) {
$config,
$urlGenerator,
$this->shareManager,
$this->collaboratorSearch
$this->collaboratorSearch,
$this->groupManager,
$this->userManager
])
->setMethods(['isRemoteSharingAllowed'])
->getMock();
Expand Down