summaryrefslogtreecommitdiffstats
path: root/apps/dav/command/syncsystemaddressbook.php
blob: 01f4d08940ba708e96aab358d1ff840c8946ebb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace OCA\DAV\Command;

use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IUserManager;
use Sabre\CardDAV\Plugin;
use Sabre\VObject\Component\VCard;
use Sabre\VObject\Property\Text;
use Sabre\VObject\Reader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SyncSystemAddressBook extends Command {

	/** @var IUserManager */
	protected $userManager;

	/** @var \OCP\IDBConnection */
	protected $dbConnection;

	/** @var IConfig */
	protected $config;

	/** @var CardDavBackend */
	private $backend;

	/**
	 * @param IUserManager $userManager
	 * @param IDBConnection $dbConnection
	 */
	function __construct(IUserManager $userManager, IDBConnection $dbConnection, IConfig $config) {
		parent::__construct();
		$this->userManager = $userManager;
		$this->dbConnection = $dbConnection;
		$this->config = $config;
	}

	protected function configure() {
		$this
			->setName('dav:sync-system-addressbook')
			->setDescription('Synchronizes users to the system addressbook');
	}

	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();

		$output->writeln('Syncing users ...');
		$progress = new ProgressBar($output);
		$progress->start();
		$page = 0;
		foreach( $this->userManager->getBackends() as $backend) {
			$users = $backend->getUsers('', 50, $page++);
			foreach($users as $user) {
				$user = $this->userManager->get($user);
				$name = $user->getBackendClassName();
				$userId = $user->getUID();
				$displayName = $user->getDisplayName();
				$cardId = "$name:$userId.vcf";
				$card = $this->backend->getCard($systemAddressBook['id'], $cardId);
				if ($card === false) {
					$vCard = new VCard();
					$vCard->add(new Text($vCard, 'UID', $user->getUID()));
					$vCard->add(new Text($vCard, 'FN', $displayName));
//					$vCard->add(new Text($vCard, 'EMAIL', $user->getEMailAddress()));
					//$vCard->add(new Binary($vCard, 'PHOTO', $user->getAvatar()));
					$vCard->validate();
					$this->backend->createCard($systemAddressBook['id'], $cardId, $vCard->serialize());
				} else {
					$updated = false;
					$vCard = Reader::read($card['carddata']);
					if($vCard->FN !== $user->getDisplayName()) {
						$vCard->FN = new Text($vCard, 'FN', $displayName);
						$updated = true;
					}
					if ($updated) {
						$this->backend->updateCard($systemAddressBook['id'], $cardId, $vCard->serialize());
					}
				}
				$progress->advance();
			}
		}
		$progress->finish();
	}

	protected function ensureSystemAddressBookExists() {
		$book = $this->backend->getAddressBooksByUri('system');
		if (!is_null($book)) {
			return $book;
		}
		$systemPrincipal = "principals/system";
		$this->backend->createAddressBook($systemPrincipal, 'system', [
			'{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance'
		]);

		return $this->backend->getAddressBooksByUri('system');
	}
}