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

Respect sharing options when searching using MailPlugin #7428 #7490

Merged
merged 2 commits into from
Dec 18, 2017
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
30 changes: 29 additions & 1 deletion lib/private/Collaboration/Collaborators/MailPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserSession;
use OCP\Share;

class MailPlugin implements ISearchPlugin {
protected $shareeEnumeration;
protected $shareWithGroupOnly;

/** @var IManager */
private $contactsManager;
Expand All @@ -42,12 +45,21 @@ class MailPlugin implements ISearchPlugin {
/** @var IConfig */
private $config;

public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config) {
/** @var IGroupManager */
private $groupManager;

/** @var IUserSession */
private $userSession;

public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
$this->contactsManager = $contactsManager;
$this->cloudIdManager = $cloudIdManager;
$this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;

$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
}

/**
Expand Down Expand Up @@ -77,6 +89,22 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;

if (isset($contact['isLocalSystemBook'])) {
if ($this->shareWithGroupOnly) {
/*
* Check if the user may share with the user associated with the e-mail of the just found contact
*/
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
$found = false;
foreach ($userGroups as $userGroup) {
if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
$found = true;
break;
}
}
if (!$found) {
continue;
}
}
if ($exactEmailMatch) {
try {
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
Expand Down
139 changes: 138 additions & 1 deletion tests/lib/Collaboration/Collaborators/MailPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserSession;
use OCP\Share;
use Test\TestCase;

Expand All @@ -50,17 +52,25 @@ class MailPluginTest extends TestCase {
/** @var SearchResult */
protected $searchResult;

/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
protected $groupManager;

/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession;

public function setUp() {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->contactsManager = $this->createMock(IManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->cloudIdManager = new CloudIdManager();
$this->searchResult = new SearchResult();
}

public function instantiatePlugin() {
$this->plugin = new MailPlugin($this->contactsManager, $this->cloudIdManager, $this->config);
$this->plugin = new MailPlugin($this->contactsManager, $this->cloudIdManager, $this->config, $this->groupManager, $this->userSession);
}

/**
Expand Down Expand Up @@ -333,4 +343,131 @@ public function dataGetEmail() {
]
];
}

/**
* @dataProvider dataGetEmailGroupsOnly
*
* @param string $searchTerm
* @param array $contacts
* @param array $expected
* @param bool $exactIdMatch
* @param bool $reachedEnd
* @param array groups
*/
public function testSearchGroupsOnly($searchTerm, $contacts, $expected, $exactIdMatch, $reachedEnd, $userToGroupMapping) {
$this->config->expects($this->any())
->method('getAppValue')
->willReturnCallback(
function($appName, $key, $default) {
if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
return 'yes';
} else if ($appName === 'core' && $key === 'shareapi_only_share_with_group_members') {
return 'yes';
}
return $default;
}
);

$this->instantiatePlugin();

/** @var \OCP\IUser | \PHPUnit_Framework_MockObject_MockObject */
$currentUser = $this->createMock('\OCP\IUser');

$currentUser->expects($this->any())
->method('getUID')
->willReturn('currentUser');

$this->contactsManager->expects($this->any())
->method('search')
->with($searchTerm, ['EMAIL', 'FN'])
->willReturn($contacts);

$this->userSession->expects($this->any())
->method('getUser')
->willReturn($currentUser);

$this->groupManager->expects($this->any())
->method('getUserGroupIds')
->willReturnCallback(function(\OCP\IUser $user) use ($userToGroupMapping) {
return $userToGroupMapping[$user->getUID()];
});

$this->groupManager->expects($this->any())
->method('isInGroup')
->willReturnCallback(function($userId, $group) use ($userToGroupMapping) {
return in_array($group, $userToGroupMapping[$userId]);
});

$moreResults = $this->plugin->search($searchTerm, 0, 0, $this->searchResult);
$result = $this->searchResult->asArray();

$this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('emails')));
$this->assertEquals($expected, $result);
$this->assertSame($reachedEnd, $moreResults);
}

public function dataGetEmailGroupsOnly() {
return [
// The user `User` can share with the current user
[
'test',
[
[
'FN' => 'User',
'EMAIL' => ['test@example.com'],
'CLOUD' => ['test@localhost'],
'isLocalSystemBook' => true,
'UID' => 'User'
]
],
['users' => [['label' => 'User (test@example.com)','value' => ['shareType' => 0, 'shareWith' => 'test'],]], 'emails' => [], 'exact' => ['emails' => [], 'users' => []]],
false,
true,
[
"currentUser" => ["group1"],
"User" => ["group1"]
]
],
// The user `User` cannot share with the current user
[
'test',
[
[
'FN' => 'User',
'EMAIL' => ['test@example.com'],
'CLOUD' => ['test@localhost'],
'isLocalSystemBook' => true,
'UID' => 'User'
]
],
['emails'=> [], 'exact' => ['emails' => []]],
false,
true,
[
"currentUser" => ["group1"],
"User" => ["group2"]
]
],
// The user `User` cannot share with the current user, but there is an exact match on the e-mail address -> share by e-mail
[
'test@example.com',
[
[
'FN' => 'User',
'EMAIL' => ['test@example.com'],
'CLOUD' => ['test@localhost'],
'isLocalSystemBook' => true,
'UID' => 'User'
]
],
['emails' => [], 'exact' => ['emails' => [['label' => 'test@example.com', 'value' => ['shareType' => 4,'shareWith' => 'test@example.com']]]]],
false,
true,
[
"currentUser" => ["group1"],
"User" => ["group2"]
]
]
];
}
}