Skip to content

Commit 4b9e480

Browse files
committed
Add command to synchronize specific ldap user
Signed-off-by: Guillaume COLSON <guillaume.colson@univ-lorraine.fr>
1 parent d4486b8 commit 4b9e480

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

apps/user_ldap/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ A user logs into Nextcloud with their LDAP or AD credentials, and is granted acc
5555
<command>OCA\User_LDAP\Command\SetConfig</command>
5656
<command>OCA\User_LDAP\Command\ShowConfig</command>
5757
<command>OCA\User_LDAP\Command\ShowRemnants</command>
58+
<command>OCA\User_LDAP\Command\SyncUser</command>
5859
<command>OCA\User_LDAP\Command\TestConfig</command>
5960
</commands>
6061

apps/user_ldap/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'OCA\\User_LDAP\\Command\\SetConfig' => $baseDir . '/../lib/Command/SetConfig.php',
2020
'OCA\\User_LDAP\\Command\\ShowConfig' => $baseDir . '/../lib/Command/ShowConfig.php',
2121
'OCA\\User_LDAP\\Command\\ShowRemnants' => $baseDir . '/../lib/Command/ShowRemnants.php',
22+
'OCA\\User_LDAP\\Command\\SyncUser' => $baseDir . '/../lib/Command/SyncUser.php',
2223
'OCA\\User_LDAP\\Command\\TestConfig' => $baseDir . '/../lib/Command/TestConfig.php',
2324
'OCA\\User_LDAP\\Configuration' => $baseDir . '/../lib/Configuration.php',
2425
'OCA\\User_LDAP\\Connection' => $baseDir . '/../lib/Connection.php',

apps/user_ldap/composer/composer/autoload_static.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
class ComposerStaticInitUser_LDAP
88
{
99
public static $prefixLengthsPsr4 = array (
10-
'O' =>
10+
'O' =>
1111
array (
1212
'OCA\\User_LDAP\\' => 14,
1313
),
1414
);
1515

1616
public static $prefixDirsPsr4 = array (
17-
'OCA\\User_LDAP\\' =>
17+
'OCA\\User_LDAP\\' =>
1818
array (
1919
0 => __DIR__ . '/..' . '/../lib',
2020
),
@@ -34,6 +34,7 @@ class ComposerStaticInitUser_LDAP
3434
'OCA\\User_LDAP\\Command\\SetConfig' => __DIR__ . '/..' . '/../lib/Command/SetConfig.php',
3535
'OCA\\User_LDAP\\Command\\ShowConfig' => __DIR__ . '/..' . '/../lib/Command/ShowConfig.php',
3636
'OCA\\User_LDAP\\Command\\ShowRemnants' => __DIR__ . '/..' . '/../lib/Command/ShowRemnants.php',
37+
'OCA\\User_LDAP\\Command\\SyncUser' => __DIR__ . '/..' . '/../lib/Command/SyncUser.php',
3738
'OCA\\User_LDAP\\Command\\TestConfig' => __DIR__ . '/..' . '/../lib/Command/TestConfig.php',
3839
'OCA\\User_LDAP\\Configuration' => __DIR__ . '/..' . '/../lib/Configuration.php',
3940
'OCA\\User_LDAP\\Connection' => __DIR__ . '/..' . '/../lib/Connection.php',
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2016, ownCloud, Inc.
4+
*
5+
* @author Guillaume Colson <guillaume.colson@univ-lorraine.fr>
6+
*
7+
* @license AGPL-3.0
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
namespace OCA\User_LDAP\Command;
23+
24+
use OC\ServerNotAvailableException;
25+
use OCA\User_LDAP\AccessFactory;
26+
use OCA\User_LDAP\Configuration;
27+
use OCA\User_LDAP\ConnectionFactory;
28+
use OCA\User_LDAP\Helper;
29+
use OCA\User_LDAP\LDAP;
30+
use OCA\User_LDAP\Mapping\UserMapping;
31+
use OCA\User_LDAP\User\Manager;
32+
33+
use OCA\User_LDAP\User_Proxy;
34+
use OCP\IAvatarManager;
35+
use OCP\IConfig;
36+
use OCP\IDBConnection;
37+
use OCP\IUserManager;
38+
use OCP\Notification\IManager;
39+
40+
use Symfony\Component\Console\Command\Command;
41+
use Symfony\Component\Console\Input\InputArgument;
42+
use Symfony\Component\Console\Input\InputInterface;
43+
use Symfony\Component\Console\Input\InputOption;
44+
use Symfony\Component\Console\Output\OutputInterface;
45+
46+
class SyncUser extends Command {
47+
/** @var \OCP\IConfig */
48+
protected $ocConfig;
49+
/** @var Manager */
50+
protected $userManager;
51+
/** @var IDBConnection */
52+
protected $dbc;
53+
54+
public function __construct(IConfig $ocConfig) {
55+
$this->ocConfig = $ocConfig;
56+
$this->dbc = \OC::$server->getDatabaseConnection();
57+
$this->userManager = new \OCA\User_LDAP\User\Manager(
58+
\OC::$server->getConfig(),
59+
new \OCA\User_LDAP\FilesystemHelper(),
60+
new \OCA\User_LDAP\LogWrapper(),
61+
\OC::$server->getAvatarManager(),
62+
new \OCP\Image(),
63+
\OC::$server->getUserManager(),
64+
\OC::$server->getNotificationManager(),
65+
\OC::$server->getShareManager()
66+
);
67+
68+
parent::__construct();
69+
}
70+
71+
protected function configure() {
72+
$this
73+
->setName('ldap:syncuser')
74+
->setDescription('Synchronize user from LDAP immediately')
75+
->addArgument(
76+
'uid',
77+
InputArgument::REQUIRED,
78+
'the uid of the account to sync'
79+
)
80+
;
81+
}
82+
83+
protected function execute(InputInterface $input, OutputInterface $output): int {
84+
$helper = new Helper($this->ocConfig, \OC::$server->getDatabaseConnection());
85+
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
86+
$prefix = $this->ocConfig->getAppValue('user_ldap', 'background_sync_prefix', null);
87+
$ldapWrapper = new LDAP();
88+
89+
$connectionFactory = new ConnectionFactory($ldapWrapper);
90+
$connection = $connectionFactory->get($prefix);
91+
92+
$accessFactory = new AccessFactory(
93+
$ldapWrapper,
94+
$this->userManager,
95+
$helper,
96+
$this->ocConfig,
97+
\OC::$server->getUserManager()
98+
);
99+
100+
$access = $accessFactory->get($connection);
101+
$access->setUserMapper(new UserMapping($this->dbc));
102+
103+
$loginName = $access->escapeFilterPart($input->getArgument('uid'));
104+
$filter = str_replace('%uid', $loginName, $connection->ldapLoginFilter);
105+
106+
$results = $access->fetchListOfUsers(
107+
$filter,
108+
$access->userManager->getAttributes(),
109+
1,
110+
0,
111+
true
112+
);
113+
114+
if (count($results) > 0) {
115+
$line = 'Sync of '. $results[0]['cn'][0] .' ('. $results[0]['uid'][0] .') done';
116+
$output->writeln($line);
117+
} else {
118+
$output->writeln('No user found with uid : '.$input->getArgument('uid'));
119+
}
120+
return 0;
121+
}
122+
}

0 commit comments

Comments
 (0)