Skip to content

Commit 5251169

Browse files
committed
feat(user_ldap): Add option to check all seen users
This can be useful in some situations to sync all seen users with --update Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 8598f8b commit 5251169

1 file changed

Lines changed: 56 additions & 23 deletions

File tree

apps/user_ldap/lib/Command/CheckUser.php

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use OCA\User_LDAP\Mapping\UserMapping;
1212
use OCA\User_LDAP\User\DeletedUsersIndex;
1313
use OCA\User_LDAP\User_Proxy;
14+
use OCP\IUser;
15+
use OCP\IUserManager;
1416
use Symfony\Component\Console\Command\Command;
1517
use Symfony\Component\Console\Input\InputArgument;
1618
use Symfony\Component\Console\Input\InputInterface;
@@ -23,6 +25,7 @@ public function __construct(
2325
protected Helper $helper,
2426
protected DeletedUsersIndex $dui,
2527
protected UserMapping $mapping,
28+
protected IUserManager $userManager,
2629
) {
2730
parent::__construct();
2831
}
@@ -33,7 +36,7 @@ protected function configure(): void {
3336
->setDescription('checks whether a user exists on LDAP.')
3437
->addArgument(
3538
'ocName',
36-
InputArgument::REQUIRED,
39+
InputArgument::OPTIONAL,
3740
'the user name as used in Nextcloud, or the LDAP DN'
3841
)
3942
->addOption(
@@ -48,44 +51,74 @@ protected function configure(): void {
4851
InputOption::VALUE_NONE,
4952
'syncs values from LDAP'
5053
)
54+
->addOption(
55+
'all-seen-users',
56+
null,
57+
InputOption::VALUE_NONE,
58+
'sync all seen users instead of only one'
59+
)
5160
;
5261
}
5362

5463
protected function execute(InputInterface $input, OutputInterface $output): int {
5564
try {
5665
$this->assertAllowed($input->getOption('force'));
5766
$uid = $input->getArgument('ocName');
58-
if ($this->backend->getLDAPAccess($uid)->stringResemblesDN($uid)) {
59-
$username = $this->backend->dn2UserName($uid);
60-
if ($username !== false) {
61-
$uid = $username;
62-
}
63-
}
64-
$wasMapped = $this->userWasMapped($uid);
65-
$exists = $this->backend->userExistsOnLDAP($uid, true);
66-
if ($exists === true) {
67-
$output->writeln('The user is still available on LDAP.');
68-
if ($input->getOption('update')) {
69-
$this->updateUser($uid, $output);
70-
}
71-
return self::SUCCESS;
72-
}
7367

74-
if ($wasMapped) {
75-
$this->dui->markUser($uid);
76-
$output->writeln('The user does not exists on LDAP anymore.');
77-
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
78-
. $uid . '"');
68+
if ($uid !== null) {
69+
return $this->checkUser($input, $output, $uid);
70+
} elseif ($input->getOption('all-seen-users')) {
71+
$this->userManager->callForSeenUsers(
72+
function (IUser $user) use ($input, $output): true {
73+
try {
74+
$output->writeln('<info>Checking ' . $user->getUID() . '…</info>', OutputInterface::VERBOSITY_VERBOSE);
75+
$this->checkUser($input, $output, $user->getUID());
76+
} catch (\Exception $e) {
77+
$output->writeln('<error> ' . $user->getUID() . ': ' . $e->getMessage() . '</error>');
78+
}
79+
/* Always continue */
80+
return true;
81+
}
82+
);
83+
$output->writeln('<info>Finished checking all seen users.</info>', OutputInterface::VERBOSITY_VERBOSE);
7984
return self::SUCCESS;
85+
} else {
86+
throw new \InvalidArgumentException('Either a user name or --all-seen-users is required');
8087
}
81-
82-
throw new \Exception('The given user is not a recognized LDAP user.');
8388
} catch (\Exception $e) {
8489
$output->writeln('<error>' . $e->getMessage() . '</error>');
8590
return self::FAILURE;
8691
}
8792
}
8893

94+
private function checkUser(InputInterface $input, OutputInterface $output, string $uid): int {
95+
if ($this->backend->getLDAPAccess($uid)->stringResemblesDN($uid)) {
96+
$username = $this->backend->dn2UserName($uid);
97+
if ($username !== false) {
98+
$uid = $username;
99+
}
100+
}
101+
$wasMapped = $this->userWasMapped($uid);
102+
$exists = $this->backend->userExistsOnLDAP($uid, true);
103+
if ($exists === true) {
104+
$output->writeln('The user is still available on LDAP.');
105+
if ($input->getOption('update')) {
106+
$this->updateUser($uid, $output);
107+
}
108+
return self::SUCCESS;
109+
}
110+
111+
if ($wasMapped) {
112+
$this->dui->markUser($uid);
113+
$output->writeln('The user does not exists on LDAP anymore.');
114+
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
115+
. $uid . '"');
116+
return self::SUCCESS;
117+
}
118+
119+
throw new \Exception('The given user is not a recognized LDAP user.');
120+
}
121+
89122
/**
90123
* checks whether a user is actually mapped
91124
* @param string $ocName the username as used in Nextcloud

0 commit comments

Comments
 (0)