You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nextcloud/apps/user_ldap/lib/Command/TestConfig.php

119 lines
3.7 KiB
PHTML

<?php
/**
8 years ago
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
8 years ago
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @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 <http://www.gnu.org/licenses/>
*
*/
namespace OCA\User_LDAP\Command;
use OCA\User_LDAP\AccessFactory;
use OCA\User_LDAP\Connection;
use OCA\User_LDAP\Helper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
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() {
$this
->setName('ldap:test-config')
->setDescription('tests an LDAP configuration')
->addArgument(
'configID',
InputArgument::REQUIRED,
'the configuration ID'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
LDAP User Cleanup: Port from stable7 without further adjustements LDAP User Cleanup background job for user clean up adjust user backend for clean up register background job remove dead code dependency injection make Helper non-static for proper testing check whether it is OK to run clean up job. Do not forget to pass arguments. use correct method to get the config from server methods can be private, proper indirect testing is given no automatic user deletion make limit readable for test purposes make method less complex add first tests let preferences accept limit and offset for getUsersForValue DI via constructor does not work for background jobs after detecting, now we have retrieving deleted users and their details we need this method to be public for now finalize export method, add missing getter clean up namespaces and get rid of unnecessary files helper is not static anymore cleanup according to scrutinizer add cli tool to show deleted users uses are necessary after recent namespace change also remove user from mappings table on deletion add occ command to delete users fix use statement improve output big fixes / improvements PHP doc return true in userExists early for cleaning up deleted users bump version control state and interval with one config.php setting, now ldapUserCleanupInterval. 0 will disable it. enabled by default. improve doc rename cli method to be consistent with others introduce ldapUserCleanupInterval in sample config don't show last login as unix epoche start when no login happend less log output consistent namespace for OfflineUser rename GarbageCollector to DeletedUsersIndex and move it to user subdir fix unit tests add tests for deleteUser more test adjustements Conflicts: apps/user_ldap/ajax/clearMappings.php apps/user_ldap/appinfo/app.php apps/user_ldap/lib/access.php apps/user_ldap/lib/helper.php apps/user_ldap/tests/helper.php core/register_command.php lib/private/preferences.php lib/private/user.php add ldap:check-user to check user existance on the fly Conflicts: apps/user_ldap/lib/helper.php forgotten file PHPdoc fixes, no code change and don't forget to adjust tests
10 years ago
$availableConfigs = $helper->getServerConfigurationPrefixes();
$configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) {
$output->writeln('Invalid configID');
return 1;
}
$result = $this->testConfig($configID);
switch ($result) {
case static::SUCCESS:
$output->writeln('The configuration is valid and the connection could be established!');
return 0;
case static::INVALID:
$output->writeln('The configuration is invalid. Please have a look at the logs for further details.');
break;
case static::BINDFAILURE:
$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 1;
}
/**
* Tests the specified connection
*/
protected function testConfig(string $configID): int {
$lw = new \OCA\User_LDAP\LDAP();
$connection = new Connection($lw, $configID);
// Ensure validation is run before we attempt the bind
$connection->getConfiguration();
if (!$connection->setConfiguration([
'ldap_configuration_active' => 1,
])) {
return static::INVALID;
}
if (!$connection->bind()) {
return static::BINDFAILURE;
}
$access = $this->accessFactory->get($connection);
$result = $access->countObjects(1);
if (!is_int($result) || ($result <= 0)) {
return static::SEARCHFAILURE;
}
return static::SUCCESS;
}
}