Skip to content

Commit

Permalink
Add command to synchronize specific ldap user
Browse files Browse the repository at this point in the history
  • Loading branch information
goyome committed Oct 19, 2021
1 parent d4486b8 commit da60aa8
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/user_ldap/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ A user logs into Nextcloud with their LDAP or AD credentials, and is granted acc
<command>OCA\User_LDAP\Command\SetConfig</command>
<command>OCA\User_LDAP\Command\ShowConfig</command>
<command>OCA\User_LDAP\Command\ShowRemnants</command>
<command>OCA\User_LDAP\Command\SyncUser</command>
<command>OCA\User_LDAP\Command\TestConfig</command>
</commands>

Expand Down
1 change: 1 addition & 0 deletions apps/user_ldap/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'OCA\\User_LDAP\\Command\\SetConfig' => $baseDir . '/../lib/Command/SetConfig.php',
'OCA\\User_LDAP\\Command\\ShowConfig' => $baseDir . '/../lib/Command/ShowConfig.php',
'OCA\\User_LDAP\\Command\\ShowRemnants' => $baseDir . '/../lib/Command/ShowRemnants.php',
'OCA\\User_LDAP\\Command\\SyncUser' => $baseDir . '/../lib/Command/SyncUser.php',
'OCA\\User_LDAP\\Command\\TestConfig' => $baseDir . '/../lib/Command/TestConfig.php',
'OCA\\User_LDAP\\Configuration' => $baseDir . '/../lib/Configuration.php',
'OCA\\User_LDAP\\Connection' => $baseDir . '/../lib/Connection.php',
Expand Down
5 changes: 3 additions & 2 deletions apps/user_ldap/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
class ComposerStaticInitUser_LDAP
{
public static $prefixLengthsPsr4 = array (
'O' =>
'O' =>
array (
'OCA\\User_LDAP\\' => 14,
),
);

public static $prefixDirsPsr4 = array (
'OCA\\User_LDAP\\' =>
'OCA\\User_LDAP\\' =>
array (
0 => __DIR__ . '/..' . '/../lib',
),
Expand All @@ -34,6 +34,7 @@ class ComposerStaticInitUser_LDAP
'OCA\\User_LDAP\\Command\\SetConfig' => __DIR__ . '/..' . '/../lib/Command/SetConfig.php',
'OCA\\User_LDAP\\Command\\ShowConfig' => __DIR__ . '/..' . '/../lib/Command/ShowConfig.php',
'OCA\\User_LDAP\\Command\\ShowRemnants' => __DIR__ . '/..' . '/../lib/Command/ShowRemnants.php',
'OCA\\User_LDAP\\Command\\SyncUser' => __DIR__ . '/..' . '/../lib/Command/SyncUser.php',
'OCA\\User_LDAP\\Command\\TestConfig' => __DIR__ . '/..' . '/../lib/Command/TestConfig.php',
'OCA\\User_LDAP\\Configuration' => __DIR__ . '/..' . '/../lib/Configuration.php',
'OCA\\User_LDAP\\Connection' => __DIR__ . '/..' . '/../lib/Connection.php',
Expand Down
122 changes: 122 additions & 0 deletions apps/user_ldap/lib/Command/SyncUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Guillaume Colson <guillaume.colson@univ-lorraine.fr>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\User_LDAP\Command;

use OC\ServerNotAvailableException;
use OCA\User_LDAP\AccessFactory;
use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\ConnectionFactory;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager;

use OCA\User_LDAP\User_Proxy;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IUserManager;
use OCP\Notification\IManager;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SyncUser extends Command {
/** @var \OCP\IConfig */
protected $ocConfig;
/** @var Manager */
protected $userManager;
/** @var IDBConnection */
protected $dbc;

public function __construct(IConfig $ocConfig) {
$this->ocConfig = $ocConfig;
$this->dbc = \OC::$server->getDatabaseConnection();
$this->userManager = new \OCA\User_LDAP\User\Manager(
\OC::$server->getConfig(),
new \OCA\User_LDAP\FilesystemHelper(),
new \OCA\User_LDAP\LogWrapper(),
\OC::$server->getAvatarManager(),
new \OCP\Image(),
\OC::$server->getUserManager(),
\OC::$server->getNotificationManager(),
\OC::$server->getShareManager()
);

parent::__construct();
}

protected function configure() {
$this
->setName('ldap:syncuser')
->setDescription('Synchronize user from LDAP immediately')
->addArgument(
'uid',
InputArgument::REQUIRED,
'the uid of the account to sync'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$helper = new Helper($this->ocConfig, \OC::$server->getDatabaseConnection());
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
$prefix = $this->ocConfig->getAppValue('user_ldap', 'background_sync_prefix', null);
$ldapWrapper = new LDAP();

$connectionFactory = new ConnectionFactory($ldapWrapper);
$connection = $connectionFactory->get($prefix);

$accessFactory = new AccessFactory(
$ldapWrapper,
$this->userManager,
$helper,
$this->ocConfig,
\OC::$server->getUserManager()
);

$access = $accessFactory->get($connection);
$access->setUserMapper(new UserMapping($this->dbc));

$loginName = $access->escapeFilterPart($input->getArgument('uid'));
$filter = str_replace('%uid', $loginName, $connection->ldapLoginFilter);

$results = $access->fetchListOfUsers(
$filter,
$access->userManager->getAttributes(),
1,
0,
true
);

if (count($results) > 0) {
$line = 'Sync of '. $results[0]['cn'][0] .' ('. $results[0]['uid'][0] .') done';
$output->writeln($line);
} else {
$output->writeln('No user found with uid : '.$input->getArgument('uid'));
}
return 0;
}
}

0 comments on commit da60aa8

Please sign in to comment.