]> source.dussan.org Git - nextcloud-server.git/commitdiff
Users are available under it's own principal resource named 'principals/users' this...
authorThomas Müller <thomas.mueller@tmit.eu>
Tue, 24 Nov 2015 10:15:31 +0000 (11:15 +0100)
committerThomas Müller <thomas.mueller@tmit.eu>
Wed, 25 Nov 2015 21:23:34 +0000 (22:23 +0100)
apps/dav/command/createaddressbook.php
apps/dav/command/createcalendar.php
apps/dav/lib/connector/sabre/auth.php
apps/dav/lib/connector/sabre/principal.php
apps/dav/lib/rootcollection.php
apps/dav/lib/server.php
apps/dav/tests/unit/connector/sabre/auth.php
apps/dav/tests/unit/connector/sabre/principal.php

index 371ea44121d02463f94d0dd0d5956c1ea96f8633..ea89e7aa0a8eb47e7ab9b5ebf3ac1bda819fc772 100644 (file)
@@ -58,6 +58,6 @@ class CreateAddressBook extends Command {
 
                $name = $input->getArgument('name');
                $carddav = new CardDavBackend($this->dbConnection, $principalBackend);
-               $carddav->createAddressBook("principals/$user", $name, []);
+               $carddav->createAddressBook("principals/users/$user", $name, []);
        }
 }
index da4f248e51d3d3b49114ef3630022ef05d734022..5e7b17dae66ebac32dcaa2d190d25730eacff075 100644 (file)
@@ -47,6 +47,6 @@ class CreateCalendar extends Command {
                }
                $name = $input->getArgument('name');
                $caldav = new CalDavBackend($this->dbConnection);
-               $caldav->createCalendar("principals/$user", $name, []);
+               $caldav->createCalendar("principals/users/$user", $name, []);
        }
 }
index 655152a2cc17629ee3351719174f8d41e8464b84..803db78ecd7de9fd1635b97aadddcead38bcd488 100644 (file)
@@ -54,6 +54,7 @@ class Auth extends AbstractBasic {
                                                                IUserSession $userSession) {
                $this->session = $session;
                $this->userSession = $userSession;
+               $this->principalPrefix = 'principals/users/';
        }
 
        /**
index 7fb14c031f99ef1c3db1ca407b86429268014de3..cd3b14b68412c2a37b1a052ec991189c87989a3f 100644 (file)
 
 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 */
@@ -66,20 +68,9 @@ class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
        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);
                        }
                }
 
@@ -95,21 +86,18 @@ class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
         * @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;
@@ -140,10 +128,10 @@ class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
         * @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');
@@ -151,8 +139,8 @@ class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
 
                        // 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',
@@ -202,4 +190,24 @@ class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
        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;
+       }
 }
index 672e0a98684aaf460a2f77f74ad24ae193c123e8..c0a37a1de9d914207a53fcd0961fa858b5993e15 100644 (file)
@@ -23,9 +23,9 @@ class RootCollection extends SimpleCollection {
                $disableListing = !$config->getSystemValue('debug', false);
 
                // setup the first level of the dav tree
-               $principalCollection = new Collection($principalBackend);
+               $principalCollection = new Collection($principalBackend, 'principals/users');
                $principalCollection->disableListing = $disableListing;
-               $filesCollection = new Files\RootCollection($principalBackend);
+               $filesCollection = new Files\RootCollection($principalBackend, 'principals/users');
                $filesCollection->disableListing = $disableListing;
                $caldavBackend = new CalDavBackend($db);
                $calendarRoot = new CalendarRoot($principalBackend, $caldavBackend);
@@ -37,7 +37,7 @@ class RootCollection extends SimpleCollection {
                $addressBookRoot->disableListing = $disableListing;
 
                $children = [
-                               $principalCollection,
+                               new SimpleCollection('principals', [$principalCollection]),
                                $filesCollection,
                                $calendarRoot,
                                $addressBookRoot,
index 587c0091e237269f3aa7d6de5b8845b437256d5f..ffdb917085e118d8680aabe715754542d5532aad 100644 (file)
@@ -41,9 +41,13 @@ class Server {
                $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ListenerPlugin($dispatcher));
                $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
 
+               // acl
+               $acl = new \Sabre\DAVACL\Plugin();
+               $acl->defaultUsernamePath = 'principals/users';
+               $this->server->addPlugin($acl);
+
                // calendar plugins
                $this->server->addPlugin(new \Sabre\CalDAV\Plugin());
-               $this->server->addPlugin(new \Sabre\DAVACL\Plugin());
                $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
                $senderEmail = \OCP\Util::getDefaultEmailAddress('no-reply');
                $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
index 595bd441617afcb503767c97458e0ac78e0aff89..47dd237b7610ee4255ab641ff4490bb4080ea7a7 100644 (file)
@@ -279,7 +279,7 @@ class Auth extends TestCase {
                        ->method('close');
 
                $response = $this->auth->check($request, $response);
-               $this->assertEquals([true, 'principals/MyWrongDavUser'], $response);
+               $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response);
        }
 
        public function testAuthenticateNoBasicAuthenticateHeadersProvided() {
@@ -353,7 +353,7 @@ class Auth extends TestCase {
                        ->method('getUser')
                        ->will($this->returnValue($user));
                $response = $this->auth->check($server->httpRequest, $server->httpResponse);
-               $this->assertEquals([true, 'principals/username'], $response);
+               $this->assertEquals([true, 'principals/users/username'], $response);
        }
 
        public function testAuthenticateInvalidCredentials() {
index 2fbab124fb7e1cd4c0813748d1bdc90cd158b84f..1d7721c546dd2172450d5d9d940fc6bc44247d76 100644 (file)
@@ -41,13 +41,17 @@ class Principal extends \Test\TestCase {
                $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
@@ -68,16 +72,16 @@ class Principal extends \Test\TestCase {
 
                $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);
        }
 
@@ -88,7 +92,7 @@ class Principal extends \Test\TestCase {
                        ->with('')
                        ->will($this->returnValue([]));
 
-               $response = $this->connector->getPrincipalsByPrefix('principals');
+               $response = $this->connector->getPrincipalsByPrefix('principals/users');
                $this->assertSame([], $response);
        }
 
@@ -96,7 +100,7 @@ class Principal extends \Test\TestCase {
                $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
@@ -111,10 +115,10 @@ class Principal extends \Test\TestCase {
                        ->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);
        }
 
@@ -122,7 +126,7 @@ class Principal extends \Test\TestCase {
                $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
@@ -137,11 +141,11 @@ class Principal extends \Test\TestCase {
                        ->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);
        }
 
@@ -152,7 +156,7 @@ class Principal extends \Test\TestCase {
                        ->with('foo')
                        ->will($this->returnValue(null));
 
-               $response = $this->connector->getPrincipalByPath('principals/foo');
+               $response = $this->connector->getPrincipalByPath('principals/users/foo');
                $this->assertSame(null, $response);
        }
 
@@ -160,7 +164,7 @@ class Principal extends \Test\TestCase {
                $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
@@ -174,8 +178,8 @@ class Principal extends \Test\TestCase {
                        ->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);
        }
 
        /**
@@ -189,14 +193,14 @@ class Principal extends \Test\TestCase {
                        ->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
@@ -211,10 +215,10 @@ class Principal extends \Test\TestCase {
                        ->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);
        }
 
@@ -229,7 +233,7 @@ class Principal extends \Test\TestCase {
                        ->with('foo')
                        ->will($this->returnValue(null));
 
-               $this->connector->getGroupMembership('principals/foo');
+               $this->connector->getGroupMembership('principals/users/foo');
        }
 
        /**
@@ -237,7 +241,7 @@ class Principal extends \Test\TestCase {
         * @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() {
@@ -245,6 +249,6 @@ class Principal extends \Test\TestCase {
        }
 
        public function testSearchPrincipals() {
-               $this->assertSame([], $this->connector->searchPrincipals('principals', []));
+               $this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
        }
 }