Merge pull request #7490 from nextcloud/fix_7428

Respect sharing options when searching using MailPlugin #7428
This commit is contained in:
Morris Jobke 2017-12-18 14:08:52 +01:00 committed by GitHub
commit 3da92a9a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 167 additions and 2 deletions

View File

@ -30,10 +30,13 @@ use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserSession;
use OCP\Share; use OCP\Share;
class MailPlugin implements ISearchPlugin { class MailPlugin implements ISearchPlugin {
protected $shareeEnumeration; protected $shareeEnumeration;
protected $shareWithGroupOnly;
/** @var IManager */ /** @var IManager */
private $contactsManager; private $contactsManager;
@ -42,12 +45,21 @@ class MailPlugin implements ISearchPlugin {
/** @var IConfig */ /** @var IConfig */
private $config; 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->contactsManager = $contactsManager;
$this->cloudIdManager = $cloudIdManager; $this->cloudIdManager = $cloudIdManager;
$this->config = $config; $this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; $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';
} }
/** /**
@ -77,6 +89,22 @@ class MailPlugin implements ISearchPlugin {
$exactEmailMatch = strtolower($emailAddress) === $lowerSearch; $exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
if (isset($contact['isLocalSystemBook'])) { 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) { if ($exactEmailMatch) {
try { try {
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);

View File

@ -31,6 +31,8 @@ use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserSession;
use OCP\Share; use OCP\Share;
use Test\TestCase; use Test\TestCase;
@ -50,17 +52,25 @@ class MailPluginTest extends TestCase {
/** @var SearchResult */ /** @var SearchResult */
protected $searchResult; protected $searchResult;
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
protected $groupManager;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->contactsManager = $this->createMock(IManager::class); $this->contactsManager = $this->createMock(IManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->cloudIdManager = new CloudIdManager(); $this->cloudIdManager = new CloudIdManager();
$this->searchResult = new SearchResult(); $this->searchResult = new SearchResult();
} }
public function instantiatePlugin() { 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);
} }
/** /**
@ -333,4 +343,131 @@ class MailPluginTest extends TestCase {
] ]
]; ];
} }
/**
* @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"]
]
]
];
}
} }