Skip to content
1 change: 1 addition & 0 deletions apps/dav/appinfo/v1/caldav.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getConfig(),
\OC::$server->getAppManager(),
'principals/'
);
$db = \OC::$server->getDatabaseConnection();
Expand Down
1 change: 1 addition & 0 deletions apps/dav/appinfo/v1/carddav.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getConfig(),
\OC::$server->getAppManager(),
'principals/'
);
$db = \OC::$server->getDatabaseConnection();
Expand Down
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
4 changes: 3 additions & 1 deletion apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ function getAddressBooksForUser($principalUri) {
}
$result->closeCursor();

// query for shared calendars
// query for shared addressbooks
$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
3 changes: 2 additions & 1 deletion apps/dav/lib/Command/CreateCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->groupManager,
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getConfig()
\OC::$server->getConfig(),
\OC::$server->getAppManager()
);
$random = \OC::$server->getSecureRandom();
$logger = \OC::$server->getLogger();
Expand Down
82 changes: 81 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 All @@ -32,6 +34,9 @@

namespace OCA\DAV\Connector\Sabre;

use OCA\Circles\Exceptions\CircleDoesNotExistException;
use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -60,12 +65,18 @@ class Principal implements BackendInterface {
/** @var IConfig */
private $config;

/** @var IAppManager */
private $appManager;

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

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

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

/**
* @param IUserManager $userManager
* @param IGroupManager $groupManager
Expand All @@ -79,14 +90,16 @@ public function __construct(IUserManager $userManager,
IShareManager $shareManager,
IUserSession $userSession,
IConfig $config,
IAppManager $appManager,
$principalPrefix = 'principals/users/') {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->shareManager = $shareManager;
$this->userSession = $userSession;
$this->config = $config;
$this->appManager = $appManager;
$this->principalPrefix = trim($principalPrefix, '/');
$this->hasGroups = ($principalPrefix === 'principals/users/');
$this->hasGroups = $this->hasCircles = ($principalPrefix === 'principals/users/');
}

/**
Expand Down Expand Up @@ -131,6 +144,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 +403,69 @@ public function getPrincipalPrefix() {
return $this->principalPrefix;
}

/**
* @param string $circleUniqueId
* @return array|null
* @suppress PhanUndeclaredClassMethod
* @suppress PhanUndeclaredClassCatch
*/
protected function circleToPrincipal($circleUniqueId) {
if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) {
return null;
}

try {
$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($circleUniqueId, true);
} catch(QueryException $ex) {
return null;
} catch(CircleDoesNotExistException $ex) {
return null;
}

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
* @suppress PhanUndeclaredClassMethod
*/
public function getCircleMembership($principal):array {
if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) {
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');
}

$circles = \OCA\Circles\Api\v1\Circles::joinedCircles($name, true);

$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
3 changes: 2 additions & 1 deletion apps/dav/lib/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function __construct() {
$groupManager,
$shareManager,
\OC::$server->getUserSession(),
$config
$config,
\OC::$server->getAppManager()
);
$groupPrincipalBackend = new GroupPrincipalBackend($groupManager, $userSession, $shareManager, $l10n);
$calendarResourcePrincipalBackend = new ResourcePrincipalBackend($db, $userSession, $groupManager, $logger);
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\ILogger;
Expand Down Expand Up @@ -83,6 +84,7 @@ public function setUp() {
$this->createMock(ShareManager::class),
$this->createMock(IUserSession::class),
$this->createMock(IConfig::class),
$this->createMock(IAppManager::class),
])
->setMethods(['getPrincipalByPath', 'getGroupMembership'])
->getMock();
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/tests/unit/CardDAV/CardDavBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCA\DAV\CardDAV\AddressBook;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\App\IAppManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
Expand Down Expand Up @@ -131,6 +132,7 @@ public function setUp() {
$this->createMock(ShareManager::class),
$this->createMock(IUserSession::class),
$this->createMock(IConfig::class),
$this->createMock(IAppManager::class),
])
->setMethods(['getPrincipalByPath', 'getGroupMembership'])
->getMock();
Expand Down
9 changes: 8 additions & 1 deletion apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace OCA\DAV\Tests\unit\Connector\Sabre;

use OC\User\User;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -59,19 +60,25 @@ class PrincipalTest extends TestCase {
/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
private $config;

/** @var IAppManager | \PHPUnit_Framework_MockObject_MockObject */
private $appManager;

public function setUp() {
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->shareManager = $this->createMock(IManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->config = $this->createMock(IConfig::class);
$this->appManager = $this->createMock(IAppManager::class);

$this->connector = new \OCA\DAV\Connector\Sabre\Principal(
$this->userManager,
$this->groupManager,
$this->shareManager,
$this->userSession,
$this->config);
$this->config,
$this->appManager
);
parent::setUp();
}

Expand Down
3 changes: 2 additions & 1 deletion apps/files_trashbin/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function __construct (array $urlParams = []) {
\OC::$server->getGroupManager(),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getConfig()
\OC::$server->getConfig(),
\OC::$server->getAppManager()
);
});

Expand Down
3 changes: 2 additions & 1 deletion apps/files_versions/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function __construct(array $urlParams = array()) {
$server->getGroupManager(),
$server->getShareManager(),
$server->getUserSession(),
$server->getConfig()
$server->getConfig(),
$server->getAppManager()
);
});

Expand Down