Merge pull request #29859 from nextcloud/fix/user_ldap-test-config

Improve ldap:test-config occ command
pull/30509/head
blizzz 2 years ago committed by GitHub
commit 63a9bc2aac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,7 @@
* *
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Christoph Wurst <christoph@winzerhof-wurst.at> * @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Joas Schilling <coding@schilljs.com> * @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl> * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu> * @author Thomas Müller <thomas.mueller@tmit.eu>
@ -25,6 +26,7 @@
*/ */
namespace OCA\User_LDAP\Command; namespace OCA\User_LDAP\Command;
use OCA\User_LDAP\AccessFactory;
use OCA\User_LDAP\Connection; use OCA\User_LDAP\Connection;
use OCA\User_LDAP\Helper; use OCA\User_LDAP\Helper;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -33,6 +35,19 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class TestConfig extends Command { class TestConfig extends Command {
protected const SUCCESS = 0;
protected const INVALID = 1;
protected const BINDFAILURE = 2;
protected const SEARCHFAILURE = 3;
/** @var AccessFactory */
protected $accessFactory;
public function __construct(AccessFactory $accessFactory) {
$this->accessFactory = $accessFactory;
parent::__construct();
}
protected function configure() { protected function configure() {
$this $this
->setName('ldap:test-config') ->setName('ldap:test-config')
@ -41,7 +56,7 @@ class TestConfig extends Command {
'configID', 'configID',
InputArgument::REQUIRED, InputArgument::REQUIRED,
'the configuration ID' 'the configuration ID'
) )
; ;
} }
@ -50,44 +65,54 @@ class TestConfig extends Command {
$availableConfigs = $helper->getServerConfigurationPrefixes(); $availableConfigs = $helper->getServerConfigurationPrefixes();
$configID = $input->getArgument('configID'); $configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) { if (!in_array($configID, $availableConfigs)) {
$output->writeln("Invalid configID"); $output->writeln('Invalid configID');
return 1; return 1;
} }
$result = $this->testConfig($configID); $result = $this->testConfig($configID);
if ($result === 0) { switch ($result) {
$output->writeln('The configuration is valid and the connection could be established!'); case static::SUCCESS:
} elseif ($result === 1) { $output->writeln('The configuration is valid and the connection could be established!');
$output->writeln('The configuration is invalid. Please have a look at the logs for further details.'); return 0;
return 1; case static::INVALID:
} elseif ($result === 2) { $output->writeln('The configuration is invalid. Please have a look at the logs for further details.');
$output->writeln('The configuration is valid, but the Bind failed. Please check the server settings and credentials.'); break;
} else { case static::BINDFAILURE:
$output->writeln('Your LDAP server was kidnapped by aliens.'); $output->writeln('The configuration is valid, but the bind failed. Please check the server settings and credentials.');
break;
case static::SEARCHFAILURE:
$output->writeln('The configuration is valid and the bind passed, but a simple search on the base fails. Please check the server base setting.');
break;
default:
$output->writeln('Your LDAP server was kidnapped by aliens.');
break;
} }
return 0; return 1;
} }
/** /**
* tests the specified connection * Tests the specified connection
* @param string $configID
* @return int
*/ */
protected function testConfig($configID) { protected function testConfig(string $configID): int {
$lw = new \OCA\User_LDAP\LDAP(); $lw = new \OCA\User_LDAP\LDAP();
$connection = new Connection($lw, $configID); $connection = new Connection($lw, $configID);
//ensure validation is run before we attempt the bind // Ensure validation is run before we attempt the bind
$connection->getConfiguration(); $connection->getConfiguration();
if (!$connection->setConfiguration([ if (!$connection->setConfiguration([
'ldap_configuration_active' => 1, 'ldap_configuration_active' => 1,
])) { ])) {
return 1; return static::INVALID;
}
if (!$connection->bind()) {
return static::BINDFAILURE;
} }
if ($connection->bind()) { $access = $this->accessFactory->get($connection);
return 0; $result = $access->countObjects(1);
if (!is_int($result) || ($result <= 0)) {
return static::SEARCHFAILURE;
} }
return 2; return static::SUCCESS;
} }
} }

Loading…
Cancel
Save