summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/carddav/syncservice.php
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-01-13 20:56:25 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-01-14 12:10:45 +0100
commita91954907b51debaa9cabc4ccb89bfa6ebcc9c94 (patch)
tree591ac36c35ab6d6648dffd4647331fb67d66cbda /apps/dav/lib/carddav/syncservice.php
parent8f89e3520d74e1805dc46947742a17683ece6ca7 (diff)
downloadnextcloud-server-a91954907b51debaa9cabc4ccb89bfa6ebcc9c94.tar.gz
nextcloud-server-a91954907b51debaa9cabc4ccb89bfa6ebcc9c94.zip
Start updating system addressbook
Diffstat (limited to 'apps/dav/lib/carddav/syncservice.php')
-rw-r--r--apps/dav/lib/carddav/syncservice.php85
1 files changed, 82 insertions, 3 deletions
diff --git a/apps/dav/lib/carddav/syncservice.php b/apps/dav/lib/carddav/syncservice.php
index 30dc78c4877..97a46d62501 100644
--- a/apps/dav/lib/carddav/syncservice.php
+++ b/apps/dav/lib/carddav/syncservice.php
@@ -21,18 +21,27 @@
namespace OCA\DAV\CardDAV;
+use OCP\IUser;
+use OCP\IUserManager;
use Sabre\DAV\Client;
use Sabre\DAV\Xml\Response\MultiStatus;
use Sabre\DAV\Xml\Service;
-use Sabre\HTTP\ClientException;
+use Sabre\VObject\Reader;
class SyncService {
/** @var CardDavBackend */
private $backend;
- public function __construct(CardDavBackend $backend) {
+ /** @var IUserManager */
+ private $userManager;
+
+ /** @var array */
+ private $localSystemAddressBook;
+
+ public function __construct(CardDavBackend $backend, IUserManager $userManager) {
$this->backend = $backend;
+ $this->userManager = $userManager;
}
/**
@@ -80,7 +89,7 @@ class SyncService {
* @return array|null
* @throws \Sabre\DAV\Exception\BadRequest
*/
- protected function ensureSystemAddressBookExists($principal, $id, $properties) {
+ public function ensureSystemAddressBookExists($principal, $id, $properties) {
$book = $this->backend->getAddressBooksByUri($id);
if (!is_null($book)) {
return $book;
@@ -180,5 +189,75 @@ class SyncService {
return ['response' => $result, 'token' => $multiStatus->getSyncToken()];
}
+ /**
+ * @param IUser $user
+ */
+ public function updateUser($user) {
+ $systemAddressBook = $this->getLocalSystemAddressBook();
+ $addressBookId = $systemAddressBook['id'];
+ $converter = new Converter();
+ $name = $user->getBackendClassName();
+ $userId = $user->getUID();
+
+ $cardId = "$name:$userId.vcf";
+ $card = $this->backend->getCard($addressBookId, $cardId);
+ if ($card === false) {
+ $vCard = $converter->createCardFromUser($user);
+ $this->backend->createCard($addressBookId, $cardId, $vCard->serialize());
+ } else {
+ $vCard = Reader::read($card['carddata']);
+ if ($converter->updateCard($vCard, $user)) {
+ $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize());
+ }
+ }
+ }
+
+ /**
+ * @param IUser|string $userOrCardId
+ */
+ public function deleteUser($userOrCardId) {
+ $systemAddressBook = $this->getLocalSystemAddressBook();
+ if ($userOrCardId instanceof IUser){
+ $name = $userOrCardId->getBackendClassName();
+ $userId = $userOrCardId->getUID();
+
+ $userOrCardId = "$name:$userId.vcf";
+ }
+ $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId);
+ }
+
+ /**
+ * @return array|null
+ */
+ public function getLocalSystemAddressBook() {
+ if (is_null($this->localSystemAddressBook)) {
+ $systemPrincipal = "principals/system/system";
+ $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [
+ '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance'
+ ]);
+ }
+
+ return $this->localSystemAddressBook;
+ }
+
+ public function syncInstance(\Closure $progressCallback) {
+ $systemAddressBook = $this->getLocalSystemAddressBook();
+ $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) {
+ $this->updateUser($user);
+ $progressCallback();
+ });
+
+ // remove no longer existing
+ $allCards = $this->backend->getCards($systemAddressBook['id']);
+ foreach($allCards as $card) {
+ $vCard = Reader::read($card['carddata']);
+ $uid = $vCard->UID->getValue();
+ // load backend and see if user exists
+ if (!$this->userManager->userExists($uid)) {
+ $this->deleteUser($card['uri']);
+ }
+ }
+ }
+
}