Adding group principals to new dav endpoint
parent
a0345b9465
commit
f9c08112da
@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\DAV\DAV;
|
||||
|
||||
use OCP\IGroup;
|
||||
use OCP\IGroupManager;
|
||||
use Sabre\DAV\Exception;
|
||||
use \Sabre\DAV\PropPatch;
|
||||
use Sabre\DAVACL\PrincipalBackend\BackendInterface;
|
||||
|
||||
class GroupPrincipalBackend implements BackendInterface {
|
||||
|
||||
const PRINCIPAL_PREFIX = 'principals/groups';
|
||||
|
||||
/** @var IGroupManager */
|
||||
private $groupManager;
|
||||
|
||||
/**
|
||||
* @param IGroupManager $IGroupManager
|
||||
*/
|
||||
public function __construct(IGroupManager $IGroupManager) {
|
||||
$this->groupManager = $IGroupManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of principals based on a prefix.
|
||||
*
|
||||
* This prefix will often contain something like 'principals'. You are only
|
||||
* expected to return principals that are in this base path.
|
||||
*
|
||||
* You are expected to return at least a 'uri' for every user, you can
|
||||
* return any additional properties if you wish so. Common properties are:
|
||||
* {DAV:}displayname
|
||||
*
|
||||
* @param string $prefixPath
|
||||
* @return string[]
|
||||
*/
|
||||
public function getPrincipalsByPrefix($prefixPath) {
|
||||
$principals = [];
|
||||
|
||||
if ($prefixPath === self::PRINCIPAL_PREFIX) {
|
||||
foreach($this->groupManager->search('') as $user) {
|
||||
$principals[] = $this->groupToPrincipal($user);
|
||||
}
|
||||
}
|
||||
|
||||
return $principals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specific principal, specified by it's path.
|
||||
* The returned structure should be the exact same as from
|
||||
* getPrincipalsByPrefix.
|
||||
*
|
||||
* @param string $path
|
||||
* @return array
|
||||
*/
|
||||
public function getPrincipalByPath($path) {
|
||||
$elements = explode('/', $path);
|
||||
if ($elements[0] !== 'principals') {
|
||||
return null;
|
||||
}
|
||||
if ($elements[1] !== 'groups') {
|
||||
return null;
|
||||
}
|
||||
$name = $elements[2];
|
||||
$user = $this->groupManager->get($name);
|
||||
|
||||
if (!is_null($user)) {
|
||||
return $this->groupToPrincipal($user);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of members for a group-principal
|
||||
*
|
||||
* @param string $principal
|
||||
* @return string[]
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getGroupMemberSet($principal) {
|
||||
// TODO: implement if we want that
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of groups a principal is a member of
|
||||
*
|
||||
* @param string $principal
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getGroupMembership($principal) {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the list of group members for a group principal.
|
||||
*
|
||||
* The principals should be passed as a list of uri's.
|
||||
*
|
||||
* @param string $principal
|
||||
* @param string[] $members
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setGroupMemberSet($principal, array $members) {
|
||||
throw new Exception('Setting members of the group is not supported yet');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param PropPatch $propPatch
|
||||
* @return int
|
||||
*/
|
||||
function updatePrincipal($path, PropPatch $propPatch) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefixPath
|
||||
* @param array $searchProperties
|
||||
* @param string $test
|
||||
* @return array
|
||||
*/
|
||||
function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param string $principalPrefix
|
||||
* @return string
|
||||
*/
|
||||
function findByUri($uri, $principalPrefix) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IGroup $group
|
||||
* @return array
|
||||
*/
|
||||
protected function groupToPrincipal($group) {
|
||||
$groupId = $group->getGID();
|
||||
$principal = [
|
||||
'uri' => "principals/groups/$groupId",
|
||||
'{DAV:}displayname' => $groupId,
|
||||
];
|
||||
|
||||
return $principal;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\DAV\Tests\Unit\DAV;
|
||||
|
||||
use OCA\DAV\DAV\GroupPrincipalBackend;
|
||||
use OCP\IGroupManager;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
use \Sabre\DAV\PropPatch;
|
||||
|
||||
class GroupPrincipalTest extends \Test\TestCase {
|
||||
|
||||
/** @var IGroupManager | PHPUnit_Framework_MockObject_MockObject */
|
||||
private $groupManager;
|
||||
|
||||
/** @var GroupPrincipalBackend */
|
||||
private $connector;
|
||||
|
||||
public function setUp() {
|
||||
$this->groupManager = $this->getMockBuilder('\OCP\IGroupManager')
|
||||
->disableOriginalConstructor()->getMock();
|
||||
|
||||
$this->connector = new GroupPrincipalBackend($this->groupManager);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testGetPrincipalsByPrefixWithoutPrefix() {
|
||||
$response = $this->connector->getPrincipalsByPrefix('');
|
||||
$this->assertSame([], $response);
|
||||
}
|
||||
|
||||
public function testGetPrincipalsByPrefixWithUsers() {
|
||||
$group1 = $this->mockGroup('foo');
|
||||
$group2 = $this->mockGroup('bar');
|
||||
$this->groupManager
|
||||
->expects($this->once())
|
||||
->method('search')
|
||||
->with('')
|
||||
->will($this->returnValue([$group1, $group2]));
|
||||
|
||||
$expectedResponse = [
|
||||
0 => [
|
||||
'uri' => 'principals/groups/foo',
|
||||
'{DAV:}displayname' => 'foo'
|
||||
],
|
||||
1 => [
|
||||
'uri' => 'principals/groups/bar',
|
||||
'{DAV:}displayname' => 'bar',
|
||||
]
|
||||
];
|
||||
$response = $this->connector->getPrincipalsByPrefix('principals/groups');
|
||||
$this->assertSame($expectedResponse, $response);
|
||||
}
|
||||
|
||||
public function testGetPrincipalsByPrefixEmpty() {
|
||||
$this->groupManager
|
||||
->expects($this->once())
|
||||
->method('search')
|
||||
->with('')
|
||||
->will($this->returnValue([]));
|
||||
|
||||
$response = $this->connector->getPrincipalsByPrefix('principals/groups');
|
||||
$this->assertSame([], $response);
|
||||
}
|
||||
|
||||
public function testGetPrincipalsByPathWithoutMail() {
|
||||
$group1 = $this->mockGroup('foo');
|
||||
$this->groupManager
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->will($this->returnValue($group1));
|
||||
|
||||
$expectedResponse = [
|
||||
'uri' => 'principals/groups/foo',
|
||||
'{DAV:}displayname' => 'foo'
|
||||
];
|
||||
$response = $this->connector->getPrincipalByPath('principals/groups/foo');
|
||||
$this->assertSame($expectedResponse, $response);
|
||||
}
|
||||
|
||||
public function testGetPrincipalsByPathWithMail() {
|
||||
$fooUser = $this->mockGroup('foo');
|
||||
$this->groupManager
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->will($this->returnValue($fooUser));
|
||||
|
||||
$expectedResponse = [
|
||||
'uri' => 'principals/groups/foo',
|
||||
'{DAV:}displayname' => 'foo',
|
||||
];
|
||||
$response = $this->connector->getPrincipalByPath('principals/groups/foo');
|
||||
$this->assertSame($expectedResponse, $response);
|
||||
}
|
||||
|
||||
public function testGetPrincipalsByPathEmpty() {
|
||||
$this->groupManager
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->will($this->returnValue(null));
|
||||
|
||||
$response = $this->connector->getPrincipalByPath('principals/groups/foo');
|
||||
$this->assertSame(null, $response);
|
||||
}
|
||||
|
||||
public function testGetGroupMemberSet() {
|
||||
$response = $this->connector->getGroupMemberSet('principals/groups/foo');
|
||||
$this->assertSame([], $response);
|
||||
}
|
||||
|
||||
public function testGetGroupMembership() {
|
||||
$response = $this->connector->getGroupMembership('principals/groups/foo');
|
||||
$this->assertSame([], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Sabre\DAV\Exception
|
||||
* @expectedExceptionMessage Setting members of the group is not supported yet
|
||||
*/
|
||||
public function testSetGroupMembership() {
|
||||
$this->connector->setGroupMemberSet('principals/groups/foo', ['foo']);
|
||||
}
|
||||
|
||||
public function testUpdatePrincipal() {
|
||||
$this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch(array())));
|
||||
}
|
||||
|
||||
public function testSearchPrincipals() {
|
||||
$this->assertSame([], $this->connector->searchPrincipals('principals/groups', []));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private function mockGroup($gid) {
|
||||
$fooUser = $this->getMockBuilder('\OC\Group\Group')
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$fooUser
|
||||
->expects($this->exactly(1))
|
||||
->method('getGID')
|
||||
->will($this->returnValue($gid));
|
||||
return $fooUser;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue