]> source.dussan.org Git - nextcloud-server.git/commitdiff
Better displaynames for shared address books 936/head
authorJoas Schilling <coding@schilljs.com>
Thu, 18 Aug 2016 13:14:04 +0000 (15:14 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Wed, 24 Aug 2016 06:50:25 +0000 (08:50 +0200)
apps/dav/appinfo/v1/carddav.php
apps/dav/lib/AppInfo/Application.php
apps/dav/lib/CardDAV/CardDavBackend.php
apps/dav/lib/RootCollection.php
apps/dav/tests/unit/CardDAV/CardDavBackendTest.php

index 005946830ae388c0085c116a42448efb0ac42549..e379707807f98b45a03a75fa6b619d9ad2e06191 100644 (file)
@@ -48,7 +48,7 @@ $principalBackend = new Principal(
        'principals/'
 );
 $db = \OC::$server->getDatabaseConnection();
-$cardDavBackend = new CardDavBackend($db, $principalBackend);
+$cardDavBackend = new CardDavBackend($db, $principalBackend, \OC::$server->getUserManager());
 
 $debugging = \OC::$server->getConfig()->getSystemValue('debug', false);
 
index e1a01f1236127a781009eb40b42aa3f79a013055..17145847fa1387c58e80ea92dc41d8dc0fd6564f 100644 (file)
@@ -87,7 +87,7 @@ class Application extends App {
                                $c->getServer()->getUserManager(),
                                $c->getServer()->getGroupManager()
                        );
-                       return new CardDavBackend($db, $principal, $dispatcher);
+                       return new CardDavBackend($db, $principal, $c->getServer()->getUserManager(), $dispatcher);
                });
 
                $container->registerService('CalDavBackend', function($c) {
index f8552f1323e113cdafc20382de0c1bad01f257e4..f6d80dfe701abe558d57d5a98b855a0a7544b8e0 100644 (file)
@@ -33,6 +33,8 @@ use OCP\DB\QueryBuilder\IQueryBuilder;
 use OCA\DAV\DAV\Sharing\Backend;
 use OCA\DAV\DAV\Sharing\IShareable;
 use OCP\IDBConnection;
+use OCP\IUser;
+use OCP\IUserManager;
 use PDO;
 use Sabre\CardDAV\Backend\BackendInterface;
 use Sabre\CardDAV\Backend\SyncSupport;
@@ -66,6 +68,14 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                        'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME',
                        'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD');
 
+       /**
+        * @var string[] Map of uid => display name
+        */
+       protected $userDisplayNames;
+
+       /** @var IUserManager */
+       private $userManager;
+
        /** @var EventDispatcherInterface */
        private $dispatcher;
 
@@ -74,13 +84,16 @@ class CardDavBackend implements BackendInterface, SyncSupport {
         *
         * @param IDBConnection $db
         * @param Principal $principalBackend
+        * @param IUserManager $userManager
         * @param EventDispatcherInterface $dispatcher
         */
        public function __construct(IDBConnection $db,
                                                                Principal $principalBackend,
+                                                               IUserManager $userManager,
                                                                EventDispatcherInterface $dispatcher = null) {
                $this->db = $db;
                $this->principalBackend = $principalBackend;
+               $this->userManager = $userManager;
                $this->dispatcher = $dispatcher;
                $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook');
        }
@@ -143,7 +156,7 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                while($row = $result->fetch()) {
                        list(, $name) = URLUtil::splitPath($row['principaluri']);
                        $uri = $row['uri'] . '_shared_by_' . $name;
-                       $displayName = $row['displayname'] . "($name)";
+                       $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')';
                        if (!isset($addressBooks[$row['id']])) {
                                $addressBooks[$row['id']] = [
                                        'id'  => $row['id'],
@@ -163,6 +176,20 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                return array_values($addressBooks);
        }
 
+       private function getUserDisplayName($uid) {
+               if (!isset($this->userDisplayNames[$uid])) {
+                       $user = $this->userManager->get($uid);
+
+                       if ($user instanceof IUser) {
+                               $this->userDisplayNames[$uid] = $user->getDisplayName();
+                       } else {
+                               $this->userDisplayNames[$uid] = $uid;
+                       }
+               }
+
+               return $this->userDisplayNames[$uid];
+       }
+
        /**
         * @param int $addressBookId
         */
index de3f7e2a857a606e4e38af831e3ab329fbaf206d..974d08bc34f87017cf541c84f5623e2990b9ed32 100644 (file)
@@ -83,11 +83,11 @@ class RootCollection extends SimpleCollection {
                        \OC::$server->getLogger()
                );
 
-               $usersCardDavBackend = new CardDavBackend($db, $userPrincipalBackend, $dispatcher);
+               $usersCardDavBackend = new CardDavBackend($db, $userPrincipalBackend, \OC::$server->getUserManager(), $dispatcher);
                $usersAddressBookRoot = new AddressBookRoot($userPrincipalBackend, $usersCardDavBackend, 'principals/users');
                $usersAddressBookRoot->disableListing = $disableListing;
 
-               $systemCardDavBackend = new CardDavBackend($db, $userPrincipalBackend, $dispatcher);
+               $systemCardDavBackend = new CardDavBackend($db, $userPrincipalBackend, \OC::$server->getUserManager(), $dispatcher);
                $systemAddressBookRoot = new AddressBookRoot(new SystemPrincipalBackend(), $systemCardDavBackend, 'principals/system');
                $systemAddressBookRoot->disableListing = $disableListing;
 
index f74d12f665804dd707ad64d9e8edc13d6d3fc5d7..bbeadf81277344dea685560a4cb6b91d12354211 100644 (file)
@@ -53,6 +53,9 @@ class CardDavBackendTest extends TestCase {
        /** @var Principal | \PHPUnit_Framework_MockObject_MockObject */
        private $principal;
 
+       /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */
+       private $userManager;
+
        /** @var  IDBConnection */
        private $db;
 
@@ -69,6 +72,9 @@ class CardDavBackendTest extends TestCase {
        public function setUp() {
                parent::setUp();
 
+               $this->userManager = $this->getMockBuilder('OCP\IUserManager')
+                       ->disableOriginalConstructor()
+                       ->getMock();
                $this->principal = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal')
                        ->disableOriginalConstructor()
                        ->setMethods(['getPrincipalByPath', 'getGroupMembership'])
@@ -83,7 +89,7 @@ class CardDavBackendTest extends TestCase {
 
                $this->db = \OC::$server->getDatabaseConnection();
 
-               $this->backend = new CardDavBackend($this->db, $this->principal, null);
+               $this->backend = new CardDavBackend($this->db, $this->principal, $this->userManager, null);
 
                // start every test with a empty cards_properties and cards table
                $query = $this->db->getQueryBuilder();
@@ -161,7 +167,7 @@ class CardDavBackendTest extends TestCase {
 
                /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $backend */
                $backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
-                               ->setConstructorArgs([$this->db, $this->principal, null])
+                               ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
                                ->setMethods(['updateProperties', 'purgeProperties'])->getMock();
 
                // create a new address book
@@ -207,7 +213,7 @@ class CardDavBackendTest extends TestCase {
        public function testMultiCard() {
 
                $this->backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
-                       ->setConstructorArgs([$this->db, $this->principal, null])
+                       ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
                        ->setMethods(['updateProperties'])->getMock();
 
                // create a new address book
@@ -254,7 +260,7 @@ class CardDavBackendTest extends TestCase {
        public function testDeleteWithoutCard() {
 
                $this->backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
-                       ->setConstructorArgs([$this->db, $this->principal, null])
+                       ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
                        ->setMethods([
                                'getCardId',
                                'addChange',
@@ -295,7 +301,7 @@ class CardDavBackendTest extends TestCase {
        public function testSyncSupport() {
 
                $this->backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
-                       ->setConstructorArgs([$this->db, $this->principal, null])
+                       ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
                        ->setMethods(['updateProperties'])->getMock();
 
                // create a new address book
@@ -353,7 +359,7 @@ class CardDavBackendTest extends TestCase {
                $cardId = 2;
 
                $backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
-                       ->setConstructorArgs([$this->db, $this->principal, null])
+                       ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
                        ->setMethods(['getCardId'])->getMock();
 
                $backend->expects($this->any())->method('getCardId')->willReturn($cardId);