Skip to content

Commit f5d1365

Browse files
authored
Merge pull request #30938 from nextcloud/fix/user_ldap-fix-check-ldap-new-user
Fix ldap:check-user method for newly created LDAP users
2 parents ac4978e + 44680b5 commit f5d1365

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

apps/user_ldap/lib/Access.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public function dn2groupname($fdn, $ldapName = null) {
488488
/**
489489
* returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure
490490
*
491-
* @param string $dn the dn of the user object
491+
* @param string $fdn the dn of the user object
492492
* @param string $ldapName optional, the display name of the object
493493
* @return string|false with with the name to use in Nextcloud
494494
* @throws \Exception
@@ -1770,7 +1770,7 @@ private function detectUuidAttribute(string $dn, bool $isUser = true, bool $forc
17701770
/**
17711771
* @param string $dn
17721772
* @param bool $isUser
1773-
* @param null $ldapRecord
1773+
* @param array|null $ldapRecord
17741774
* @return false|string
17751775
* @throws ServerNotAvailableException
17761776
*/

apps/user_ldap/lib/Command/CheckUser.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
66
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
7+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
78
* @author Joas Schilling <coding@schilljs.com>
89
* @author Morris Jobke <hey@morrisjobke.de>
910
* @author Roeland Jago Douma <roeland@famdouma.nl>
@@ -48,12 +49,6 @@ class CheckUser extends Command {
4849
/** @var UserMapping */
4950
protected $mapping;
5051

51-
/**
52-
* @param User_Proxy $uBackend
53-
* @param Helper $helper
54-
* @param DeletedUsersIndex $dui
55-
* @param UserMapping $mapping
56-
*/
5752
public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
5853
$this->backend = $uBackend;
5954
$this->helper = $helper;
@@ -62,14 +57,14 @@ public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIn
6257
parent::__construct();
6358
}
6459

65-
protected function configure() {
60+
protected function configure(): void {
6661
$this
6762
->setName('ldap:check-user')
6863
->setDescription('checks whether a user exists on LDAP.')
6964
->addArgument(
7065
'ocName',
7166
InputArgument::REQUIRED,
72-
'the user name as used in Nextcloud'
67+
'the user name as used in Nextcloud, or the LDAP DN'
7368
)
7469
->addOption(
7570
'force',
@@ -88,23 +83,31 @@ protected function configure() {
8883

8984
protected function execute(InputInterface $input, OutputInterface $output): int {
9085
try {
86+
$this->assertAllowed($input->getOption('force'));
9187
$uid = $input->getArgument('ocName');
92-
$this->isAllowed($input->getOption('force'));
93-
$this->confirmUserIsMapped($uid);
88+
if ($this->backend->getLDAPAccess($uid)->stringResemblesDN($uid)) {
89+
$username = $this->backend->dn2UserName($uid);
90+
if ($username !== false) {
91+
$uid = $username;
92+
}
93+
}
94+
$wasMapped = $this->userWasMapped($uid);
9495
$exists = $this->backend->userExistsOnLDAP($uid, true);
9596
if ($exists === true) {
9697
$output->writeln('The user is still available on LDAP.');
9798
if ($input->getOption('update')) {
9899
$this->updateUser($uid, $output);
99100
}
100101
return 0;
102+
} elseif ($wasMapped) {
103+
$this->dui->markUser($uid);
104+
$output->writeln('The user does not exists on LDAP anymore.');
105+
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
106+
. $uid . '"');
107+
return 0;
108+
} else {
109+
throw new \Exception('The given user is not a recognized LDAP user.');
101110
}
102-
103-
$this->dui->markUser($uid);
104-
$output->writeln('The user does not exists on LDAP anymore.');
105-
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
106-
. $uid . '"');
107-
return 0;
108111
} catch (\Exception $e) {
109112
$output->writeln('<error>' . $e->getMessage(). '</error>');
110113
return 1;
@@ -114,24 +117,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114117
/**
115118
* checks whether a user is actually mapped
116119
* @param string $ocName the username as used in Nextcloud
117-
* @throws \Exception
118-
* @return true
119120
*/
120-
protected function confirmUserIsMapped($ocName) {
121+
protected function userWasMapped(string $ocName): bool {
121122
$dn = $this->mapping->getDNByName($ocName);
122-
if ($dn === false) {
123-
throw new \Exception('The given user is not a recognized LDAP user.');
124-
}
125-
126-
return true;
123+
return $dn !== false;
127124
}
128125

129126
/**
130127
* checks whether the setup allows reliable checking of LDAP user existence
131128
* @throws \Exception
132-
* @return true
133129
*/
134-
protected function isAllowed($force) {
130+
protected function assertAllowed(bool $force): void {
135131
if ($this->helper->haveDisabledConfigurations() && !$force) {
136132
throw new \Exception('Cannot check user existence, because '
137133
. 'disabled LDAP configurations are present.');
@@ -140,8 +136,6 @@ protected function isAllowed($force) {
140136
// we don't check ldapUserCleanupInterval from config.php because this
141137
// action is triggered manually, while the setting only controls the
142138
// background job.
143-
144-
return true;
145139
}
146140

147141
private function updateUser(string $uid, OutputInterface $output): void {

0 commit comments

Comments
 (0)