From 54fb0f4f045ad1fa598c02b1d641b3695a9779e8 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Fri, 26 Jul 2019 14:50:16 +0200 Subject: [PATCH] provide room / resource properties as principal properties Signed-off-by: Georg Ehrke --- .../AbstractPrincipalBackend.php | 63 ++++++++++++++++--- .../ResourcePrincipalBackend.php | 2 +- .../ResourceBooking/RoomPrincipalBackend.php | 2 +- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php b/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php index 90d147f6741..bc96041d915 100644 --- a/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php +++ b/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php @@ -1,6 +1,6 @@ + * @copyright 2019, Georg Ehrke * * @author Georg Ehrke * @@ -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'] === '') { diff --git a/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php b/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php index f8f10e78f94..0f6e6e7b4fd 100644 --- a/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php +++ b/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php @@ -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'); } } diff --git a/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php b/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php index 3059ed80ea6..68a344aa0ca 100644 --- a/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php +++ b/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php @@ -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'); } } -- 2.39.5