namespace OCA\DAV\Connector\Sabre;
+use OCP\IUser;
use OCP\IUserManager;
use OCP\IConfig;
use \Sabre\DAV\PropPatch;
+use Sabre\HTTP\URLUtil;
class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
/** @var IConfig */
public function getPrincipalsByPrefix($prefixPath) {
$principals = [];
- if ($prefixPath === 'principals') {
+ if ($prefixPath === 'principals/users') {
foreach($this->userManager->search('') as $user) {
-
- $principal = [
- 'uri' => 'principals/' . $user->getUID(),
- '{DAV:}displayname' => $user->getUID(),
- ];
-
- $email = $this->config->getUserValue($user->getUID(), 'settings', 'email');
- if(!empty($email)) {
- $principal['{http://sabredav.org/ns}email-address'] = $email;
- }
-
- $principals[] = $principal;
+ $principals[] = $this->userToPrincipal($user);
}
}
* @return array
*/
public function getPrincipalByPath($path) {
- list($prefix, $name) = explode('/', $path);
+ $elements = explode('/', $path);
+ if ($elements[0] !== 'principals') {
+ return null;
+ }
+ if ($elements[1] !== 'users') {
+ return null;
+ }
+ $name = $elements[2];
$user = $this->userManager->get($name);
- if ($prefix === 'principals' && !is_null($user)) {
- $principal = [
- 'uri' => 'principals/' . $user->getUID(),
- '{DAV:}displayname' => $user->getUID(),
- ];
-
- $email = $this->config->getUserValue($user->getUID(), 'settings', 'email');
- if($email) {
- $principal['{http://sabredav.org/ns}email-address'] = $email;
- }
-
- return $principal;
+ if (!is_null($user)) {
+ return $this->userToPrincipal($user);
}
return null;
* @throws \Sabre\DAV\Exception
*/
public function getGroupMembership($principal) {
- list($prefix, $name) = \Sabre\HTTP\URLUtil::splitPath($principal);
+ list($prefix, $name) = URLUtil::splitPath($principal);
$group_membership = array();
- if ($prefix === 'principals') {
+ if ($prefix === 'principals/users') {
$principal = $this->getPrincipalByPath($principal);
if (!$principal) {
throw new \Sabre\DAV\Exception('Principal not found');
// TODO: for now the user principal has only its own groups
return array(
- 'principals/'.$name.'/calendar-proxy-read',
- 'principals/'.$name.'/calendar-proxy-write',
+ 'principals/users/'.$name.'/calendar-proxy-read',
+ 'principals/users/'.$name.'/calendar-proxy-write',
// The addressbook groups are not supported in Sabre,
// see http://groups.google.com/group/sabredav-discuss/browse_thread/thread/ef2fa9759d55f8c#msg_5720afc11602e753
//'principals/'.$name.'/addressbook-proxy-read',
function findByUri($uri, $principalPrefix) {
return '';
}
+
+ /**
+ * @param IUser $user
+ * @return array
+ */
+ protected function userToPrincipal($user) {
+ $userId = $user->getUID();
+ $displayName = $user->getDisplayName();
+ $principal = [
+ 'uri' => "principals/users/$userId",
+ '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName,
+ ];
+
+ $email = $this->config->getUserValue($user->getUID(), 'settings', 'email');
+ if (!empty($email)) {
+ $principal['{http://sabredav.org/ns}email-address'] = $email;
+ return $principal;
+ }
+ return $principal;
+ }
}
$fooUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$fooUser
- ->expects($this->exactly(3))
- ->method('getUID')
- ->will($this->returnValue('foo'));
+ ->expects($this->exactly(2))
+ ->method('getUID')
+ ->will($this->returnValue('foo'));
+ $fooUser
+ ->expects($this->exactly(1))
+ ->method('getDisplayName')
+ ->will($this->returnValue('Dr. Foo-Bar'));
$barUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$barUser
- ->expects($this->exactly(3))
+ ->expects($this->exactly(2))
->method('getUID')
->will($this->returnValue('bar'));
$this->userManager
$expectedResponse = [
0 => [
- 'uri' => 'principals/foo',
- '{DAV:}displayname' => 'foo'
+ 'uri' => 'principals/users/foo',
+ '{DAV:}displayname' => 'Dr. Foo-Bar'
],
1 => [
- 'uri' => 'principals/bar',
+ 'uri' => 'principals/users/bar',
'{DAV:}displayname' => 'bar',
'{http://sabredav.org/ns}email-address' => 'bar@owncloud.org'
]
];
- $response = $this->connector->getPrincipalsByPrefix('principals');
+ $response = $this->connector->getPrincipalsByPrefix('principals/users');
$this->assertSame($expectedResponse, $response);
}
->with('')
->will($this->returnValue([]));
- $response = $this->connector->getPrincipalsByPrefix('principals');
+ $response = $this->connector->getPrincipalsByPrefix('principals/users');
$this->assertSame([], $response);
}
$fooUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$fooUser
- ->expects($this->exactly(3))
+ ->expects($this->exactly(2))
->method('getUID')
->will($this->returnValue('foo'));
$this->userManager
->will($this->returnValue(''));
$expectedResponse = [
- 'uri' => 'principals/foo',
+ 'uri' => 'principals/users/foo',
'{DAV:}displayname' => 'foo'
];
- $response = $this->connector->getPrincipalByPath('principals/foo');
+ $response = $this->connector->getPrincipalByPath('principals/users/foo');
$this->assertSame($expectedResponse, $response);
}
$fooUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$fooUser
- ->expects($this->exactly(3))
+ ->expects($this->exactly(2))
->method('getUID')
->will($this->returnValue('foo'));
$this->userManager
->will($this->returnValue('foo@owncloud.org'));
$expectedResponse = [
- 'uri' => 'principals/foo',
+ 'uri' => 'principals/users/foo',
'{DAV:}displayname' => 'foo',
'{http://sabredav.org/ns}email-address' => 'foo@owncloud.org'
];
- $response = $this->connector->getPrincipalByPath('principals/foo');
+ $response = $this->connector->getPrincipalByPath('principals/users/foo');
$this->assertSame($expectedResponse, $response);
}
->with('foo')
->will($this->returnValue(null));
- $response = $this->connector->getPrincipalByPath('principals/foo');
+ $response = $this->connector->getPrincipalByPath('principals/users/foo');
$this->assertSame(null, $response);
}
$fooUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$fooUser
- ->expects($this->exactly(3))
+ ->expects($this->exactly(2))
->method('getUID')
->will($this->returnValue('foo'));
$this->userManager
->with('foo', 'settings', 'email')
->will($this->returnValue('foo@owncloud.org'));
- $response = $this->connector->getGroupMemberSet('principals/foo');
- $this->assertSame(['principals/foo'], $response);
+ $response = $this->connector->getGroupMemberSet('principals/users/foo');
+ $this->assertSame(['principals/users/foo'], $response);
}
/**
->with('foo')
->will($this->returnValue(null));
- $this->connector->getGroupMemberSet('principals/foo');
+ $this->connector->getGroupMemberSet('principals/users/foo');
}
public function testGetGroupMembership() {
$fooUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$fooUser
- ->expects($this->exactly(3))
+ ->expects($this->exactly(2))
->method('getUID')
->will($this->returnValue('foo'));
$this->userManager
->will($this->returnValue('foo@owncloud.org'));
$expectedResponse = [
- 'principals/foo/calendar-proxy-read',
- 'principals/foo/calendar-proxy-write'
+ 'principals/users/foo/calendar-proxy-read',
+ 'principals/users/foo/calendar-proxy-write'
];
- $response = $this->connector->getGroupMembership('principals/foo');
+ $response = $this->connector->getGroupMembership('principals/users/foo');
$this->assertSame($expectedResponse, $response);
}
->with('foo')
->will($this->returnValue(null));
- $this->connector->getGroupMembership('principals/foo');
+ $this->connector->getGroupMembership('principals/users/foo');
}
/**
* @expectedExceptionMessage Setting members of the group is not supported yet
*/
public function testSetGroupMembership() {
- $this->connector->setGroupMemberSet('principals/foo', ['foo']);
+ $this->connector->setGroupMemberSet('principals/users/foo', ['foo']);
}
public function testUpdatePrincipal() {
}
public function testSearchPrincipals() {
- $this->assertSame([], $this->connector->searchPrincipals('principals', []));
+ $this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
}
}