2018-05-28 21:12:13 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2019-07-26 15:50:16 +03:00
|
|
|
* @copyright 2019, Georg Ehrke <oc.list@georgehrke.com>
|
2018-05-28 21:12:13 +03:00
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2018-05-28 21:12:13 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2018-05-28 21:12:13 +03:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-28 21:12:13 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
namespace OCA\DAV\CalDAV\ResourceBooking;
|
|
|
|
|
2019-08-14 14:38:11 +03:00
|
|
|
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
|
|
|
|
use OCA\DAV\Traits\PrincipalProxyTrait;
|
2018-05-28 21:12:13 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IGroupManager;
|
2018-06-05 22:26:29 +03:00
|
|
|
use OCP\ILogger;
|
2018-05-28 21:12:13 +03:00
|
|
|
use OCP\IUserSession;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Sabre\DAV\PropPatch;
|
|
|
|
use Sabre\DAVACL\PrincipalBackend\BackendInterface;
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
abstract class AbstractPrincipalBackend implements BackendInterface {
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
2018-06-05 22:26:29 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2019-08-14 14:38:11 +03:00
|
|
|
/** @var ProxyMapper */
|
|
|
|
private $proxyMapper;
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
/** @var string */
|
|
|
|
private $principalPrefix;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $dbTableName;
|
|
|
|
|
2019-07-26 15:50:16 +03:00
|
|
|
/** @var string */
|
|
|
|
private $dbMetaDataTableName;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $dbForeignKeyName;
|
|
|
|
|
2018-10-16 15:39:04 +03:00
|
|
|
/** @var string */
|
|
|
|
private $cuType;
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
/**
|
|
|
|
* @param IDBConnection $dbConnection
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param IGroupManager $groupManager
|
2018-06-05 22:26:29 +03:00
|
|
|
* @param ILogger $logger
|
2018-05-28 21:12:13 +03:00
|
|
|
* @param string $principalPrefix
|
|
|
|
* @param string $dbPrefix
|
2018-10-16 15:39:04 +03:00
|
|
|
* @param string $cuType
|
2018-05-28 21:12:13 +03:00
|
|
|
*/
|
|
|
|
public function __construct(IDBConnection $dbConnection,
|
|
|
|
IUserSession $userSession,
|
|
|
|
IGroupManager $groupManager,
|
2018-06-05 22:26:29 +03:00
|
|
|
ILogger $logger,
|
2019-08-14 14:38:11 +03:00
|
|
|
ProxyMapper $proxyMapper,
|
2018-10-16 15:39:04 +03:00
|
|
|
string $principalPrefix,
|
|
|
|
string $dbPrefix,
|
|
|
|
string $cuType) {
|
2018-05-28 21:12:13 +03:00
|
|
|
$this->db = $dbConnection;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->groupManager = $groupManager;
|
2018-06-05 22:26:29 +03:00
|
|
|
$this->logger = $logger;
|
2019-08-14 14:38:11 +03:00
|
|
|
$this->proxyMapper = $proxyMapper;
|
2018-05-28 21:12:13 +03:00
|
|
|
$this->principalPrefix = $principalPrefix;
|
2019-07-26 15:50:16 +03:00
|
|
|
$this->dbTableName = 'calendar_' . $dbPrefix . 's';
|
|
|
|
$this->dbMetaDataTableName = $this->dbTableName . '_md';
|
|
|
|
$this->dbForeignKeyName = $dbPrefix . '_id';
|
2018-10-16 15:39:04 +03:00
|
|
|
$this->cuType = $cuType;
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
|
|
|
|
2019-08-14 14:38:11 +03:00
|
|
|
use PrincipalProxyTrait;
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
/**
|
|
|
|
* Returns a list of principals based on a prefix.
|
|
|
|
*
|
|
|
|
* This prefix will often contain something like 'principals'. You are only
|
|
|
|
* expected to return principals that are in this base path.
|
|
|
|
*
|
|
|
|
* You are expected to return at least a 'uri' for every user, you can
|
|
|
|
* return any additional properties if you wish so. Common properties are:
|
|
|
|
* {DAV:}displayname
|
|
|
|
*
|
|
|
|
* @param string $prefixPath
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getPrincipalsByPrefix($prefixPath) {
|
|
|
|
$principals = [];
|
|
|
|
|
|
|
|
if ($prefixPath === $this->principalPrefix) {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
2018-06-05 22:26:29 +03:00
|
|
|
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname'])
|
2018-05-28 21:12:13 +03:00
|
|
|
->from($this->dbTableName);
|
|
|
|
$stmt = $query->execute();
|
|
|
|
|
2019-07-26 15:50:16 +03:00
|
|
|
$metaDataQuery = $this->db->getQueryBuilder();
|
|
|
|
$metaDataQuery->select([$this->dbForeignKeyName, 'key', 'value'])
|
|
|
|
->from($this->dbMetaDataTableName);
|
|
|
|
$metaDataStmt = $metaDataQuery->execute();
|
|
|
|
$metaDataRows = $metaDataStmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$metaDataById = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($metaDataRows as $metaDataRow) {
|
2019-07-26 15:50:16 +03:00
|
|
|
if (!isset($metaDataById[$metaDataRow[$this->dbForeignKeyName]])) {
|
|
|
|
$metaDataById[$metaDataRow[$this->dbForeignKeyName]] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$metaDataById[$metaDataRow[$this->dbForeignKeyName]][$metaDataRow['key']] =
|
|
|
|
$metaDataRow['value'];
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
|
2019-07-26 15:50:16 +03:00
|
|
|
$id = $row['id'];
|
|
|
|
|
|
|
|
if (isset($metaDataById[$id])) {
|
|
|
|
$principals[] = $this->rowToPrincipal($row, $metaDataById[$id]);
|
|
|
|
} else {
|
|
|
|
$principals[] = $this->rowToPrincipal($row);
|
|
|
|
}
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $principals;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a specific principal, specified by it's path.
|
|
|
|
* The returned structure should be the exact same as from
|
|
|
|
* getPrincipalsByPrefix.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getPrincipalByPath($path) {
|
|
|
|
if (strpos($path, $this->principalPrefix) !== 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-12 12:15:48 +03:00
|
|
|
[, $name] = \Sabre\Uri\split($path);
|
2018-05-28 21:12:13 +03:00
|
|
|
|
2021-01-12 12:15:48 +03:00
|
|
|
[$backendId, $resourceId] = explode('-', $name, 2);
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
2018-06-05 22:26:29 +03:00
|
|
|
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname'])
|
2018-05-28 21:12:13 +03:00
|
|
|
->from($this->dbTableName)
|
|
|
|
->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
|
|
|
|
->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)));
|
|
|
|
$stmt = $query->execute();
|
|
|
|
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$row) {
|
2018-05-28 21:12:13 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-26 15:50:16 +03:00
|
|
|
$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 = [];
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($metaDataRows as $metaDataRow) {
|
2019-07-26 15:50:16 +03:00
|
|
|
$metadata[$metaDataRow['key']] = $metaDataRow['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->rowToPrincipal($row, $metadata);
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
|
|
|
|
2019-08-01 19:37:50 +03:00
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function getPrincipalById($id):?array {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname'])
|
|
|
|
->from($this->dbTableName)
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id)));
|
|
|
|
$stmt = $query->execute();
|
|
|
|
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$row) {
|
2019-08-01 19:37:50 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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 = [];
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($metaDataRows as $metaDataRow) {
|
2019-08-01 19:37:50 +03:00
|
|
|
$metadata[$metaDataRow['key']] = $metaDataRow['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->rowToPrincipal($row, $metadata);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param PropPatch $propPatch
|
|
|
|
* @return int
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function updatePrincipal($path, PropPatch $propPatch) {
|
2018-05-28 21:12:13 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $prefixPath
|
|
|
|
* @param array $searchProperties
|
|
|
|
* @param string $test
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
|
2018-05-28 21:12:13 +03:00
|
|
|
$results = [];
|
|
|
|
if (\count($searchProperties) === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if ($prefixPath !== $this->principalPrefix) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (!$user) {
|
2018-06-05 22:26:29 +03:00
|
|
|
return [];
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
|
|
|
$usersGroups = $this->groupManager->getUserGroupIds($user);
|
|
|
|
|
|
|
|
foreach ($searchProperties as $prop => $value) {
|
|
|
|
switch ($prop) {
|
|
|
|
case '{http://sabredav.org/ns}email-address':
|
|
|
|
$query = $this->db->getQueryBuilder();
|
2018-06-05 22:26:29 +03:00
|
|
|
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
|
2018-05-28 21:12:13 +03:00
|
|
|
->from($this->dbTableName)
|
2018-06-05 22:26:29 +03:00
|
|
|
->where($query->expr()->iLike('email', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($value) . '%')));
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
$stmt = $query->execute();
|
2018-06-05 22:26:29 +03:00
|
|
|
$principals = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
|
2018-06-05 22:26:29 +03:00
|
|
|
if (!$this->isAllowedToAccessResource($row, $usersGroups)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$principals[] = $this->rowToPrincipal($row)['uri'];
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
2018-06-05 22:26:29 +03:00
|
|
|
$results[] = $principals;
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{DAV:}displayname':
|
|
|
|
$query = $this->db->getQueryBuilder();
|
2018-06-05 22:26:29 +03:00
|
|
|
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
|
2018-05-28 21:12:13 +03:00
|
|
|
->from($this->dbTableName)
|
2018-06-05 22:26:29 +03:00
|
|
|
->where($query->expr()->iLike('displayname', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($value) . '%')));
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
$stmt = $query->execute();
|
2018-06-05 22:26:29 +03:00
|
|
|
$principals = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
|
2018-06-05 22:26:29 +03:00
|
|
|
if (!$this->isAllowedToAccessResource($row, $usersGroups)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$principals[] = $this->rowToPrincipal($row)['uri'];
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
2018-06-05 22:26:29 +03:00
|
|
|
$results[] = $principals;
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
break;
|
|
|
|
|
2019-02-20 22:22:56 +03:00
|
|
|
case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set':
|
|
|
|
// If you add support for more search properties that qualify as a user-address,
|
|
|
|
// please also add them to the array below
|
|
|
|
$results[] = $this->searchPrincipals($this->principalPrefix, [
|
|
|
|
'{http://sabredav.org/ns}email-address' => $value,
|
|
|
|
], 'anyof');
|
|
|
|
break;
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
default:
|
2019-08-01 19:37:50 +03:00
|
|
|
$rowsByMetadata = $this->searchPrincipalsByMetadataKey($prop, $value);
|
2020-04-09 14:53:40 +03:00
|
|
|
$filteredRows = array_filter($rowsByMetadata, function ($row) use ($usersGroups) {
|
2019-08-01 19:37:50 +03:00
|
|
|
return $this->isAllowedToAccessResource($row, $usersGroups);
|
|
|
|
});
|
|
|
|
|
2021-02-14 14:43:31 +03:00
|
|
|
$results[] = array_map(function ($row): string {
|
2019-08-01 19:37:50 +03:00
|
|
|
return $row['uri'];
|
|
|
|
}, $filteredRows);
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// results is an array of arrays, so this is not the first search result
|
|
|
|
// but the results of the first searchProperty
|
|
|
|
if (count($results) === 1) {
|
2018-06-05 22:26:29 +03:00
|
|
|
return $results[0];
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ($test) {
|
|
|
|
case 'anyof':
|
2018-06-05 22:26:29 +03:00
|
|
|
return array_values(array_unique(array_merge(...$results)));
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
case 'allof':
|
|
|
|
default:
|
2018-06-05 22:26:29 +03:00
|
|
|
return array_values(array_intersect(...$results));
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 19:37:50 +03:00
|
|
|
/**
|
|
|
|
* Searches principals based on their metadata keys.
|
|
|
|
* This allows to search for all principals with a specific key.
|
|
|
|
* e.g.:
|
|
|
|
* '{http://nextcloud.com/ns}room-building-address' => 'ABC Street 123, ...'
|
|
|
|
*
|
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function searchPrincipalsByMetadataKey($key, $value):array {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select([$this->dbForeignKeyName])
|
|
|
|
->from($this->dbMetaDataTableName)
|
|
|
|
->where($query->expr()->eq('key', $query->createNamedParameter($key)))
|
|
|
|
->andWhere($query->expr()->iLike('value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($value) . '%')));
|
|
|
|
$stmt = $query->execute();
|
|
|
|
|
|
|
|
$rows = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
|
2019-08-01 19:37:50 +03:00
|
|
|
$id = $row[$this->dbForeignKeyName];
|
|
|
|
|
|
|
|
$principalRow = $this->getPrincipalById($id);
|
|
|
|
if (!$principalRow) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows[] = $principalRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rows;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
/**
|
|
|
|
* @param string $uri
|
|
|
|
* @param string $principalPrefix
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function findByUri($uri, $principalPrefix) {
|
2018-05-28 21:12:13 +03:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (!$user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$usersGroups = $this->groupManager->getUserGroupIds($user);
|
|
|
|
|
|
|
|
if (strpos($uri, 'mailto:') === 0) {
|
|
|
|
$email = substr($uri, 7);
|
|
|
|
$query = $this->db->getQueryBuilder();
|
2018-06-05 22:26:29 +03:00
|
|
|
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
|
2018-05-28 21:12:13 +03:00
|
|
|
->from($this->dbTableName)
|
|
|
|
->where($query->expr()->eq('email', $query->createNamedParameter($email)));
|
|
|
|
|
|
|
|
$stmt = $query->execute();
|
|
|
|
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$row) {
|
2018-05-28 21:12:13 +03:00
|
|
|
return null;
|
|
|
|
}
|
2018-06-05 22:26:29 +03:00
|
|
|
if (!$this->isAllowedToAccessResource($row, $usersGroups)) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-28 21:12:13 +03:00
|
|
|
|
|
|
|
return $this->rowToPrincipal($row)['uri'];
|
|
|
|
}
|
2018-06-05 22:26:29 +03:00
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
if (strpos($uri, 'principal:') === 0) {
|
2018-06-05 22:26:29 +03:00
|
|
|
$path = substr($uri, 10);
|
|
|
|
if (strpos($path, $this->principalPrefix) !== 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:15:48 +03:00
|
|
|
[, $name] = \Sabre\Uri\split($path);
|
|
|
|
[$backendId, $resourceId] = explode('-', $name, 2);
|
2018-06-05 22:26:29 +03:00
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions'])
|
|
|
|
->from($this->dbTableName)
|
|
|
|
->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
|
|
|
|
->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)));
|
|
|
|
$stmt = $query->execute();
|
|
|
|
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
|
2018-05-28 21:12:13 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$row) {
|
2018-06-05 22:26:29 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->isAllowedToAccessResource($row, $usersGroups)) {
|
|
|
|
return null;
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
2018-06-05 22:26:29 +03:00
|
|
|
|
|
|
|
return $this->rowToPrincipal($row)['uri'];
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* convert database row to principal
|
2019-07-26 15:50:16 +03:00
|
|
|
*
|
|
|
|
* @param String[] $row
|
|
|
|
* @param String[] $metadata
|
|
|
|
* @return Array
|
2018-05-28 21:12:13 +03:00
|
|
|
*/
|
2020-10-05 16:12:57 +03:00
|
|
|
private function rowToPrincipal(array $row, array $metadata = []):array {
|
2019-07-26 15:50:16 +03:00
|
|
|
return array_merge([
|
2018-05-28 21:12:13 +03:00
|
|
|
'uri' => $this->principalPrefix . '/' . $row['backend_id'] . '-' . $row['resource_id'],
|
|
|
|
'{DAV:}displayname' => $row['displayname'],
|
2018-10-16 15:39:04 +03:00
|
|
|
'{http://sabredav.org/ns}email-address' => $row['email'],
|
|
|
|
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->cuType,
|
2019-07-26 15:50:16 +03:00
|
|
|
], $metadata);
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|
2018-06-05 22:26:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $row
|
|
|
|
* @param $userGroups
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-07-26 15:50:16 +03:00
|
|
|
private function isAllowedToAccessResource(array $row, array $userGroups):bool {
|
2018-06-05 22:26:29 +03:00
|
|
|
if (!isset($row['group_restrictions']) ||
|
|
|
|
$row['group_restrictions'] === null ||
|
|
|
|
$row['group_restrictions'] === '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// group restrictions contains something, but not parsable, deny access and log warning
|
|
|
|
$json = json_decode($row['group_restrictions']);
|
|
|
|
if (!\is_array($json)) {
|
|
|
|
$this->logger->info('group_restrictions field could not be parsed for ' . $this->dbTableName . '::' . $row['id'] . ', denying access to resource');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty array => no group restrictions
|
|
|
|
if (empty($json)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !empty(array_intersect($json, $userGroups));
|
|
|
|
}
|
2018-05-28 21:12:13 +03:00
|
|
|
}
|