diff options
author | Bjoern Schiessle <bjoern@schiessle.org> | 2016-11-11 14:36:17 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2016-11-21 11:29:57 +0100 |
commit | 8f33d9d1c07c164b18be9d36e9e9f79b5bdfa8be (patch) | |
tree | 2f6d2e5b58666a2cddc19ff2aed5cf7fa32cf36d /apps | |
parent | 987995ac920a2d1e1251df80a482e3aef97c06f6 (diff) | |
download | nextcloud-server-8f33d9d1c07c164b18be9d36e9e9f79b5bdfa8be.tar.gz nextcloud-server-8f33d9d1c07c164b18be9d36e9e9f79b5bdfa8be.zip |
update system address book if the user change the personal settings
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/dav/lib/AppInfo/Application.php | 10 | ||||
-rw-r--r-- | apps/dav/lib/CardDAV/Converter.php | 144 | ||||
-rw-r--r-- | apps/dav/lib/CardDAV/SyncService.php | 27 | ||||
-rw-r--r-- | apps/dav/lib/HookManager.php | 9 |
4 files changed, 96 insertions, 94 deletions
diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 844e0780ffb..80d2946ab07 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -101,6 +101,12 @@ class Application extends App { } }); + $dispatcher->addListener('OC\AccountManager::userUpdated', function(GenericEvent $event) { + $user = $event->getSubject(); + $syncService = $this->getContainer()->query(SyncService::class); + $syncService->updateUser($user); + }); + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', function(GenericEvent $event) { /** @var Backend $backend */ $backend = $this->getContainer()->query(Backend::class); @@ -136,6 +142,10 @@ class Application extends App { ); }); + $dispatcher->addListener('OC\AccountManager::userUpdated', function(GenericEvent $event) { + error_log("hello"); + }); + $listener = function(GenericEvent $event, $eventName) { /** @var Backend $backend */ $backend = $this->getContainer()->query(Backend::class); diff --git a/apps/dav/lib/CardDAV/Converter.php b/apps/dav/lib/CardDAV/Converter.php index d1fb754017e..065c5494ec3 100644 --- a/apps/dav/lib/CardDAV/Converter.php +++ b/apps/dav/lib/CardDAV/Converter.php @@ -22,6 +22,7 @@ namespace OCA\DAV\CardDAV; +use OC\Accounts\AccountManager; use OCP\IImage; use OCP\IUser; use Sabre\VObject\Component\VCard; @@ -29,109 +30,76 @@ use Sabre\VObject\Property\Text; class Converter { + /** @var AccountManager */ + private $accountManager; + /** - * @param IUser $user - * @return VCard + * Converter constructor. + * + * @param AccountManager $accountManager */ - public function createCardFromUser(IUser $user) { - - $uid = $user->getUID(); - $displayName = $user->getDisplayName(); - $displayName = empty($displayName ) ? $uid : $displayName; - $emailAddress = $user->getEMailAddress(); - $cloudId = $user->getCloudId(); - $image = $this->getAvatarImage($user); - - $vCard = new VCard(); - $vCard->VERSION = '3.0'; - $vCard->UID = $uid; - if (!empty($displayName)) { - $vCard->FN = $displayName; - $vCard->N = $this->splitFullName($displayName); - } - if (!empty($emailAddress)) { - $vCard->add(new Text($vCard, 'EMAIL', $emailAddress, ['TYPE' => 'OTHER'])); - } - if (!empty($cloudId)) { - $vCard->CLOUD = $cloudId; - } - if ($image) { - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); - } - $vCard->validate(); - - return $vCard; + public function __construct(AccountManager $accountManager) { + $this->accountManager = $accountManager; } /** - * @param VCard $vCard * @param IUser $user - * @return bool + * @return VCard|null */ - public function updateCard(VCard $vCard, IUser $user) { + public function createCardFromUser(IUser $user) { + + $userData = $this->accountManager->getUser($user); + $uid = $user->getUID(); - $displayName = $user->getDisplayName(); - $displayName = empty($displayName ) ? $uid : $displayName; - $emailAddress = $user->getEMailAddress(); $cloudId = $user->getCloudId(); $image = $this->getAvatarImage($user); - $updated = false; - if($this->propertyNeedsUpdate($vCard, 'FN', $displayName)) { - $vCard->FN = new Text($vCard, 'FN', $displayName); - unset($vCard->N); - $vCard->add(new Text($vCard, 'N', $this->splitFullName($displayName))); - $updated = true; - } - if($this->propertyNeedsUpdate($vCard, 'EMAIL', $emailAddress)) { - $vCard->EMAIL = new Text($vCard, 'EMAIL', $emailAddress); - $updated = true; - } - if($this->propertyNeedsUpdate($vCard, 'CLOUD', $cloudId)) { - $vCard->CLOUD = new Text($vCard, 'CLOUD', $cloudId); - $updated = true; - } - - if($this->propertyNeedsUpdate($vCard, 'PHOTO', $image)) { - unset($vCard->PHOTO); - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); - $updated = true; - } - - if (empty($emailAddress) && !is_null($vCard->EMAIL)) { - unset($vCard->EMAIL); - $updated = true; - } - if (empty($cloudId) && !is_null($vCard->CLOUD)) { - unset($vCard->CLOUD); - $updated = true; - } - if (empty($image) && !is_null($vCard->PHOTO)) { - unset($vCard->PHOTO); - $updated = true; + $vCard = new VCard(); + $vCard->add(new Text($vCard, 'UID', $uid)); + + $publish = false; + + foreach ($userData as $property => $value) { + if ($value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || + $value['scope'] === AccountManager::VISIBILITY_PUBLIC + ) { + $publish = true; + switch ($property) { + case AccountManager::PROPERTY_DISPLAYNAME: + $vCard->add(new Text($vCard, 'FN', $value['value'])); + $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); + break; + case AccountManager::PROPERTY_AVATAR: + if ($image !== null) { + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); + } + break; + case AccountManager::PROPERTY_EMAIL: + $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); + break; + case AccountManager::PROPERTY_WEBSITE: + $vCard->add(new Text($vCard, 'URL', $value['value'])); + break; + case AccountManager::PROPERTY_PHONE: + $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); + break; + case AccountManager::PROPERTY_ADDRESS: + $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); + break; + case AccountManager::PROPERTY_TWITTER: + $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); + break; + } + } } - return $updated; - } - - /** - * @param VCard $vCard - * @param string $name - * @param string|IImage $newValue - * @return bool - */ - private function propertyNeedsUpdate(VCard $vCard, $name, $newValue) { - if (is_null($newValue)) { - return false; + if ($publish && !empty($cloudId)) { + $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); + $vCard->validate(); + return $vCard; } - $value = $vCard->__get($name); - if (!is_null($value)) { - $value = $value->getValue(); - $newValue = $newValue instanceof IImage ? $newValue->data() : $newValue; - return $value !== $newValue; - } - return true; + return null; } /** diff --git a/apps/dav/lib/CardDAV/SyncService.php b/apps/dav/lib/CardDAV/SyncService.php index 1ad37da6bfe..1293d8ae8a0 100644 --- a/apps/dav/lib/CardDAV/SyncService.php +++ b/apps/dav/lib/CardDAV/SyncService.php @@ -24,6 +24,7 @@ namespace OCA\DAV\CardDAV; +use OC\Accounts\AccountManager; use OCP\AppFramework\Http; use OCP\ILogger; use OCP\IUser; @@ -48,10 +49,22 @@ class SyncService { /** @var array */ private $localSystemAddressBook; - public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger) { + /** @var AccountManager */ + private $accountManager; + + /** + * SyncService constructor. + * + * @param CardDavBackend $backend + * @param IUserManager $userManager + * @param ILogger $logger + * @param AccountManager $accountManager + */ + public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, AccountManager $accountManager) { $this->backend = $backend; $this->userManager = $userManager; $this->logger = $logger; + $this->accountManager = $accountManager; } /** @@ -215,7 +228,7 @@ class SyncService { public function updateUser($user) { $systemAddressBook = $this->getLocalSystemAddressBook(); $addressBookId = $systemAddressBook['id']; - $converter = new Converter(); + $converter = new Converter($this->accountManager); $name = $user->getBackendClassName(); $userId = $user->getUID(); @@ -223,10 +236,14 @@ class SyncService { $card = $this->backend->getCard($addressBookId, $cardId); if ($card === false) { $vCard = $converter->createCardFromUser($user); - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); + if ($vCard !== null) { + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); + } } else { - $vCard = Reader::read($card['carddata']); - if ($converter->updateCard($vCard, $user)) { + $vCard = $converter->createCardFromUser($user); + if (is_null($vCard)) { + $this->backend->deleteCard($addressBookId, $cardId); + } else { $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); } } diff --git a/apps/dav/lib/HookManager.php b/apps/dav/lib/HookManager.php index 247d4b291af..26f3895a459 100644 --- a/apps/dav/lib/HookManager.php +++ b/apps/dav/lib/HookManager.php @@ -27,6 +27,8 @@ use OCA\DAV\CardDAV\SyncService; use OCP\IUser; use OCP\IUserManager; use OCP\Util; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\GenericEvent; class HookManager { @@ -51,14 +53,19 @@ class HookManager { /** @var array */ private $addressBooksToDelete; + /** @var EventDispatcher */ + private $eventDispatcher; + public function __construct(IUserManager $userManager, SyncService $syncService, CalDavBackend $calDav, - CardDavBackend $cardDav) { + CardDavBackend $cardDav, + EventDispatcher $eventDispatcher) { $this->userManager = $userManager; $this->syncService = $syncService; $this->calDav = $calDav; $this->cardDav = $cardDav; + $this->eventDispatcher = $eventDispatcher; } public function setup() { |