Skip to content

Commit

Permalink
Merge pull request #16455 from nextcloud/enh/noid/ldap-update-user-cmd
Browse files Browse the repository at this point in the history
adds an --update flag to check-user for manual sync of the ldap record
  • Loading branch information
rullzer committed Jul 18, 2019
2 parents b291d9b + 40c9a74 commit 3011449
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions apps/user_ldap/lib/Command/CheckUser.php
Expand Up @@ -29,32 +29,31 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use OCA\User_LDAP\User\DeletedUsersIndex;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\Helper as LDAPHelper;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\User_Proxy;

class CheckUser extends Command {
/** @var \OCA\User_LDAP\User_Proxy */
/** @var User_Proxy */
protected $backend;

/** @var \OCA\User_LDAP\Helper */
/** @var Helper */
protected $helper;

/** @var \OCA\User_LDAP\User\DeletedUsersIndex */
/** @var DeletedUsersIndex */
protected $dui;

/** @var \OCA\User_LDAP\Mapping\UserMapping */
/** @var UserMapping */
protected $mapping;

/**
* @param User_Proxy $uBackend
* @param LDAPHelper $helper
* @param Helper $helper
* @param DeletedUsersIndex $dui
* @param UserMapping $mapping
*/
public function __construct(User_Proxy $uBackend, LDAPHelper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
$this->backend = $uBackend;
$this->helper = $helper;
$this->dui = $dui;
Expand All @@ -77,6 +76,12 @@ protected function configure() {
InputOption::VALUE_NONE,
'ignores disabled LDAP configuration'
)
->addOption(
'update',
null,
InputOption::VALUE_NONE,
'syncs values from LDAP'
)
;
}

Expand All @@ -88,6 +93,9 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$exists = $this->backend->userExistsOnLDAP($uid);
if($exists === true) {
$output->writeln('The user is still available on LDAP.');
if($input->getOption('update')) {
$this->updateUser($uid, $output);
}
return;
}

Expand Down Expand Up @@ -133,4 +141,26 @@ protected function isAllowed($force) {
return true;
}

private function updateUser(string $uid, OutputInterface $output): void {
try {
$access = $this->backend->getLDAPAccess($uid);
$attrs = $access->userManager->getAttributes();
$user = $access->userManager->get($uid);
$avatarAttributes = $access->getConnection()->resolveRule('avatar');
$result = $access->search('objectclass=*', [$user->getDN()], $attrs, 1, 0);
foreach ($result[0] as $attribute => $valueSet) {
$output->writeln(' ' . $attribute . ': ');
foreach ($valueSet as $value) {
if (in_array($attribute, $avatarAttributes)) {
$value = '{ImageData}';
}
$output->writeln(' ' . $value);
}
}
$access->batchApplyUserAttributes($result);
} catch (\Exception $e) {
$output->writeln('<error>Error while trying to lookup and update attributes from LDAP</error>');
}
}

}

0 comments on commit 3011449

Please sign in to comment.