]> source.dussan.org Git - nextcloud-server.git/commitdiff
provide room / resource properties as principal properties
authorGeorg Ehrke <developer@georgehrke.com>
Fri, 26 Jul 2019 12:50:16 +0000 (14:50 +0200)
committerGeorg Ehrke <developer@georgehrke.com>
Thu, 1 Aug 2019 08:08:56 +0000 (10:08 +0200)
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php
apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php
apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php

index 90d147f6741ecd3441e2f0acce3fac97b7e075dd..bc96041d915fa2724b5555c107ed727cb3da0dc0 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
+ * @copyright 2019, Georg Ehrke <oc.list@georgehrke.com>
  *
  * @author Georg Ehrke <oc.list@georgehrke.com>
  *
@@ -50,6 +50,12 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
        /** @var string */
        private $dbTableName;
 
+       /** @var string */
+       private $dbMetaDataTableName;
+
+       /** @var string */
+       private $dbForeignKeyName;
+
        /** @var string */
        private $cuType;
 
@@ -74,7 +80,9 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
                $this->groupManager = $groupManager;
                $this->logger = $logger;
                $this->principalPrefix = $principalPrefix;
-               $this->dbTableName = 'calendar_' . $dbPrefix;
+               $this->dbTableName = 'calendar_' . $dbPrefix . 's';
+               $this->dbMetaDataTableName = $this->dbTableName . '_md';
+               $this->dbForeignKeyName = $dbPrefix . '_id';
                $this->cuType = $cuType;
        }
 
@@ -100,8 +108,31 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
                                ->from($this->dbTableName);
                        $stmt = $query->execute();
 
+                       $metaDataQuery = $this->db->getQueryBuilder();
+                       $metaDataQuery->select([$this->dbForeignKeyName, 'key', 'value'])
+                               ->from($this->dbMetaDataTableName);
+                       $metaDataStmt = $metaDataQuery->execute();
+                       $metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC);
+
+                       $metaDataById = [];
+                       foreach($metaDataRows as $metaDataRow) {
+                               if (!isset($metaDataById[$metaDataRow[$this->dbForeignKeyName]])) {
+                                       $metaDataById[$metaDataRow[$this->dbForeignKeyName]] = [];
+                               }
+
+                               $metaDataById[$metaDataRow[$this->dbForeignKeyName]][$metaDataRow['key']] =
+                                       $metaDataRow['value'];
+                       }
+
                        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
-                               $principals[] = $this->rowToPrincipal($row);
+                               $id = $row['id'];
+
+                               if (isset($metaDataById[$id])) {
+                                       $principals[] = $this->rowToPrincipal($row, $metaDataById[$id]);
+                               } else {
+                                       $principals[] = $this->rowToPrincipal($row);
+                               }
+
                        }
 
                        $stmt->closeCursor();
@@ -138,7 +169,19 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
                        return null;
                }
 
-               return $this->rowToPrincipal($row);
+               $metaDataQuery = $this->db->getQueryBuilder();
+               $metaDataQuery->select(['key', 'value'])
+                       ->from($this->dbMetaDataTableName)
+                       ->where($metaDataQuery->expr()->eq($this->dbForeignKeyName, $metaDataQuery->createNamedParameter($row['id'])));
+               $metaDataStmt = $metaDataQuery->execute();
+               $metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC);
+               $metadata = [];
+
+               foreach($metaDataRows as $metaDataRow) {
+                       $metadata[$metaDataRow['key']] = $metaDataRow['value'];
+               }
+
+               return $this->rowToPrincipal($row, $metadata);
        }
 
        /**
@@ -338,14 +381,18 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
 
        /**
         * convert database row to principal
+        *
+        * @param String[] $row
+        * @param String[] $metadata
+        * @return Array
         */
-       private function rowToPrincipal($row) {
-               return [
+       private function rowToPrincipal(array $row, array $metadata=[]):array {
+               return array_merge([
                        'uri' => $this->principalPrefix . '/' . $row['backend_id'] . '-' . $row['resource_id'],
                        '{DAV:}displayname' => $row['displayname'],
                        '{http://sabredav.org/ns}email-address' => $row['email'],
                        '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->cuType,
-               ];
+               ], $metadata);
        }
 
        /**
@@ -353,7 +400,7 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
         * @param $userGroups
         * @return bool
         */
-       private function isAllowedToAccessResource($row, $userGroups) {
+       private function isAllowedToAccessResource(array $row, array $userGroups):bool {
                if (!isset($row['group_restrictions']) ||
                        $row['group_restrictions'] === null ||
                        $row['group_restrictions'] === '') {
index f8f10e78f9411f368d3b9324eb043eafd56f2260..0f6e6e7b4fd4ea2bc039dd2fc170f6a168456f72 100644 (file)
@@ -40,6 +40,6 @@ class ResourcePrincipalBackend extends AbstractPrincipalBackend {
                                                                IGroupManager $groupManager,
                                                                ILogger $logger) {
                parent::__construct($dbConnection, $userSession, $groupManager, $logger,
-                       'principals/calendar-resources', 'resources', 'RESOURCE');
+                       'principals/calendar-resources', 'resource', 'RESOURCE');
        }
 }
index 3059ed80ea6c135ee92ebe335f177de92bdd309a..68a344aa0cae72f14c1204545c4004fe2eef49a1 100644 (file)
@@ -40,6 +40,6 @@ class RoomPrincipalBackend extends AbstractPrincipalBackend {
                                                                IGroupManager $groupManager,
                                                                ILogger $logger) {
                parent::__construct($dbConnection, $userSession, $groupManager, $logger,
-                       'principals/calendar-rooms', 'rooms', 'ROOM');
+                       'principals/calendar-rooms', 'room', 'ROOM');
        }
 }