Principal search: Take sharing settings into account

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
This commit is contained in:
Georg Ehrke 2017-12-04 15:02:55 +01:00 committed by Roeland Jago Douma
parent f543039858
commit 6802e2b59a
No known key found for this signature in database
GPG Key ID: F941078878347C0C
6 changed files with 275 additions and 35 deletions

View File

@ -44,6 +44,8 @@ $authBackend = new Auth(
$principalBackend = new Principal( $principalBackend = new Principal(
\OC::$server->getUserManager(), \OC::$server->getUserManager(),
\OC::$server->getGroupManager(), \OC::$server->getGroupManager(),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
'principals/' 'principals/'
); );
$db = \OC::$server->getDatabaseConnection(); $db = \OC::$server->getDatabaseConnection();

View File

@ -45,6 +45,8 @@ $authBackend = new Auth(
$principalBackend = new Principal( $principalBackend = new Principal(
\OC::$server->getUserManager(), \OC::$server->getUserManager(),
\OC::$server->getGroupManager(), \OC::$server->getGroupManager(),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
'principals/' 'principals/'
); );
$db = \OC::$server->getDatabaseConnection(); $db = \OC::$server->getDatabaseConnection();

View File

@ -75,7 +75,9 @@ class CreateCalendar extends Command {
} }
$principalBackend = new Principal( $principalBackend = new Principal(
$this->userManager, $this->userManager,
$this->groupManager $this->groupManager,
\OC::$server->getShareManager(),
\OC::$server->getUserSession()
); );
$random = \OC::$server->getSecureRandom(); $random = \OC::$server->getSecureRandom();
$logger = \OC::$server->getLogger(); $logger = \OC::$server->getLogger();

View File

@ -34,6 +34,8 @@ use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager as IShareManager;
use Sabre\DAV\Exception; use Sabre\DAV\Exception;
use \Sabre\DAV\PropPatch; use \Sabre\DAV\PropPatch;
use Sabre\DAVACL\PrincipalBackend\BackendInterface; use Sabre\DAVACL\PrincipalBackend\BackendInterface;
@ -47,6 +49,12 @@ class Principal implements BackendInterface {
/** @var IGroupManager */ /** @var IGroupManager */
private $groupManager; private $groupManager;
/** @var IShareManager */
private $shareManager;
/** @var IUserSession */
private $userSession;
/** @var string */ /** @var string */
private $principalPrefix; private $principalPrefix;
@ -56,13 +64,19 @@ class Principal implements BackendInterface {
/** /**
* @param IUserManager $userManager * @param IUserManager $userManager
* @param IGroupManager $groupManager * @param IGroupManager $groupManager
* @param IShareManager $shareManager
* @param IUserSession $userSession
* @param string $principalPrefix * @param string $principalPrefix
*/ */
public function __construct(IUserManager $userManager, public function __construct(IUserManager $userManager,
IGroupManager $groupManager, IGroupManager $groupManager,
IShareManager $shareManager,
IUserSession $userSession,
$principalPrefix = 'principals/users/') { $principalPrefix = 'principals/users/') {
$this->userManager = $userManager; $this->userManager = $userManager;
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->shareManager = $shareManager;
$this->userSession = $userSession;
$this->principalPrefix = trim($principalPrefix, '/'); $this->principalPrefix = trim($principalPrefix, '/');
$this->hasGroups = ($principalPrefix === 'principals/users/'); $this->hasGroups = ($principalPrefix === 'principals/users/');
} }
@ -106,7 +120,7 @@ class Principal implements BackendInterface {
if ($prefix === $this->principalPrefix) { if ($prefix === $this->principalPrefix) {
$user = $this->userManager->get($name); $user = $this->userManager->get($name);
if (!is_null($user)) { if ($user !== null) {
return $this->userToPrincipal($user); return $this->userToPrincipal($user);
} }
} }
@ -189,37 +203,65 @@ class Principal implements BackendInterface {
* @param string $test * @param string $test
* @return array * @return array
*/ */
function searchUserPrincipals(array $searchProperties, $test = 'allof') { protected function searchUserPrincipals(array $searchProperties, $test = 'allof') {
$results = []; $results = [];
//TODO: If more properties should be supported, hook this up to a db query // If sharing is disabled, return the empty array
if (!$this->shareManager->shareApiEnabled()) {
return [];
}
// If sharing is restricted to group members only,
// return only members that have groups in common
$restrictGroups = false;
if ($this->shareManager->shareWithGroupMembersOnly()) {
$user = $this->userSession->getUser();
if (!$user) {
return [];
}
$restrictGroups = $this->groupManager->getUserGroupIds($user);
}
foreach ($searchProperties as $prop => $value) { foreach ($searchProperties as $prop => $value) {
switch ($prop) { switch ($prop) {
case '{http://sabredav.org/ns}email-address': case '{http://sabredav.org/ns}email-address':
$users = $this->userManager->getByEmail($value); $users = $this->userManager->getByEmail($value);
$results[] = array_map(function ($user) {
return $this->principalPrefix . '/' . $user->getUID(); $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) {
}, $users); // is sharing restricted to groups only?
if ($restrictGroups !== false) {
$userGroups = $this->groupManager->getUserGroupIds($user);
if (count(array_intersect($userGroups, $restrictGroups)) === 0) {
return $carry;
}
}
$carry[] = $this->principalPrefix . '/' . $user->getUID();
return $carry;
}, []);
break; break;
default: default:
$results[] = []; $results[] = [];
break; break;
} }
} }
if (count($results) == 1) { // results is an array of arrays, so this is not the first search result
// but the results of the first searchProperty
if (count($results) === 1) {
return $results[0]; return $results[0];
} }
switch ($test) { switch ($test) {
case 'allof':
return array_intersect(...$results);
case 'anyof': case 'anyof':
return array_unique(array_merge(...$results)); return array_unique(array_merge(...$results));
}
return []; case 'allof':
default:
return array_intersect(...$results);
}
} }
/** /**
@ -229,13 +271,14 @@ class Principal implements BackendInterface {
* @return array * @return array
*/ */
function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
if (count($searchProperties) == 0) { if (count($searchProperties) === 0) {
return []; return [];
} }
switch ($prefixPath) { switch ($prefixPath) {
case 'principals/users': case 'principals/users':
return $this->searchUserPrincipals($searchProperties, $test); return $this->searchUserPrincipals($searchProperties, $test);
default: default:
return []; return [];
} }
@ -247,17 +290,43 @@ class Principal implements BackendInterface {
* @return string * @return string
*/ */
function findByUri($uri, $principalPrefix) { function findByUri($uri, $principalPrefix) {
if (substr($uri, 0, 7) === 'mailto:') { // If sharing is disabled, return null as in user not found
if ($principalPrefix === principals/users) { if (!$this->shareManager->shareApiEnabled()) {
$email = substr($uri, 7); return null;
$users = $this->userManager->getByEmail($email);
if (count($users) === 1) {
return $this->principalPrefix . '/' . $users[0]->getUID();
} }
// If sharing is restricted to group members only,
// return only members that have groups in common
$restrictGroups = false;
if ($this->shareManager->shareWithGroupMembersOnly()) {
$user = $this->userSession->getUser();
if (!$user) {
return null;
}
$restrictGroups = $this->groupManager->getUserGroupIds($user);
}
if (strpos($uri, 'mailto:') === 0) {
if ($principalPrefix === 'principals/users') {
$users = $this->userManager->getByEmail(substr($uri, 7));
if (count($users) !== 1) {
return null;
}
$user = $users[0];
if ($restrictGroups !== false) {
$userGroups = $this->groupManager->getUserGroupIds($user);
if (count(array_intersect($userGroups, $restrictGroups)) === 0) {
return null;
} }
} }
return ''; return $this->principalPrefix . '/' . $user->getUID();
}
}
return null;
} }
/** /**

View File

@ -43,11 +43,14 @@ class RootCollection extends SimpleCollection {
$logger = \OC::$server->getLogger(); $logger = \OC::$server->getLogger();
$userManager = \OC::$server->getUserManager(); $userManager = \OC::$server->getUserManager();
$groupManager = \OC::$server->getGroupManager(); $groupManager = \OC::$server->getGroupManager();
$shareManager = \OC::$server->getShareManager();
$db = \OC::$server->getDatabaseConnection(); $db = \OC::$server->getDatabaseConnection();
$dispatcher = \OC::$server->getEventDispatcher(); $dispatcher = \OC::$server->getEventDispatcher();
$userPrincipalBackend = new Principal( $userPrincipalBackend = new Principal(
$userManager, $userManager,
$groupManager $groupManager,
$shareManager,
\OC::$server->getUserSession()
); );
$groupPrincipalBackend = new GroupPrincipalBackend($groupManager); $groupPrincipalBackend = new GroupPrincipalBackend($groupManager);
// as soon as debug mode is enabled we allow listing of principals // as soon as debug mode is enabled we allow listing of principals

View File

@ -29,6 +29,9 @@ namespace OCA\DAV\Tests\unit\Connector\Sabre;
use OC\User\User; use OC\User\User;
use OCP\IGroup; use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IManager;
use \Sabre\DAV\PropPatch; use \Sabre\DAV\PropPatch;
use OCP\IUserManager; use OCP\IUserManager;
use Test\TestCase; use Test\TestCase;
@ -40,14 +43,22 @@ class PrincipalTest extends TestCase {
private $connector; private $connector;
/** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */ /** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */
private $groupManager; private $groupManager;
/** @var IManager | \PHPUnit_Framework_MockObject_MockObject */
private $shareManager;
/** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */
private $userSession;
public function setUp() { public function setUp() {
$this->userManager = $this->createMock(IUserManager::class); $this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class); $this->groupManager = $this->createMock(IGroupManager::class);
$this->shareManager = $this->createMock(IManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->connector = new \OCA\DAV\Connector\Sabre\Principal( $this->connector = new \OCA\DAV\Connector\Sabre\Principal(
$this->userManager, $this->userManager,
$this->groupManager); $this->groupManager,
$this->shareManager,
$this->userSession);
parent::setUp(); parent::setUp();
} }
@ -255,21 +266,172 @@ class PrincipalTest extends TestCase {
$this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch(array()))); $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch(array())));
} }
public function testSearchPrincipals() { public function testSearchPrincipalsWithEmptySearchProperties() {
$this->assertSame([], $this->connector->searchPrincipals('principals/users', [])); $this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
} }
public function testFindByUri() { public function testSearchPrincipalsWithWrongPrefixPath() {
$fooUser = $this->createMock(User::class); $this->assertSame([], $this->connector->searchPrincipals('principals/groups',
$fooUser ['{http://sabredav.org/ns}email-address' => 'foo']));
->expects($this->exactly(1)) }
->method('getUID')
->will($this->returnValue('foo'));
$this->userManager->expects($this->once())->method('getByEmail')->willReturn([ /**
$fooUser * @dataProvider searchPrincipalsDataProvider
]); */
$ret = $this->connector->findByUri('mailto:foo@bar.net', 'principals/users'); public function testSearchPrincipals($sharingEnabled, $groupsOnly, $result) {
$this->assertSame('principals/users/foo', $ret); $this->shareManager->expects($this->once())
->method('shareAPIEnabled')
->will($this->returnValue($sharingEnabled));
if ($sharingEnabled) {
$this->shareManager->expects($this->once())
->method('shareWithGroupMembersOnly')
->will($this->returnValue($groupsOnly));
if ($groupsOnly) {
$user = $this->createMock(IUser::class);
$this->userSession->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
$this->groupManager->expects($this->at(0))
->method('getUserGroupIds')
->with($user)
->will($this->returnValue(['group1', 'group2']));
}
} else {
$this->shareManager->expects($this->never())
->method('shareWithGroupMembersOnly');
$this->groupManager->expects($this->never())
->method($this->anything());
}
$user2 = $this->createMock(IUser::class);
$user2->method('getUID')->will($this->returnValue('user2'));
$user3 = $this->createMock(IUser::class);
$user3->method('getUID')->will($this->returnValue('user3'));
if ($sharingEnabled) {
$this->userManager->expects($this->at(0))
->method('getByEmail')
->with('user')
->will($this->returnValue([$user2, $user3]));
}
if ($sharingEnabled && $groupsOnly) {
$this->groupManager->expects($this->at(1))
->method('getUserGroupIds')
->with($user2)
->will($this->returnValue(['group1', 'group3']));
$this->groupManager->expects($this->at(2))
->method('getUserGroupIds')
->with($user3)
->will($this->returnValue(['group3', 'group4']));
}
$this->assertEquals($result, $this->connector->searchPrincipals('principals/users',
['{http://sabredav.org/ns}email-address' => 'user']));
}
public function searchPrincipalsDataProvider() {
return [
[true, false, ['principals/users/user2', 'principals/users/user3']],
[true, true, ['principals/users/user2']],
[false, false, []],
];
}
public function testFindByUriSharingApiDisabled() {
$this->shareManager->expects($this->once())
->method('shareApiEnabled')
->will($this->returnValue(false));
$this->assertEquals(null, $this->connector->findByUri('mailto:user@foo.com', 'principals/users'));
}
/**
* @dataProvider findByUriWithGroupRestrictionDataProvider
*/
public function testFindByUriWithGroupRestriction($uri, $email, $expects) {
$this->shareManager->expects($this->once())
->method('shareApiEnabled')
->will($this->returnValue(true));
$this->shareManager->expects($this->once())
->method('shareWithGroupMembersOnly')
->will($this->returnValue(true));
$user = $this->createMock(IUser::class);
$this->userSession->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
$this->groupManager->expects($this->at(0))
->method('getUserGroupIds')
->with($user)
->will($this->returnValue(['group1', 'group2']));
$user2 = $this->createMock(IUser::class);
$user2->method('getUID')->will($this->returnValue('user2'));
$user3 = $this->createMock(IUser::class);
$user3->method('getUID')->will($this->returnValue('user3'));
$this->userManager->expects($this->once())
->method('getByEmail')
->with($email)
->will($this->returnValue([$email === 'user2@foo.bar' ? $user2 : $user3]));
if ($email === 'user2@foo.bar') {
$this->groupManager->expects($this->at(1))
->method('getUserGroupIds')
->with($user2)
->will($this->returnValue(['group1', 'group3']));
} else {
$this->groupManager->expects($this->at(1))
->method('getUserGroupIds')
->with($user3)
->will($this->returnValue(['group3', 'group3']));
}
$this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
}
public function findByUriWithGroupRestrictionDataProvider() {
return [
['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
['mailto:user3@foo.bar', 'user3@foo.bar', null],
];
}
/**
* @dataProvider findByUriWithoutGroupRestrictionDataProvider
*/
public function testFindByUriWithoutGroupRestriction($uri, $email, $expects) {
$this->shareManager->expects($this->once())
->method('shareApiEnabled')
->will($this->returnValue(true));
$this->shareManager->expects($this->once())
->method('shareWithGroupMembersOnly')
->will($this->returnValue(false));
$user2 = $this->createMock(IUser::class);
$user2->method('getUID')->will($this->returnValue('user2'));
$user3 = $this->createMock(IUser::class);
$user3->method('getUID')->will($this->returnValue('user3'));
$this->userManager->expects($this->once())
->method('getByEmail')
->with($email)
->will($this->returnValue([$email === 'user2@foo.bar' ? $user2 : $user3]));
$this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
}
public function findByUriWithoutGroupRestrictionDataProvider() {
return [
['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
['mailto:user3@foo.bar', 'user3@foo.bar', 'principals/users/user3'],
];
} }
} }