Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for full contact info #581

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions lib/LDAPUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCP\LDAP\IDeletionFlagSupport;
use OCP\LDAP\ILDAPProvider;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class LDAPUserManager implements ILDAPUserPlugin {
/** @var ILDAPProvider */
Expand Down Expand Up @@ -74,6 +75,10 @@ public function __construct(IUserManager $userManager, IUserSession $userSession
$this->logger = $logger;

$this->userManager->listen('\OC\User', 'changeUser', [$this, 'changeUserHook']);
if ($this->configuration->canUpdateContactInfo()) {
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->addListener('OC\AccountManager::userUpdated', [$this, 'userUpdatedHook']);
}
$this->makeLdapBackendFirst();
}

Expand Down Expand Up @@ -441,6 +446,96 @@ public function changeUserHook(IUser $user, string $feature, $attr1, $attr2): vo
}
}

public function userUpdatedHook(GenericEvent $event): void {
$user = $event->getSubject();
$args = $event->getArguments();

try {
$userDN = $this->getUserDN($user->getUID());
} catch (Exception $e) {
return;
}

$connection = $this->ldapProvider->getLDAPConnection($user->getUID());

if (!is_resource($connection) && !is_object($connection)) {
$this->logger->debug('LDAP resource not available', ['app' => 'ldap_write_support']);
throw new ServerNotAvailableException('LDAP server is not available');
}

try {
$updates = array();
$urls = array();
$deletes = array();
foreach ($args as $arg) {
// `displayname` and `email` also exist and could replace above until
// non-Symfony calls are standardized.
// `headline`, `twitter`, and `fediverse` are also available.
switch ($arg['name']) {
case 'phone':
if ($arg['value'] == "") {
$deletes['telephoneNumber'] = array();
} else {
$updates['telephoneNumber'] = $arg['value'];
}
break;
case 'address':
if ($arg['value'] == "") {
$deletes['l'] = array();
} else {
$updates['l'] = $arg['value'];
}
break;
case 'website':
if ($arg['value'] == "") {
$deletes['wWWHomePage'] = array();
} else {
$updates['wWWHomePage'] = $arg['value'];
}
break;
case 'organisation':
if ($arg['value'] == "") {
$deletes['department'] = array();
} else {
$updates['department'] = $arg['value'];
}
break;
case 'role':
if ($arg['value'] == "") {
$deletes['title'] = array();
} else {
$updates['title'] = $arg['value'];
}
break;
case 'biography':
if ($arg['value'] == "") {
$deletes['description'] = array();
} else {
$updates['description'] = $arg['value'];
}
break;
}
}
if (ldap_mod_replace($connection, $userDN, $updates)) {
if (count($deletes) > 0) {
try {
@ldap_mod_del($connection, $userDN, $deletes);
} catch(Exception $e) {
// Nothing required
}
}
return;
}
throw new HintException('Failed to set LDAP information');
} catch (ConstraintViolationException $e) {
throw new HintException(
$e->getMessage(),
$this->l10n->t('LDAP change rejected'),
$e->getCode()
);
}
}

private function getUserDN($uid): string {
return $this->ldapProvider->getUserDN($uid);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public function hasAvatarPermission(): bool {
return $this->config->getAppValue('ldap_write_support', 'hasAvatarPermission', '1') === '1';
}

public function canUpdateContactInfo(): bool {
return $this->config->getAppValue('ldap_write_support', 'canUpdateContactInfo', '1') === '1';
}

public function getUserTemplate() {
return $this->config->getAppValue(
Application::APP_ID,
Expand Down
1 change: 1 addition & 0 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function getForm() {
'createRequireActorFromLdap' => $this->config->isLdapActorRequired(),
'createPreventFallback' => $this->config->isPreventFallback(),
'hasAvatarPermission' => $this->config->hasAvatarPermission(),
'canUpdateContactInfo' => $this->config->canUpdateContactInfo(),
'newUserRequireEmail' => $this->config->isRequireEmail(),
'newUserGenerateUserID' => $this->config->isGenerateUserId(),
]
Expand Down
4 changes: 4 additions & 0 deletions src/components/AdminSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
@change.stop.prevent="toggleSwitch('hasAvatarPermission', !switches.hasAvatarPermission)">
{{ t('ldap_write_support', 'Allow users to set their avatar') }}
</NcActionCheckbox>
<NcActionCheckbox :checked="switches.canUpdateContactInfo"
@change.stop.prevent="toggleSwitch('canUpdateContactInfo', !switches.canUpdateContactInfo)">
{{ t('ldap_write_support', 'Update LDAP/AD when contact info changes') }}
</NcActionCheckbox>
</ul>
<h3>{{ t('ldap_write_support', 'User template') }}</h3>
<p>{{ t('ldap_write_support', 'LDIF template for creating users. Following placeholders may be used') }}</p>
Expand Down