Skip to content
Next Next commit
Share calendar to circle
* Allow user to share calendar with a circle
* Allow user to see calendars shared with his/her circles

Signed-off-by: Vinicius Cubas Brand <[email protected]>
  • Loading branch information
viniciuscb authored and georgehrke committed Mar 1, 2019
commit 5e37b52241381e5b0f57c58a88a5dbcf0870b3bd
4 changes: 4 additions & 0 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* @author Stefan Weil <[email protected]>
* @author Thomas Citharel <[email protected]>
* @author Thomas Müller <[email protected]>
* @author Vinicius Cubas Brand <[email protected]>
* @author Daniel Tygel <[email protected]>
*
* @license AGPL-3.0
*
Expand Down Expand Up @@ -301,6 +303,8 @@ function getCalendarsForUser($principalUri) {

// query for shared calendars
$principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true);
$principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal));

$principals = array_map(function($principal) {
return urldecode($principal);
}, $principals);
Expand Down
71 changes: 70 additions & 1 deletion apps/dav/lib/Connector/Sabre/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @author Thomas Tanghus <[email protected]>
* @author Vincent Petry <[email protected]>
* @author Georg Ehrke <[email protected]>
* @author Vinicius Cubas Brand <[email protected]>
* @author Daniel Tygel <[email protected]>
*
* @license AGPL-3.0
*
Expand Down Expand Up @@ -66,6 +68,9 @@ class Principal implements BackendInterface {
/** @var bool */
private $hasGroups;

/** @var bool */
private $hasCircles;

/**
* @param IUserManager $userManager
* @param IGroupManager $groupManager
Expand All @@ -86,7 +91,7 @@ public function __construct(IUserManager $userManager,
$this->userSession = $userSession;
$this->config = $config;
$this->principalPrefix = trim($principalPrefix, '/');
$this->hasGroups = ($principalPrefix === 'principals/users/');
$this->hasGroups = $this->hasCircles = ($principalPrefix === 'principals/users/');
}

/**
Expand Down Expand Up @@ -131,6 +136,8 @@ public function getPrincipalByPath($path) {
if ($user !== null) {
return $this->userToPrincipal($user);
}
} else if ($prefix === 'principals/circles') {
return $this->circleToPrincipal($name);
}
return null;
}
Expand Down Expand Up @@ -388,4 +395,66 @@ public function getPrincipalPrefix() {
return $this->principalPrefix;
}

/**
* @param string $circleUniqueId
* @return array|null
*/
protected function circleToPrincipal($circleUniqueId) {
if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
return null;
}

$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($circleUniqueId);

if (!$circle) {
return null;
}

$principal = [
'uri' => 'principals/circles/' . $circleUniqueId,
'{DAV:}displayname' => $circle->getName(),
];

return $principal;
}

/**
* Returns the list of circles a principal is a member of
*
* @param string $principal
* @param bool $needGroups
* @return array
* @throws Exception
*/
public function getCircleMembership($principal) {
if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
return [];
}

list($prefix, $name) = \Sabre\Uri\split($principal);

if ($this->hasCircles && $prefix === $this->principalPrefix) {
$user = $this->userManager->get($name);
if (!$user) {
throw new Exception('Principal not found');
}

$userSession = \OC::$server->getUserSession();
$currentUser = $userSession->getUser();

$userSession->setUser($user);
$circles = \OCA\Circles\Api\v1\Circles::joinedCircles();
$userSession->setUser($currentUser);

$circles = array_map(function($circle) {
/** @var \OCA\Circles\Model\Circle $group */
return 'principals/circles/' . urlencode($circle->getUniqueId());
}, $circles);

return $circles;

}
return [];
}

}
3 changes: 3 additions & 0 deletions apps/dav/lib/Connector/Sabre/SharesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @author Robin Appelman <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Vincent Petry <[email protected]>
* @author Vinicius Cubas Brand <[email protected]>
* @author Daniel Tygel <[email protected]>
*
* @license AGPL-3.0
*
Expand Down Expand Up @@ -125,6 +127,7 @@ private function getShareTypes(\OCP\Files\Node $node) {
\OCP\Share::SHARE_TYPE_REMOTE,
\OCP\Share::SHARE_TYPE_EMAIL,
\OCP\Share::SHARE_TYPE_ROOM,
\OCP\Share::SHARE_TYPE_CIRCLE,
];
foreach ($requestedShareTypes as $requestedShareType) {
// one of each type is enough to find out about the types
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/DAV/Sharing/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function shareWith($shareable, $element) {
}

$principal = explode('/', $parts[1], 3);
if (count($principal) !== 3 || $principal[0] !== 'principals' || !in_array($principal[1], ['users', 'groups'], true)) {
if (count($principal) !== 3 || $principal[0] !== 'principals' || !in_array($principal[1], ['users', 'groups','circles'], true)) {
// Invalid principal
return;
}
Expand Down