Skip to content

Commit

Permalink
Merge pull request #1946 from nextcloud/federated-sharing-persona-set…
Browse files Browse the repository at this point in the history
…tings

Add more personal information fields to the settings page for enhanced federated sharing
  • Loading branch information
LukasReschke authored Nov 21, 2016
2 parents ba9b17c + a1ca54a commit 94004cf
Show file tree
Hide file tree
Showing 51 changed files with 2,524 additions and 424 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
!/apps/files_sharing
!/apps/files_trashbin
!/apps/files_versions
!/apps/lookup_server_connector
!/apps/user_ldap
!/apps/provisioning_api
!/apps/systemtags
Expand Down
6 changes: 6 additions & 0 deletions apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public function registerHooks() {
}
});

$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);
Expand Down
144 changes: 56 additions & 88 deletions apps/dav/lib/CardDAV/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,116 +22,84 @@

namespace OCA\DAV\CardDAV;

use OC\Accounts\AccountManager;
use OCP\IImage;
use OCP\IUser;
use Sabre\VObject\Component\VCard;
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;
}

/**
Expand Down
27 changes: 22 additions & 5 deletions apps/dav/lib/CardDAV/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace OCA\DAV\CardDAV;

use OC\Accounts\AccountManager;
use OCP\AppFramework\Http;
use OCP\ILogger;
use OCP\IUser;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -215,18 +228,22 @@ private function parseMultiStatus($body) {
public function updateUser($user) {
$systemAddressBook = $this->getLocalSystemAddressBook();
$addressBookId = $systemAddressBook['id'];
$converter = new Converter();
$converter = new Converter($this->accountManager);
$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());
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());
}
}
Expand Down
9 changes: 8 additions & 1 deletion apps/dav/lib/HookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;

class HookManager {

Expand All @@ -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() {
Expand Down
Loading

0 comments on commit 94004cf

Please sign in to comment.