Skip to content

Commit

Permalink
dont show remote and email options if we have an exact match for loca…
Browse files Browse the repository at this point in the history
…l user email

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and skjnldsv committed May 5, 2020
1 parent 6b96fe9 commit 1cf1bc9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 47 deletions.
7 changes: 7 additions & 0 deletions lib/private/Collaboration/Collaborators/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public function search($search, array $shareTypes, $lookup, $limit, $offset) {
$searchResult->unsetResult($emailType);
}

// if we have an exact local user match, there is no need to show the remote and email matches
$userType = new SearchResultType('users');
if($searchResult->hasExactIdMatch($userType)) {
$searchResult->unsetResult($remoteType);
$searchResult->unsetResult($emailType);
}

return [$searchResult->asArray(), (bool)$hasMoreResults];
}

Expand Down
27 changes: 20 additions & 7 deletions lib/private/Collaboration/Collaborators/UserPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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 @@ -70,11 +71,11 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
$userGroups = [];
if ($this->shareWithGroupOnly) {
// Search in all the groups this user is part of
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
foreach ($userGroups as $userGroup) {
$usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset);
foreach ($usersTmp as $uid => $userDisplayName) {
$users[(string) $uid] = $userDisplayName;
$usersInGroup = $userGroup->searchDisplayName($search, $limit, $offset);
foreach ($usersInGroup as $user) {
$users[$user->getUID()] = $user;
}
}
} else {
Expand All @@ -96,9 +97,15 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {

$foundUserById = false;
$lowerSearch = strtolower($search);
foreach ($users as $uid => $userDisplayName) {
foreach ($users as $uid => $user) {
$userDisplayName = $user->getDisplayName();
$userEmail = $user->getEMailAddress();
$uid = (string) $uid;
if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
if (
strtolower($uid) === $lowerSearch ||
strtolower($userDisplayName) === $lowerSearch ||
strtolower($userEmail) === $lowerSearch
) {
if (strtolower($uid) === $lowerSearch) {
$foundUserById = true;
}
Expand Down Expand Up @@ -129,7 +136,10 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {

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

Expand All @@ -151,6 +161,9 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {

$type = new SearchResultType('users');
$searchResult->addResultSet($type, $result['wide'], $result['exact']);
if (count($result['exact'])) {
$searchResult->markExactIdMatch($type);
}

return $hasMoreResults;
}
Expand Down
96 changes: 56 additions & 40 deletions tests/lib/Collaboration/Collaborators/UserPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OC\Collaboration\Collaborators\UserPlugin;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
Expand Down Expand Up @@ -107,6 +108,16 @@ public function getUserMock($uid, $displayName, $enabled = true) {
return $user;
}

public function getGroupMock($gid) {
$group = $this->createMock(IGroup::class);

$group->expects($this->any())
->method('getGID')
->willReturn($gid);

return $group;
}

public function dataGetUsers() {
return [
['test', false, true, [], [], [], [], true, false],
Expand All @@ -117,33 +128,33 @@ public function dataGetUsers() {
'test', false, true, [], [],
[
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']],
], [], true, $this->getUserMock('test', 'Test')
], [], true, $this->getUserMock('test', 'Test'),
],
[
'test', false, false, [], [],
[
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']],
], [], true, $this->getUserMock('test', 'Test')
], [], true, $this->getUserMock('test', 'Test'),
],
[
'test', true, true, [], [],
[], [], true, $this->getUserMock('test', 'Test')
[], [], true, $this->getUserMock('test', 'Test'),
],
[
'test', true, false, [], [],
[], [], true, $this->getUserMock('test', 'Test')
[], [], true, $this->getUserMock('test', 'Test'),
],
[
'test', true, true, ['test-group'], [['test-group', 'test', 2, 0, []]],
[
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']],
], [], true, $this->getUserMock('test', 'Test')
], [], true, $this->getUserMock('test', 'Test'),
],
[
'test', true, false, ['test-group'], [['test-group', 'test', 2, 0, []]],
[
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']],
], [], true, $this->getUserMock('test', 'Test')
], [], true, $this->getUserMock('test', 'Test'),
],
[
'test',
Expand Down Expand Up @@ -247,7 +258,7 @@ public function dataGetUsers() {
true,
['abc', 'xyz'],
[
['abc', 'test', 2, 0, ['test1' => 'Test One']],
['abc', 'test', 2, 0, [$this->getUserMock('test1', 'Test One')]],
['xyz', 'test', 2, 0, []],
],
[],
Expand All @@ -263,7 +274,7 @@ public function dataGetUsers() {
false,
['abc', 'xyz'],
[
['abc', 'test', 2, 0, ['test1' => 'Test One']],
['abc', 'test', 2, 0, [$this->getUserMock('test1', 'Test One')]],
['xyz', 'test', 2, 0, []],
],
[],
Expand All @@ -278,12 +289,12 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
'test1' => 'Test One',
'test2' => 'Test Two',
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
]],
['xyz', 'test', 2, 0, [
'test1' => 'Test One',
'test2' => 'Test Two',
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
]],
],
[],
Expand All @@ -301,12 +312,12 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
'test1' => 'Test One',
'test2' => 'Test Two',
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
]],
['xyz', 'test', 2, 0, [
'test1' => 'Test One',
'test2' => 'Test Two',
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
]],
],
[],
Expand All @@ -321,10 +332,10 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
'test' => 'Test One',
$this->getUserMock('test', 'Test One'),
]],
['xyz', 'test', 2, 0, [
'test2' => 'Test Two',
$this->getUserMock('test2', 'Test Two'),
]],
],
[
Expand All @@ -343,10 +354,10 @@ public function dataGetUsers() {
['abc', 'xyz'],
[
['abc', 'test', 2, 0, [
'test' => 'Test One',
$this->getUserMock('test', 'Test One'),
]],
['xyz', 'test', 2, 0, [
'test2' => 'Test Two',
$this->getUserMock('test2', 'Test Two'),
]],
],
[
Expand Down Expand Up @@ -404,31 +415,36 @@ function($appName, $key, $default)
->method('getUser')
->willReturn($this->user);

if(!$shareWithGroupOnly) {
if (!$shareWithGroupOnly) {
$this->userManager->expects($this->once())
->method('searchDisplayName')
->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->expects($this->exactly(2))
->method('getUserGroupIds')
->withConsecutive(
[$this->user],
[$singleUser]
)
$this->groupManager->method('getUserGroups')
->with($this->user)
->willReturn($groups);

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

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

if ($singleUser !== false) {
Expand All @@ -451,24 +467,24 @@ public function takeOutCurrentUserProvider() {
$inputUsers = [
'alice' => 'Alice',
'bob' => 'Bob',
'carol' => 'Carol'
'carol' => 'Carol',
];
return [
[
$inputUsers,
['alice', 'carol'],
'bob'
'bob',
],
[
$inputUsers,
['alice', 'bob', 'carol'],
'dave'
'dave',
],
[
$inputUsers,
['alice', 'bob', 'carol'],
null
]
null,
],
];
}

Expand All @@ -483,8 +499,8 @@ public function testTakeOutCurrentUser(array $users, array $expectedUIDs, $curre

$this->session->expects($this->once())
->method('getUser')
->willReturnCallback(function() use ($currentUserId) {
if($currentUserId !== null) {
->willReturnCallback(function () use ($currentUserId) {
if ($currentUserId !== null) {
return $this->getUserMock($currentUserId, $currentUserId);
}
return null;
Expand Down

0 comments on commit 1cf1bc9

Please sign in to comment.