aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Db
diff options
context:
space:
mode:
Diffstat (limited to 'apps/dav/lib/Db')
-rw-r--r--apps/dav/lib/Db/Absence.php99
-rw-r--r--apps/dav/lib/Db/AbsenceMapper.php89
-rw-r--r--apps/dav/lib/Db/Direct.php30
-rw-r--r--apps/dav/lib/Db/DirectMapper.php23
-rw-r--r--apps/dav/lib/Db/Property.php37
-rw-r--r--apps/dav/lib/Db/PropertyMapper.php55
6 files changed, 290 insertions, 43 deletions
diff --git a/apps/dav/lib/Db/Absence.php b/apps/dav/lib/Db/Absence.php
new file mode 100644
index 00000000000..d7cd46087c3
--- /dev/null
+++ b/apps/dav/lib/Db/Absence.php
@@ -0,0 +1,99 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\DAV\Db;
+
+use DateTime;
+use Exception;
+use InvalidArgumentException;
+use JsonSerializable;
+use OC\User\OutOfOfficeData;
+use OCP\AppFramework\Db\Entity;
+use OCP\IUser;
+use OCP\User\IOutOfOfficeData;
+
+/**
+ * @method string getUserId()
+ * @method void setUserId(string $userId)
+ * @method string getFirstDay()
+ * @method void setFirstDay(string $firstDay)
+ * @method string getLastDay()
+ * @method void setLastDay(string $lastDay)
+ * @method string getStatus()
+ * @method void setStatus(string $status)
+ * @method string getMessage()
+ * @method void setMessage(string $message)
+ * @method string getReplacementUserId()
+ * @method void setReplacementUserId(?string $replacementUserId)
+ * @method string getReplacementUserDisplayName()
+ * @method void setReplacementUserDisplayName(?string $replacementUserDisplayName)
+ */
+class Absence extends Entity implements JsonSerializable {
+ protected string $userId = '';
+
+ /** Inclusive, formatted as YYYY-MM-DD */
+ protected string $firstDay = '';
+
+ /** Inclusive, formatted as YYYY-MM-DD */
+ protected string $lastDay = '';
+
+ protected string $status = '';
+
+ protected string $message = '';
+
+ protected ?string $replacementUserId = null;
+
+ protected ?string $replacementUserDisplayName = null;
+
+ public function __construct() {
+ $this->addType('userId', 'string');
+ $this->addType('firstDay', 'string');
+ $this->addType('lastDay', 'string');
+ $this->addType('status', 'string');
+ $this->addType('message', 'string');
+ $this->addType('replacementUserId', 'string');
+ $this->addType('replacementUserDisplayName', 'string');
+ }
+
+ public function toOutOufOfficeData(IUser $user, string $timezone): IOutOfOfficeData {
+ if ($user->getUID() !== $this->getUserId()) {
+ throw new InvalidArgumentException("The user doesn't match the user id of this absence! Expected " . $this->getUserId() . ', got ' . $user->getUID());
+ }
+ if ($this->getId() === null) {
+ throw new Exception('Creating out-of-office data without ID');
+ }
+
+ $tz = new \DateTimeZone($timezone);
+ $startDate = new DateTime($this->getFirstDay(), $tz);
+ $endDate = new DateTime($this->getLastDay(), $tz);
+ $endDate->setTime(23, 59);
+ return new OutOfOfficeData(
+ (string)$this->getId(),
+ $user,
+ $startDate->getTimestamp(),
+ $endDate->getTimestamp(),
+ $this->getStatus(),
+ $this->getMessage(),
+ $this->getReplacementUserId(),
+ $this->getReplacementUserDisplayName(),
+ );
+ }
+
+ public function jsonSerialize(): array {
+ return [
+ 'userId' => $this->userId,
+ 'firstDay' => $this->firstDay,
+ 'lastDay' => $this->lastDay,
+ 'status' => $this->status,
+ 'message' => $this->message,
+ 'replacementUserId' => $this->replacementUserId,
+ 'replacementUserDisplayName' => $this->replacementUserDisplayName,
+ ];
+ }
+}
diff --git a/apps/dav/lib/Db/AbsenceMapper.php b/apps/dav/lib/Db/AbsenceMapper.php
new file mode 100644
index 00000000000..1214a123236
--- /dev/null
+++ b/apps/dav/lib/Db/AbsenceMapper.php
@@ -0,0 +1,89 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\DAV\Db;
+
+use OCP\AppFramework\Db\DoesNotExistException;
+use OCP\AppFramework\Db\MultipleObjectsReturnedException;
+use OCP\AppFramework\Db\QBMapper;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\IDBConnection;
+
+/**
+ * @template-extends QBMapper<Absence>
+ */
+class AbsenceMapper extends QBMapper {
+ public function __construct(IDBConnection $db) {
+ parent::__construct($db, 'dav_absence', Absence::class);
+ }
+
+ /**
+ * @throws DoesNotExistException
+ * @throws \OCP\DB\Exception
+ */
+ public function findById(int $id): Absence {
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('*')
+ ->from($this->getTableName())
+ ->where($qb->expr()->eq(
+ 'id',
+ $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
+ IQueryBuilder::PARAM_INT),
+ );
+ try {
+ return $this->findEntity($qb);
+ } catch (MultipleObjectsReturnedException $e) {
+ // Won't happen as id is the primary key
+ throw new \RuntimeException(
+ 'The impossible has happened! The query returned multiple absence settings for one user.',
+ 0,
+ $e,
+ );
+ }
+ }
+
+ /**
+ * @throws DoesNotExistException
+ * @throws \OCP\DB\Exception
+ */
+ public function findByUserId(string $userId): Absence {
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('*')
+ ->from($this->getTableName())
+ ->where($qb->expr()->eq(
+ 'user_id',
+ $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
+ IQueryBuilder::PARAM_STR),
+ );
+ try {
+ return $this->findEntity($qb);
+ } catch (MultipleObjectsReturnedException $e) {
+ // Won't happen as there is a unique index on user_id
+ throw new \RuntimeException(
+ 'The impossible has happened! The query returned multiple absence settings for one user.',
+ 0,
+ $e,
+ );
+ }
+ }
+
+ /**
+ * @throws \OCP\DB\Exception
+ */
+ public function deleteByUserId(string $userId): void {
+ $qb = $this->db->getQueryBuilder();
+ $qb->delete($this->getTableName())
+ ->where($qb->expr()->eq(
+ 'user_id',
+ $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
+ IQueryBuilder::PARAM_STR),
+ );
+ $qb->executeStatement();
+ }
+}
diff --git a/apps/dav/lib/Db/Direct.php b/apps/dav/lib/Db/Direct.php
index ca2586ab2e0..4e4a12d225f 100644
--- a/apps/dav/lib/Db/Direct.php
+++ b/apps/dav/lib/Db/Direct.php
@@ -3,29 +3,13 @@
declare(strict_types=1);
/**
- * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Db;
use OCP\AppFramework\Db\Entity;
+use OCP\DB\Types;
/**
* @method string getUserId()
@@ -51,9 +35,9 @@ class Direct extends Entity {
protected $expiration;
public function __construct() {
- $this->addType('userId', 'string');
- $this->addType('fileId', 'int');
- $this->addType('token', 'string');
- $this->addType('expiration', 'int');
+ $this->addType('userId', Types::STRING);
+ $this->addType('fileId', Types::INTEGER);
+ $this->addType('token', Types::STRING);
+ $this->addType('expiration', Types::INTEGER);
}
}
diff --git a/apps/dav/lib/Db/DirectMapper.php b/apps/dav/lib/Db/DirectMapper.php
index c0ed10b97c6..4fedac35b72 100644
--- a/apps/dav/lib/Db/DirectMapper.php
+++ b/apps/dav/lib/Db/DirectMapper.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Db;
@@ -62,6 +45,6 @@ class DirectMapper extends QBMapper {
$qb->expr()->lt('expiration', $qb->createNamedParameter($expiration))
);
- $qb->execute();
+ $qb->executeStatement();
}
}
diff --git a/apps/dav/lib/Db/Property.php b/apps/dav/lib/Db/Property.php
new file mode 100644
index 00000000000..96c5f75ef4f
--- /dev/null
+++ b/apps/dav/lib/Db/Property.php
@@ -0,0 +1,37 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\DAV\Db;
+
+use OCP\AppFramework\Db\Entity;
+
+/**
+ * @method string getUserid()
+ * @method string getPropertypath()
+ * @method string getPropertyname()
+ * @method string getPropertyvalue()
+ */
+class Property extends Entity {
+
+ /** @var string|null */
+ protected $userid;
+
+ /** @var string|null */
+ protected $propertypath;
+
+ /** @var string|null */
+ protected $propertyname;
+
+ /** @var string|null */
+ protected $propertyvalue;
+
+ /** @var int|null */
+ protected $valuetype;
+
+}
diff --git a/apps/dav/lib/Db/PropertyMapper.php b/apps/dav/lib/Db/PropertyMapper.php
new file mode 100644
index 00000000000..1789194ee7a
--- /dev/null
+++ b/apps/dav/lib/Db/PropertyMapper.php
@@ -0,0 +1,55 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\DAV\Db;
+
+use OCP\AppFramework\Db\QBMapper;
+use OCP\IDBConnection;
+
+/**
+ * @template-extends QBMapper<Property>
+ */
+class PropertyMapper extends QBMapper {
+
+ private const TABLE_NAME = 'properties';
+
+ public function __construct(IDBConnection $db) {
+ parent::__construct($db, self::TABLE_NAME, Property::class);
+ }
+
+ /**
+ * @return Property[]
+ */
+ public function findPropertyByPathAndName(string $userId, string $path, string $name): array {
+ $selectQb = $this->db->getQueryBuilder();
+ $selectQb->select('*')
+ ->from(self::TABLE_NAME)
+ ->where(
+ $selectQb->expr()->eq('userid', $selectQb->createNamedParameter($userId)),
+ $selectQb->expr()->eq('propertypath', $selectQb->createNamedParameter($path)),
+ $selectQb->expr()->eq('propertyname', $selectQb->createNamedParameter($name)),
+ );
+ return $this->findEntities($selectQb);
+ }
+
+ /**
+ * @return Property[]
+ */
+ public function findPropertiesByPath(string $userId, string $path): array {
+ $selectQb = $this->db->getQueryBuilder();
+ $selectQb->select('*')
+ ->from(self::TABLE_NAME)
+ ->where(
+ $selectQb->expr()->eq('userid', $selectQb->createNamedParameter($userId)),
+ $selectQb->expr()->eq('propertypath', $selectQb->createNamedParameter($path)),
+ );
+ return $this->findEntities($selectQb);
+ }
+
+}