Ejemplo n.º 1
0
 /**
  * @param string $url
  * @param string $userName
  * @param string $sharedSecret
  * @param string $syncToken
  * @param int $targetBookId
  * @param string $targetPrincipal
  * @param array $targetProperties
  * @return string
  */
 public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties)
 {
     // 1. create addressbook
     $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
     $addressBookId = $book['id'];
     // 2. query changes
     $response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken);
     // 3. apply changes
     // TODO: use multi-get for download
     foreach ($response['response'] as $resource => $status) {
         $cardUri = basename($resource);
         if (isset($status[200])) {
             $vCard = $this->download($url, $sharedSecret, $resource);
             $existingCard = $this->backend->getCard($addressBookId, $cardUri);
             if ($existingCard === false) {
                 $this->backend->createCard($addressBookId, $cardUri, $vCard['body']);
             } else {
                 $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']);
             }
         } else {
             $this->backend->deleteCard($addressBookId, $cardUri);
         }
     }
     return $response['token'];
 }
Ejemplo n.º 2
0
 /**
  * @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);
 }
Ejemplo n.º 3
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $principalBackend = new Principal($this->config, $this->userManager);
     $this->backend = new CardDavBackend($this->dbConnection, $principalBackend);
     // ensure system addressbook exists
     $systemAddressBook = $this->ensureSystemAddressBookExists();
     $converter = new Converter();
     $output->writeln('Syncing users ...');
     $progress = new ProgressBar($output);
     $progress->start();
     $this->userManager->callForAllUsers(function ($user) use($systemAddressBook, $converter, $progress) {
         /** @var IUser $user */
         $name = $user->getBackendClassName();
         $userId = $user->getUID();
         $cardId = "{$name}:{$userId}.vcf";
         $card = $this->backend->getCard($systemAddressBook['id'], $cardId);
         if ($card === false) {
             $vCard = $converter->createCardFromUser($user);
             $this->backend->createCard($systemAddressBook['id'], $cardId, $vCard->serialize());
         } else {
             $vCard = Reader::read($card['carddata']);
             if ($converter->updateCard($vCard, $user)) {
                 $this->backend->updateCard($systemAddressBook['id'], $cardId, $vCard->serialize());
             }
         }
         $progress->advance();
     });
     // 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->backend->deleteCard($systemAddressBook['id'], $card['uri']);
         }
     }
     $progress->finish();
     $output->writeln('');
 }
Ejemplo n.º 4
0
 public function testDeleteWithoutCard()
 {
     $this->backend = $this->getMockBuilder('OCA\\DAV\\CardDAV\\CardDavBackend')->setConstructorArgs([$this->db, $this->principal, null])->setMethods(['getCardId', 'addChange', 'purgeProperties', 'updateProperties'])->getMock();
     // create a new address book
     $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
     $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
     $this->assertEquals(1, count($books));
     $bookId = $books[0]['id'];
     $uri = $this->getUniqueID('card');
     // create a new address book
     $this->backend->expects($this->once())->method('getCardId')->with($bookId, $uri)->willThrowException(new \InvalidArgumentException());
     $this->backend->expects($this->exactly(2))->method('addChange')->withConsecutive([$bookId, $uri, 1], [$bookId, $uri, 3]);
     $this->backend->expects($this->never())->method('purgeProperties');
     // create a card
     $this->backend->createCard($bookId, $uri, '');
     // delete the card
     $this->assertTrue($this->backend->deleteCard($bookId, $uri));
 }
Ejemplo n.º 5
0
 public function testMultiCard()
 {
     $this->backend = $this->getMockBuilder('OCA\\DAV\\CardDAV\\CardDavBackend')->setConstructorArgs([$this->db, $this->principal])->setMethods(['updateProperties'])->getMock();
     // create a new address book
     $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
     $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
     $this->assertEquals(1, count($books));
     $bookId = $books[0]['id'];
     // create a card
     $uri0 = $this->getUniqueID('card');
     $this->backend->createCard($bookId, $uri0, '');
     $uri1 = $this->getUniqueID('card');
     $this->backend->createCard($bookId, $uri1, '');
     $uri2 = $this->getUniqueID('card');
     $this->backend->createCard($bookId, $uri2, '');
     // get all the cards
     $cards = $this->backend->getCards($bookId);
     $this->assertEquals(3, count($cards));
     $this->assertEquals('', $cards[0]['carddata']);
     $this->assertEquals('', $cards[1]['carddata']);
     $this->assertEquals('', $cards[2]['carddata']);
     // get the cards
     $cards = $this->backend->getMultipleCards($bookId, [$uri1, $uri2]);
     $this->assertEquals(2, count($cards));
     foreach ($cards as $card) {
         $this->assertArrayHasKey('id', $card);
         $this->assertArrayHasKey('uri', $card);
         $this->assertArrayHasKey('lastmodified', $card);
         $this->assertArrayHasKey('etag', $card);
         $this->assertArrayHasKey('size', $card);
         $this->assertEquals('', $card['carddata']);
     }
     // delete the card
     $this->backend->deleteCard($bookId, $uri0);
     $this->backend->deleteCard($bookId, $uri1);
     $this->backend->deleteCard($bookId, $uri2);
     $cards = $this->backend->getCards($bookId);
     $this->assertEquals(0, count($cards));
 }
Ejemplo n.º 6
0
 public function testMultiCard()
 {
     // create a new address book
     $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
     $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
     $this->assertEquals(1, count($books));
     $bookId = $books[0]['id'];
     // create a card
     $uri0 = $this->getUniqueID('card');
     $this->backend->createCard($bookId, $uri0, '');
     $uri1 = $this->getUniqueID('card');
     $this->backend->createCard($bookId, $uri1, '');
     $uri2 = $this->getUniqueID('card');
     $this->backend->createCard($bookId, $uri2, '');
     // get all the cards
     $cards = $this->backend->getCards($bookId);
     $this->assertEquals(3, count($cards));
     $this->assertEquals('', $cards[0]['carddata']);
     $this->assertEquals('', $cards[1]['carddata']);
     $this->assertEquals('', $cards[2]['carddata']);
     // get the cards
     $cards = $this->backend->getMultipleCards($bookId, [$uri1, $uri2]);
     $this->assertEquals(2, count($cards));
     foreach ($cards as $card) {
         $this->assertArrayHasKey('id', $card);
         $this->assertArrayHasKey('uri', $card);
         $this->assertArrayHasKey('lastmodified', $card);
         $this->assertArrayHasKey('etag', $card);
         $this->assertArrayHasKey('size', $card);
         $this->assertEquals('', $card['carddata']);
     }
     // delete the card
     $this->backend->deleteCard($bookId, $uri0);
     $this->backend->deleteCard($bookId, $uri1);
     $this->backend->deleteCard($bookId, $uri2);
     $cards = $this->backend->getCards($bookId);
     $this->assertEquals(0, count($cards));
 }
Ejemplo n.º 7
0
 /**
  * @param object $id the unique identifier to a contact
  * @return bool successful or not
  * @since 5.0.0
  */
 public function delete($id)
 {
     $uri = $this->backend->getCardUri($id);
     return $this->backend->deleteCard($this->addressBookInfo['id'], $uri);
 }