Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions build/integration/sharees_features/sharees.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ Feature: sharees
And "exact remotes" sharees returned is empty
And "remotes" sharees returned is empty

Scenario: Search only with group members - allowed with exact match
Given As an "test"
And parameter "shareapi_only_share_with_group_members" of app "core" is set to "yes"
And user "Sharee1" belongs to group "ShareeGroup"
When getting sharees for
| search | Sharee1 |
| itemType | file |
Then the OCS status code should be "100"
And the HTTP status code should be "200"
And "exact users" sharees returned are
| Sharee1 | 0 | Sharee1 |
And "users" sharees returned is empty
And "exact groups" sharees returned is empty
And "groups" sharees returned is empty
And "exact remotes" sharees returned is empty
And "remotes" sharees returned is empty

Scenario: Search only with group members - no group as non-member
Given As an "Sharee1"
And parameter "shareapi_only_share_with_group_members" of app "core" is set to "yes"
Expand Down
19 changes: 7 additions & 12 deletions lib/private/Collaboration/Collaborators/UserPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
Expand Down Expand Up @@ -73,20 +72,19 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
$users = [];
$hasMoreResults = false;

$userGroups = [];
$currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
if ($this->shareWithGroupOnly) {
// Search in all the groups this user is part of
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
foreach ($userGroups as $userGroup) {
$usersInGroup = $userGroup->searchDisplayName($search, $limit, $offset);
foreach ($usersInGroup as $user) {
$users[$user->getUID()] = $user;
foreach ($currentUserGroups as $userGroupId) {
$usersInGroup = $this->groupManager->displayNamesInGroup($userGroupId, $search, $limit, $offset);
foreach ($usersInGroup as $userId => $displayName) {
$userId = (string) $userId;
$users[$userId] = $this->userManager->get($userId);
}
}
} else {
// Search in all users
$usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
$currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
foreach ($usersTmp as $user) {
if ($user->isEnabled()) { // Don't keep deactivated users
$users[$user->getUID()] = $user;
Expand Down Expand Up @@ -155,10 +153,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {

if ($this->shareWithGroupOnly) {
// Only add, if we have a common group
$userGroupIds = array_map(function (IGroup $group) {
return $group->getGID();
}, $userGroups);
$commonGroups = array_intersect($userGroupIds, $this->groupManager->getUserGroupIds($user));
$commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
$addUser = !empty($commonGroups);
}

Expand Down
84 changes: 47 additions & 37 deletions tests/lib/Collaboration/Collaborators/UserPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function dataGetUsers() {
true,
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [$this->getUserMock('test1', 'Test One')]],
['abc', 'test', 2, 0, ['test1' => 'Test One']],
['xyz', 'test', 2, 0, []],
],
[],
Expand All @@ -284,20 +284,22 @@ public function dataGetUsers() {
],
true,
false,
[['test1', $this->getUserMock('test1', 'Test One')]],
],
[
'test',
true,
false,
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [$this->getUserMock('test1', 'Test One')]],
['abc', 'test', 2, 0, ['test1' => 'Test One']],
['xyz', 'test', 2, 0, []],
],
[],
[],
true,
false,
[['test1', $this->getUserMock('test1', 'Test One')]],
],
[
'test',
Expand All @@ -306,12 +308,12 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
'test1' => 'Test One',
'test2' => 'Test Two',
]],
['xyz', 'test', 2, 0, [
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
'test1' => 'Test One',
'test2' => 'Test Two',
]],
],
[],
Expand All @@ -321,6 +323,10 @@ public function dataGetUsers() {
],
false,
false,
[
['test1', $this->getUserMock('test1', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
],
],
[
'test',
Expand All @@ -329,18 +335,22 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
'test1' => 'Test One',
'test2' => 'Test Two',
]],
['xyz', 'test', 2, 0, [
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
'test1' => 'Test One',
'test2' => 'Test Two',
]],
],
[],
[],
true,
false,
[
['test1', $this->getUserMock('test1', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
],
],
[
'test',
Expand All @@ -349,10 +359,10 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
$this->getUserMock('test', 'Test One'),
'test' => 'Test One',
]],
['xyz', 'test', 2, 0, [
$this->getUserMock('test2', 'Test Two'),
'test2' => 'Test Two',
]],
],
[
Expand All @@ -363,6 +373,10 @@ public function dataGetUsers() {
],
false,
false,
[
['test', $this->getUserMock('test', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
],
],
[
'test',
Expand All @@ -371,10 +385,10 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
$this->getUserMock('test', 'Test One'),
'test' => 'Test One',
]],
['xyz', 'test', 2, 0, [
$this->getUserMock('test2', 'Test Two'),
'test2' => 'Test Two',
]],
],
[
Expand All @@ -383,6 +397,10 @@ public function dataGetUsers() {
[],
true,
false,
[
['test', $this->getUserMock('test', 'Test One')],
['test2', $this->getUserMock('test2', 'Test Two')],
],
],
];
}
Expand All @@ -399,6 +417,7 @@ public function dataGetUsers() {
* @param array $expected
* @param bool $reachedEnd
* @param bool|IUser $singleUser
* @param array $users
*/
public function testSearch(
$searchTerm,
Expand All @@ -409,7 +428,8 @@ public function testSearch(
array $exactExpected,
array $expected,
$reachedEnd,
$singleUser
$singleUser,
array $users = []
) {
$this->mockConfig($shareWithGroupOnly, $shareeEnumeration, false);
$this->instantiatePlugin();
Expand All @@ -424,39 +444,29 @@ public function testSearch(
->with($searchTerm, $this->limit, $this->offset)
->willReturn($userResponse);
} else {
$groups = array_combine($groupResponse, array_map(function ($gid) {
return $this->getGroupMock($gid);
}, $groupResponse));
if ($singleUser !== false) {
$this->groupManager->method('getUserGroups')
->with($this->user)
->willReturn($groups);
$this->groupManager->method('getUserGroupIds')
->with($this->user)
->willReturn($groupResponse);

if ($singleUser !== false) {
$this->groupManager->method('getUserGroupIds')
->with($singleUser)
->willReturn($groupResponse);
} else {
$this->groupManager->expects($this->once())
->method('getUserGroups')
->with($this->user)
->willReturn($groups);
}

foreach ($userResponse as $groupDefinition) {
[$gid, $search, $limit, $offset, $users] = $groupDefinition;
$groups[$gid]->method('searchDisplayName')
->with($search, $limit, $offset)
->willReturn($users);
}
$this->groupManager->method('displayNamesInGroup')
->willReturnMap($userResponse);
}

if ($singleUser !== false) {
$this->userManager->expects($this->once())
->method('get')
->with($searchTerm)
->willReturn($singleUser);
$users[] = [$searchTerm, $singleUser];
}

if (!empty($users)) {
$this->userManager->expects($this->atLeastOnce())
->method('get')
->willReturnMap($users);
}

$moreResults = $this->plugin->search($searchTerm, $this->limit, $this->offset, $this->searchResult);
$result = $this->searchResult->asArray();
Expand Down