Skip to content

Commit

Permalink
use principaluri instead of userid, allowing to add delegates for roo…
Browse files Browse the repository at this point in the history
…ms 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>
  • Loading branch information
georgehrke committed Aug 15, 2019
1 parent c8c4696 commit 7179b22
Show file tree
Hide file tree
Showing 21 changed files with 644 additions and 222 deletions.
2 changes: 1 addition & 1 deletion apps/dav/appinfo/v1/caldav.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
\OC::$server->getGroupManager(),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getConfig(),
\OC::$server->getAppManager(),
\OC::$server->query(\OCA\DAV\CalDAV\Proxy\ProxyMapper::class),
'principals/'
);
$db = \OC::$server->getDatabaseConnection();
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/appinfo/v1/carddav.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
\OC::$server->getGroupManager(),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getConfig(),
\OC::$server->getAppManager(),
\OC::$server->query(\OCA\DAV\CalDAV\Proxy\ProxyMapper::class),
'principals/'
);
$db = \OC::$server->getDatabaseConnection();
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
'OCA\\DAV\\SystemTag\\SystemTagsObjectMappingCollection' => $baseDir . '/../lib/SystemTag/SystemTagsObjectMappingCollection.php',
'OCA\\DAV\\SystemTag\\SystemTagsObjectTypeCollection' => $baseDir . '/../lib/SystemTag/SystemTagsObjectTypeCollection.php',
'OCA\\DAV\\SystemTag\\SystemTagsRelationsCollection' => $baseDir . '/../lib/SystemTag/SystemTagsRelationsCollection.php',
'OCA\\DAV\\Traits\\PrincipalProxyTrait' => $baseDir . '/../lib/Traits/PrincipalProxyTrait.php',
'OCA\\DAV\\Upload\\AssemblyStream' => $baseDir . '/../lib/Upload/AssemblyStream.php',
'OCA\\DAV\\Upload\\ChunkingPlugin' => $baseDir . '/../lib/Upload/ChunkingPlugin.php',
'OCA\\DAV\\Upload\\CleanupService' => $baseDir . '/../lib/Upload/CleanupService.php',
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\SystemTag\\SystemTagsObjectMappingCollection' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagsObjectMappingCollection.php',
'OCA\\DAV\\SystemTag\\SystemTagsObjectTypeCollection' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagsObjectTypeCollection.php',
'OCA\\DAV\\SystemTag\\SystemTagsRelationsCollection' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagsRelationsCollection.php',
'OCA\\DAV\\Traits\\PrincipalProxyTrait' => __DIR__ . '/..' . '/../lib/Traits/PrincipalProxyTrait.php',
'OCA\\DAV\\Upload\\AssemblyStream' => __DIR__ . '/..' . '/../lib/Upload/AssemblyStream.php',
'OCA\\DAV\\Upload\\ChunkingPlugin' => __DIR__ . '/..' . '/../lib/Upload/ChunkingPlugin.php',
'OCA\\DAV\\Upload\\CleanupService' => __DIR__ . '/..' . '/../lib/Upload/CleanupService.php',
Expand Down
87 changes: 49 additions & 38 deletions apps/dav/lib/CalDAV/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -62,9 +67,6 @@ public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10
}

$this->config = $config;

// TODO: proper DI
$this->proxyMapper = \OC::$server->query(ProxyMapper::class);
}

/**
Expand Down Expand Up @@ -126,29 +128,60 @@ public function getPrincipalURI() {
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()) {
Expand Down Expand Up @@ -180,38 +213,16 @@ public function getACL() {
}

$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() {
Expand Down
14 changes: 12 additions & 2 deletions apps/dav/lib/CalDAV/Proxy/ProxyMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@
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[]
*/
Expand All @@ -52,7 +62,7 @@ public function getProxiesFor(string $proxyId): array {
}

/**
* @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[]
*/
Expand Down
42 changes: 9 additions & 33 deletions apps/dav/lib/CalDAV/ResourceBooking/AbstractPrincipalBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +46,9 @@ abstract class AbstractPrincipalBackend implements BackendInterface {
/** @var ILogger */
private $logger;

/** @var ProxyMapper */
private $proxyMapper;

/** @var string */
private $principalPrefix;

Expand Down Expand Up @@ -72,20 +77,24 @@ public function __construct(IDBConnection $dbConnection,
IUserSession $userSession,
IGroupManager $groupManager,
ILogger $logger,
ProxyMapper $proxyMapper,
string $principalPrefix,
string $dbPrefix,
string $cuType) {
$this->db = $dbConnection;
$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';
$this->dbForeignKeyName = $dbPrefix . '_id';
$this->cuType = $cuType;
}

use PrincipalProxyTrait;

/**
* Returns a list of principals based on a prefix.
*
Expand Down Expand Up @@ -215,39 +224,6 @@ public function getPrincipalById($id):?array {
return $this->rowToPrincipal($row, $metadata);
}

/**
* 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
Expand Down
14 changes: 12 additions & 2 deletions apps/dav/lib/CalDAV/ResourceBooking/ResourcePrincipalBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
14 changes: 12 additions & 2 deletions apps/dav/lib/CalDAV/ResourceBooking/RoomPrincipalBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
5 changes: 3 additions & 2 deletions apps/dav/lib/Command/CreateCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,8 +79,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$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();
Expand Down
Loading

0 comments on commit 7179b22

Please sign in to comment.