diff --git a/apps/user_ldap/appinfo/info.xml b/apps/user_ldap/appinfo/info.xml index 99d35ae5d8f58..7631fb79c64b0 100644 --- a/apps/user_ldap/appinfo/info.xml +++ b/apps/user_ldap/appinfo/info.xml @@ -55,6 +55,7 @@ A user logs into Nextcloud with their LDAP or AD credentials, and is granted acc OCA\User_LDAP\Command\SetConfig OCA\User_LDAP\Command\ShowConfig OCA\User_LDAP\Command\ShowRemnants + OCA\User_LDAP\Command\SyncUser OCA\User_LDAP\Command\TestConfig diff --git a/apps/user_ldap/composer/composer/autoload_classmap.php b/apps/user_ldap/composer/composer/autoload_classmap.php index 34f17532e5b88..1804859f950db 100644 --- a/apps/user_ldap/composer/composer/autoload_classmap.php +++ b/apps/user_ldap/composer/composer/autoload_classmap.php @@ -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', diff --git a/apps/user_ldap/composer/composer/autoload_static.php b/apps/user_ldap/composer/composer/autoload_static.php index 1973a8b31837b..99409cd81bb3f 100644 --- a/apps/user_ldap/composer/composer/autoload_static.php +++ b/apps/user_ldap/composer/composer/autoload_static.php @@ -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', ), @@ -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', diff --git a/apps/user_ldap/lib/Command/SyncUser.php b/apps/user_ldap/lib/Command/SyncUser.php new file mode 100644 index 0000000000000..fdd234c743e17 --- /dev/null +++ b/apps/user_ldap/lib/Command/SyncUser.php @@ -0,0 +1,122 @@ + + * + * @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 + * + */ +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; + } +}