aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib
diff options
context:
space:
mode:
authorGeorg Ehrke <developer@georgehrke.com>2019-08-14 13:38:11 +0200
committerGeorg Ehrke <developer@georgehrke.com>2019-08-15 15:41:28 +0200
commit63d584afb5727737fe73a0ca2ecf720022b33922 (patch)
treec3fc0a335c2f3766b61257270cdb167b9a7aa788 /apps/dav/lib
parent3d86537dc922083427a283e84d726d416f9ec95c (diff)
downloadnextcloud-server-63d584afb5727737fe73a0ca2ecf720022b33922.tar.gz
nextcloud-server-63d584afb5727737fe73a0ca2ecf720022b33922.zip
use principaluri instead of userid, allowing to add delegates for rooms and things
Signed-off-by: Georg Ehrke <developer@georgehrke.com> !fixup add owner_id and proxy_id as db index, since we use it for querying Signed-off-by: Georg Ehrke <developer@georgehrke.com> !fixup don't add ACL for each individual proxy, just use calendar-proxy groups Signed-off-by: Georg Ehrke <developer@georgehrke.com> !fixup allow delegation of resources / rooms Signed-off-by: Georg Ehrke <developer@georgehrke.com> !fixup fix addIndex call in migration Signed-off-by: Georg Ehrke <developer@georgehrke.com> !fixup fix remaining constructor calls of Principal Signed-off-by: Georg Ehrke <developer@georgehrke.com> !fixup minor fixes and unit tests Signed-off-by: Georg Ehrke <developer@georgehrke.com>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r--apps/dav/lib/CalDAV/Calendar.php87
-rw-r--r--apps/dav/lib/CalDAV/Proxy/ProxyMapper.php14
-rw-r--r--apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php42
-rw-r--r--apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php14
-rw-r--r--apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php14
-rw-r--r--apps/dav/lib/Command/CreateCalendar.php5
-rw-r--r--apps/dav/lib/Connector/Sabre/Principal.php136
-rw-r--r--apps/dav/lib/Migration/Version1011Date20190806104428.php2
-rw-r--r--apps/dav/lib/RootCollection.php8
-rw-r--r--apps/dav/lib/Traits/PrincipalProxyTrait.php223
10 files changed, 360 insertions, 185 deletions
diff --git a/apps/dav/lib/CalDAV/Calendar.php b/apps/dav/lib/CalDAV/Calendar.php
index ae5cb226b0e..38def19af1d 100644
--- a/apps/dav/lib/CalDAV/Calendar.php
+++ b/apps/dav/lib/CalDAV/Calendar.php
@@ -47,9 +47,14 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
/** @var IConfig */
private $config;
- /** @var ProxyMapper */
- private $proxyMapper;
-
+ /**
+ * Calendar constructor.
+ *
+ * @param BackendInterface $caldavBackend
+ * @param $calendarInfo
+ * @param IL10N $l10n
+ * @param IConfig $config
+ */
public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n, IConfig $config) {
parent::__construct($caldavBackend, $calendarInfo);
@@ -62,9 +67,6 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
}
$this->config = $config;
-
- // TODO: proper DI
- $this->proxyMapper = \OC::$server->query(ProxyMapper::class);
}
/**
@@ -126,29 +128,60 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
return $this->calendarInfo['principaluri'];
}
+ /**
+ * @return array
+ */
public function getACL() {
$acl = [
[
'privilege' => '{DAV:}read',
'principal' => $this->getOwner(),
'protected' => true,
- ]];
+ ],
+ [
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->getOwner() . '/calendar-proxy-write',
+ 'protected' => true,
+ ],
+ [
+ 'privilege' => '{DAV:}read',
+ 'principal' => $this->getOwner() . '/calendar-proxy-read',
+ 'protected' => true,
+ ],
+ ];
+
if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
$acl[] = [
'privilege' => '{DAV:}write',
'principal' => $this->getOwner(),
'protected' => true,
];
+ $acl[] = [
+ 'privilege' => '{DAV:}write',
+ 'principal' => $this->getOwner() . '/calendar-proxy-write',
+ 'protected' => true,
+ ];
} else {
$acl[] = [
'privilege' => '{DAV:}write-properties',
'principal' => $this->getOwner(),
'protected' => true,
];
+ $acl[] = [
+ 'privilege' => '{DAV:}write-properties',
+ 'principal' => $this->getOwner() . '/calendar-proxy-write',
+ 'protected' => true,
+ ];
}
+ $acl[] = [
+ 'privilege' => '{DAV:}write-properties',
+ 'principal' => $this->getOwner() . '/calendar-proxy-read',
+ 'protected' => true,
+ ];
+
if (!$this->isShared()) {
- return $this->addProxies($acl);
+ return $acl;
}
if ($this->getOwner() !== parent::getOwner()) {
@@ -180,38 +213,16 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
}
$acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
- $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public'];
- $acl = array_filter($acl, function($rule) use ($allowedPrincipals) {
+ $allowedPrincipals = [
+ $this->getOwner(),
+ $this->getOwner(). '/calendar-proxy-read',
+ $this->getOwner(). '/calendar-proxy-write',
+ parent::getOwner(),
+ 'principals/system/public'
+ ];
+ return array_filter($acl, function($rule) use ($allowedPrincipals) {
return \in_array($rule['principal'], $allowedPrincipals, true);
});
-
- $acl = $this->addProxies($acl);
-
- return $acl;
- }
-
- public function addProxies(array $acl): array {
- list($prefix, $name) = \Sabre\Uri\split($this->getOwner());
- $proxies = $this->proxyMapper->getProxiesOf($name);
-
- foreach ($proxies as $proxy) {
- if ($proxy->getPermissions() & ProxyMapper::PERMISSION_READ) {
- $acl[] = [
- 'privilege' => '{DAV:}read',
- 'principal' => 'principals/users/' . $proxy->getProxyId(),
- 'protected' => true,
- ];
- }
- if ($proxy->getPermissions() & ProxyMapper::PERMISSION_WRITE) {
- $acl[] = [
- 'privilege' => '{DAV:}write',
- 'principal' => 'principals/users/' . $proxy->getProxyId(),
- 'protected' => true,
- ];
- }
- }
-
- return $acl;
}
public function getChildACL() {
diff --git a/apps/dav/lib/CalDAV/Proxy/ProxyMapper.php b/apps/dav/lib/CalDAV/Proxy/ProxyMapper.php
index 6a569394c4b..8d8adb811b6 100644
--- a/apps/dav/lib/CalDAV/Proxy/ProxyMapper.php
+++ b/apps/dav/lib/CalDAV/Proxy/ProxyMapper.php
@@ -27,17 +27,27 @@ namespace OCA\DAV\CalDAV\Proxy;
use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;
+/**
+ * Class ProxyMapper
+ *
+ * @package OCA\DAV\CalDAV\Proxy
+ */
class ProxyMapper extends QBMapper {
const PERMISSION_READ = 1;
const PERMISSION_WRITE = 2;
+ /**
+ * ProxyMapper constructor.
+ *
+ * @param IDBConnection $db
+ */
public function __construct(IDBConnection $db) {
parent::__construct($db, 'dav_cal_proxy', Proxy::class);
}
/**
- * @param string $proxyId The userId that can act as a proxy for the resulting calendars
+ * @param string $proxyId The principal uri that can act as a proxy for the resulting calendars
*
* @return Proxy[]
*/
@@ -52,7 +62,7 @@ class ProxyMapper extends QBMapper {
}
/**
- * @param string $ownerId The userId that has the resulting proxies for their calendars
+ * @param string $ownerId The principal uri that has the resulting proxies for their calendars
*
* @return Proxy[]
*/
diff --git a/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php b/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php
index aab5fcab8ad..63ed3381d14 100644
--- a/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php
+++ b/apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php
@@ -22,6 +22,8 @@
*/
namespace OCA\DAV\CalDAV\ResourceBooking;
+use OCA\DAV\CalDAV\Proxy\ProxyMapper;
+use OCA\DAV\Traits\PrincipalProxyTrait;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\ILogger;
@@ -44,6 +46,9 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
/** @var ILogger */
private $logger;
+ /** @var ProxyMapper */
+ private $proxyMapper;
+
/** @var string */
private $principalPrefix;
@@ -72,6 +77,7 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
IUserSession $userSession,
IGroupManager $groupManager,
ILogger $logger,
+ ProxyMapper $proxyMapper,
string $principalPrefix,
string $dbPrefix,
string $cuType) {
@@ -79,6 +85,7 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
$this->userSession = $userSession;
$this->groupManager = $groupManager;
$this->logger = $logger;
+ $this->proxyMapper = $proxyMapper;
$this->principalPrefix = $principalPrefix;
$this->dbTableName = 'calendar_' . $dbPrefix . 's';
$this->dbMetaDataTableName = $this->dbTableName . '_md';
@@ -86,6 +93,8 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
$this->cuType = $cuType;
}
+ use PrincipalProxyTrait;
+
/**
* Returns a list of principals based on a prefix.
*
@@ -216,39 +225,6 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
}
/**
- * Returns the list of members for a group-principal
- *
- * @param string $principal
- * @return string[]
- */
- public function getGroupMemberSet($principal) {
- return [];
- }
-
- /**
- * Returns the list of groups a principal is a member of
- *
- * @param string $principal
- * @return array
- */
- public function getGroupMembership($principal) {
- return [];
- }
-
- /**
- * Updates the list of group members for a group principal.
- *
- * The principals should be passed as a list of uri's.
- *
- * @param string $principal
- * @param string[] $members
- * @throws Exception
- */
- public function setGroupMemberSet($principal, array $members) {
- throw new Exception('Setting members of the group is not supported yet');
- }
-
- /**
* @param string $path
* @param PropPatch $propPatch
* @return int
diff --git a/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php b/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php
index 0f6e6e7b4fd..128e6c21fad 100644
--- a/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php
+++ b/apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php
@@ -22,24 +22,34 @@
*/
namespace OCA\DAV\CalDAV\ResourceBooking;
+use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUserSession;
+/**
+ * Class ResourcePrincipalBackend
+ *
+ * @package OCA\DAV\CalDAV\ResourceBooking
+ */
class ResourcePrincipalBackend extends AbstractPrincipalBackend {
/**
+ * ResourcePrincipalBackend constructor.
+ *
* @param IDBConnection $dbConnection
* @param IUserSession $userSession
* @param IGroupManager $groupManager
* @param ILogger $logger
+ * @param ProxyMapper $proxyMapper
*/
public function __construct(IDBConnection $dbConnection,
IUserSession $userSession,
IGroupManager $groupManager,
- ILogger $logger) {
+ ILogger $logger,
+ ProxyMapper $proxyMapper) {
parent::__construct($dbConnection, $userSession, $groupManager, $logger,
- 'principals/calendar-resources', 'resource', 'RESOURCE');
+ $proxyMapper, 'principals/calendar-resources', 'resource', 'RESOURCE');
}
}
diff --git a/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php b/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php
index 68a344aa0ca..3e9e8f68852 100644
--- a/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php
+++ b/apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php
@@ -22,24 +22,34 @@
*/
namespace OCA\DAV\CalDAV\ResourceBooking;
+use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUserSession;
+/**
+ * Class RoomPrincipalBackend
+ *
+ * @package OCA\DAV\CalDAV\ResourceBooking
+ */
class RoomPrincipalBackend extends AbstractPrincipalBackend {
/**
+ * RoomPrincipalBackend constructor.
+ *
* @param IDBConnection $dbConnection
* @param IUserSession $userSession
* @param IGroupManager $groupManager
* @param ILogger $logger
+ * @param ProxyMapper $proxyMapper
*/
public function __construct(IDBConnection $dbConnection,
IUserSession $userSession,
IGroupManager $groupManager,
- ILogger $logger) {
+ ILogger $logger,
+ ProxyMapper $proxyMapper) {
parent::__construct($dbConnection, $userSession, $groupManager, $logger,
- 'principals/calendar-rooms', 'room', 'ROOM');
+ $proxyMapper, 'principals/calendar-rooms', 'room', 'ROOM');
}
}
diff --git a/apps/dav/lib/Command/CreateCalendar.php b/apps/dav/lib/Command/CreateCalendar.php
index 8e3a4abf8cf..a40bf48cc8e 100644
--- a/apps/dav/lib/Command/CreateCalendar.php
+++ b/apps/dav/lib/Command/CreateCalendar.php
@@ -24,6 +24,7 @@
namespace OCA\DAV\Command;
use OCA\DAV\CalDAV\CalDavBackend;
+use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\IDBConnection;
use OCP\IGroupManager;
@@ -78,8 +79,8 @@ class CreateCalendar extends Command {
$this->groupManager,
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
- \OC::$server->getConfig(),
- \OC::$server->getAppManager()
+ \OC::$server->getAppManager(),
+ \OC::$server->query(ProxyMapper::class)
);
$random = \OC::$server->getSecureRandom();
$logger = \OC::$server->getLogger();
diff --git a/apps/dav/lib/Connector/Sabre/Principal.php b/apps/dav/lib/Connector/Sabre/Principal.php
index cc8e9c7accb..5c61b8371f2 100644
--- a/apps/dav/lib/Connector/Sabre/Principal.php
+++ b/apps/dav/lib/Connector/Sabre/Principal.php
@@ -37,6 +37,7 @@ namespace OCA\DAV\Connector\Sabre;
use OCA\Circles\Exceptions\CircleDoesNotExistException;
use OCA\DAV\CalDAV\Proxy\Proxy;
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
+use OCA\DAV\Traits\PrincipalProxyTrait;
use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use OCP\IGroup;
@@ -74,9 +75,21 @@ class Principal implements BackendInterface {
/** @var bool */
private $hasCircles;
+
/** @var ProxyMapper */
private $proxyMapper;
+ /**
+ * Principal constructor.
+ *
+ * @param IUserManager $userManager
+ * @param IGroupManager $groupManager
+ * @param IShareManager $shareManager
+ * @param IUserSession $userSession
+ * @param IAppManager $appManager
+ * @param ProxyMapper $proxyMapper
+ * @param string $principalPrefix
+ */
public function __construct(IUserManager $userManager,
IGroupManager $groupManager,
IShareManager $shareManager,
@@ -94,6 +107,10 @@ class Principal implements BackendInterface {
$this->proxyMapper = $proxyMapper;
}
+ use PrincipalProxyTrait {
+ getGroupMembership as protected traitGetGroupMembership;
+ }
+
/**
* Returns a list of principals based on a prefix.
*
@@ -162,23 +179,6 @@ class Principal implements BackendInterface {
}
/**
- * Returns the list of members for a group-principal
- *
- * @param string $principal
- * @return string[]
- * @throws Exception
- */
- public function getGroupMemberSet($principal) {
- // TODO: for now the group principal has only one member, the user itself
- $principal = $this->getPrincipalByPath($principal);
- if (!$principal) {
- throw new Exception('Principal not found');
- }
-
- return [$principal['uri']];
- }
-
- /**
* Returns the list of groups a principal is a member of
*
* @param string $principal
@@ -189,99 +189,30 @@ class Principal implements BackendInterface {
public function getGroupMembership($principal, $needGroups = false) {
list($prefix, $name) = \Sabre\Uri\split($principal);
- if ($prefix === $this->principalPrefix) {
- $user = $this->userManager->get($name);
- if (!$user) {
- throw new Exception('Principal not found');
- }
-
- if ($this->hasGroups || $needGroups) {
- $groups = $this->groupManager->getUserGroups($user);
- $groups = array_map(function($group) {
- /** @var IGroup $group */
- return 'principals/groups/' . urlencode($group->getGID());
- }, $groups);
-
- $proxies = $this->proxyMapper->getProxiesFor($user->getUID());
- foreach ($proxies as $proxy) {
- if ($proxy->getPermissions() & ProxyMapper::PERMISSION_READ) {
- $groups[] = 'principals/users/' . $proxy->getOwnerId() . '/calendar-proxy-read';
- }
-
- if ($proxy->getPermissions() & ProxyMapper::PERMISSION_WRITE) {
- $groups[] = 'principals/users/' . $proxy->getOwnerId() . '/calendar-proxy-write';
- }
- }
-
- return $groups;
- }
- }
- return [];
- }
-
- /**
- * Updates the list of group members for a group principal.
- *
- * The principals should be passed as a list of uri's.
- *
- * @param string $principal
- * @param string[] $members
- * @throws Exception
- */
- public function setGroupMemberSet($principal, array $members) {
- list($prefix, $target) = \Sabre\Uri\split($principal);
-
- if ($target !== 'calendar-proxy-write' && $target !== 'calendar-proxy-read') {
- throw new Exception('Setting members of the group is not supported yet');
+ if ($prefix !== $this->principalPrefix) {
+ return [];
}
- $permission = ProxyMapper::PERMISSION_READ;
- if ($target === 'calendar-proxy-write') {
- $permission |= ProxyMapper::PERMISSION_WRITE;
+ $user = $this->userManager->get($name);
+ if (!$user) {
+ throw new Exception('Principal not found');
}
- list($prefix, $owner) = \Sabre\Uri\split($prefix);
- $proxies = $this->proxyMapper->getProxiesOf($owner);
+ $groups = [];
- foreach ($members as $member) {
- list($prefix, $name) = \Sabre\Uri\split($member);
-
- if ($prefix !== $this->principalPrefix) {
- throw new Exception('Invalid member group prefix: ' . $prefix);
- }
-
- $user = $this->userManager->get($name);
- if ($user === null) {
- throw new Exception('Invalid member: ' . $name);
- }
-
- $found = false;
- foreach ($proxies as $proxy) {
- if ($proxy->getProxyId() === $user->getUID()) {
- $found = true;
- $proxy->setPermissions($proxy->getPermissions() | $permission);
- $this->proxyMapper->update($proxy);
-
- $proxies = array_filter($proxies, function(Proxy $p) use ($proxy) {
- return $p->getId() !== $proxy->getId();
- });
- break;
- }
- }
-
- if ($found === false) {
- $proxy = new Proxy();
- $proxy->setOwnerId($owner);
- $proxy->setProxyId($user->getUID());
- $proxy->setPermissions($permission);
- $this->proxyMapper->insert($proxy);
+ if ($this->hasGroups || $needGroups) {
+ $userGroups = $this->groupManager->getUserGroups($user);
+ foreach($userGroups as $userGroup) {
+ $groups[] = 'principals/groups/' . urlencode($userGroup->getGID());
}
}
- // Delete all remaining proxies
- foreach ($proxies as $proxy) {
- $this->proxyMapper->delete($proxy);
- }
+ $groups = array_unique(array_merge(
+ $groups,
+ $this->traitGetGroupMembership($principal, $needGroups)
+ ));
+
+ return $groups;
}
/**
@@ -552,5 +483,4 @@ class Principal implements BackendInterface {
return [];
}
-
}
diff --git a/apps/dav/lib/Migration/Version1011Date20190806104428.php b/apps/dav/lib/Migration/Version1011Date20190806104428.php
index 4c237b591ec..c144e62bcde 100644
--- a/apps/dav/lib/Migration/Version1011Date20190806104428.php
+++ b/apps/dav/lib/Migration/Version1011Date20190806104428.php
@@ -47,6 +47,8 @@ class Version1011Date20190806104428 extends SimpleMigrationStep {
$table->setPrimaryKey(['id']);
$table->addUniqueIndex(['owner_id', 'proxy_id', 'permissions'], 'dav_cal_proxy_uidx');
+ $table->addIndex(['owner_id'], 'dav_cal_proxy_ioid');
+ $table->addIndex(['proxy_id'], 'dav_cal_proxy_ipid');
return $schema;
}
diff --git a/apps/dav/lib/RootCollection.php b/apps/dav/lib/RootCollection.php
index 12b870932b3..ed8297783d7 100644
--- a/apps/dav/lib/RootCollection.php
+++ b/apps/dav/lib/RootCollection.php
@@ -54,17 +54,19 @@ class RootCollection extends SimpleCollection {
$shareManager = \OC::$server->getShareManager();
$db = \OC::$server->getDatabaseConnection();
$dispatcher = \OC::$server->getEventDispatcher();
+ $proxyMapper = \OC::$server->query(ProxyMapper::class);
+
$userPrincipalBackend = new Principal(
$userManager,
$groupManager,
$shareManager,
\OC::$server->getUserSession(),
\OC::$server->getAppManager(),
- \OC::$server->query(ProxyMapper::class)
+ $proxyMapper
);
$groupPrincipalBackend = new GroupPrincipalBackend($groupManager, $userSession, $shareManager, $l10n);
- $calendarResourcePrincipalBackend = new ResourcePrincipalBackend($db, $userSession, $groupManager, $logger);
- $calendarRoomPrincipalBackend = new RoomPrincipalBackend($db, $userSession, $groupManager, $logger);
+ $calendarResourcePrincipalBackend = new ResourcePrincipalBackend($db, $userSession, $groupManager, $logger, $proxyMapper);
+ $calendarRoomPrincipalBackend = new RoomPrincipalBackend($db, $userSession, $groupManager, $logger, $proxyMapper);
// as soon as debug mode is enabled we allow listing of principals
$disableListing = !$config->getSystemValue('debug', false);
diff --git a/apps/dav/lib/Traits/PrincipalProxyTrait.php b/apps/dav/lib/Traits/PrincipalProxyTrait.php
new file mode 100644
index 00000000000..5e16d3a2f83
--- /dev/null
+++ b/apps/dav/lib/Traits/PrincipalProxyTrait.php
@@ -0,0 +1,223 @@
+<?php
+/**
+ * @copyright 2019, Georg Ehrke <oc.list@georgehrke.com>
+ *
+ * @author Georg Ehrke <oc.list@georgehrke.com>
+ *
+ * @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/>.
+ *
+ */
+namespace OCA\DAV\Traits;
+
+use OCA\DAV\CalDAV\Proxy\Proxy;
+use OCA\DAV\CalDAV\Proxy\ProxyMapper;
+use Sabre\DAV\Exception;
+
+/**
+ * Trait PrincipalTrait
+ *
+ * @package OCA\DAV\Traits
+ */
+trait PrincipalProxyTrait {
+
+ /**
+ * Returns the list of members for a group-principal
+ *
+ * @param string $principal
+ * @return string[]
+ * @throws Exception
+ */
+ public function getGroupMemberSet($principal) {
+ $members = [];
+
+ if ($this->isProxyPrincipal($principal)) {
+ $realPrincipal = $this->getPrincipalUriFromProxyPrincipal($principal);
+ $principalArray = $this->getPrincipalByPath($realPrincipal);
+ if (!$principalArray) {
+ throw new Exception('Principal not found');
+ }
+
+ $proxies = $this->proxyMapper->getProxiesOf($principalArray['uri']);
+ foreach ($proxies as $proxy) {
+ if ($this->isReadProxyPrincipal($principal) && $proxy->getPermissions() === ProxyMapper::PERMISSION_READ) {
+ $members[] = $proxy->getProxyId();
+ }
+
+ if ($this->isWriteProxyPrincipal($principal) && $proxy->getPermissions() === (ProxyMapper::PERMISSION_READ | ProxyMapper::PERMISSION_WRITE)) {
+ $members[] = $proxy->getProxyId();
+ }
+ }
+ }
+
+ return $members;
+ }
+
+ /**
+ * Returns the list of groups a principal is a member of
+ *
+ * @param string $principal
+ * @param bool $needGroups
+ * @return array
+ * @throws Exception
+ */
+ public function getGroupMembership($principal, $needGroups = false) {
+ list($prefix, $name) = \Sabre\Uri\split($principal);
+
+ if ($prefix !== $this->principalPrefix) {
+ return [];
+ }
+
+ $principalArray = $this->getPrincipalByPath($principal);
+ if (!$principalArray) {
+ throw new Exception('Principal not found');
+ }
+
+ $groups = [];
+ $proxies = $this->proxyMapper->getProxiesFor($principal);
+ foreach ($proxies as $proxy) {
+ if ($proxy->getPermissions() === ProxyMapper::PERMISSION_READ) {
+ $groups[] = $proxy->getOwnerId() . '/calendar-proxy-read';
+ }
+
+ if ($proxy->getPermissions() === (ProxyMapper::PERMISSION_READ | ProxyMapper::PERMISSION_WRITE)) {
+ $groups[] = $proxy->getOwnerId() . '/calendar-proxy-write';
+ }
+ }
+
+ return $groups;
+ }
+
+ /**
+ * Updates the list of group members for a group principal.
+ *
+ * The principals should be passed as a list of uri's.
+ *
+ * @param string $principal
+ * @param string[] $members
+ * @throws Exception
+ */
+ public function setGroupMemberSet($principal, array $members) {
+ list($principalUri, $target) = \Sabre\Uri\split($principal);
+
+ if ($target !== 'calendar-proxy-write' && $target !== 'calendar-proxy-read') {
+ throw new Exception('Setting members of the group is not supported yet');
+ }
+
+ $masterPrincipalArray = $this->getPrincipalByPath($principalUri);
+ if (!$masterPrincipalArray) {
+ throw new Exception('Principal not found');
+ }
+
+ $permission = ProxyMapper::PERMISSION_READ;
+ if ($target === 'calendar-proxy-write') {
+ $permission |= ProxyMapper::PERMISSION_WRITE;
+ }
+
+ list($prefix, $owner) = \Sabre\Uri\split($principalUri);
+ $proxies = $this->proxyMapper->getProxiesOf($principalUri);
+
+ foreach ($members as $member) {
+ list($prefix, $name) = \Sabre\Uri\split($member);
+
+ if ($prefix !== $this->principalPrefix) {
+ throw new Exception('Invalid member group prefix: ' . $prefix);
+ }
+
+ $principalArray = $this->getPrincipalByPath($member);
+ if (!$principalArray) {
+ throw new Exception('Principal not found');
+ }
+
+ $found = false;
+ foreach ($proxies as $proxy) {
+ if ($proxy->getProxyId() === $member) {
+ $found = true;
+ $proxy->setPermissions($proxy->getPermissions() | $permission);
+ $this->proxyMapper->update($proxy);
+
+ $proxies = array_filter($proxies, function(Proxy $p) use ($proxy) {
+ return $p->getId() !== $proxy->getId();
+ });
+ break;
+ }
+ }
+
+ if ($found === false) {
+ $proxy = new Proxy();
+ $proxy->setOwnerId($principalUri);
+ $proxy->setProxyId($member);
+ $proxy->setPermissions($permission);
+ $this->proxyMapper->insert($proxy);
+ }
+ }
+
+ // Delete all remaining proxies
+ foreach ($proxies as $proxy) {
+ // Write and Read Proxies have individual requests,
+ // so only delete proxies of this permission
+ if ($proxy->getPermissions() === $permission) {
+ $this->proxyMapper->delete($proxy);
+ }
+ }
+ }
+
+ /**
+ * @param string $principalUri
+ * @return bool
+ */
+ private function isProxyPrincipal(string $principalUri):bool {
+ list($realPrincipalUri, $proxy) = \Sabre\Uri\split($principalUri);
+ list($prefix, $userId) = \Sabre\Uri\split($realPrincipalUri);
+
+ if (!isset($prefix) || !isset($userId)) {
+ return false;
+ }
+ if ($prefix !== $this->principalPrefix) {
+ return false;
+ }
+
+ return $proxy === 'calendar-proxy-read'
+ || $proxy === 'calendar-proxy-write';
+
+ }
+
+ /**
+ * @param string $principalUri
+ * @return bool
+ */
+ private function isReadProxyPrincipal(string $principalUri):bool {
+ list(, $proxy) = \Sabre\Uri\split($principalUri);
+ return $proxy === 'calendar-proxy-read';
+ }
+
+ /**
+ * @param string $principalUri
+ * @return bool
+ */
+ private function isWriteProxyPrincipal(string $principalUri):bool {
+ list(, $proxy) = \Sabre\Uri\split($principalUri);
+ return $proxy === 'calendar-proxy-write';
+ }
+
+ /**
+ * @param string $principalUri
+ * @return string
+ */
+ private function getPrincipalUriFromProxyPrincipal(string $principalUri):string {
+ list($realPrincipalUri, ) = \Sabre\Uri\split($principalUri);
+ return $realPrincipalUri;
+ }
+}