diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-12 09:59:29 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-25 16:04:56 +0200 |
commit | 4ac283ecd364829d7d4c8a631182770c49dfbd6f (patch) | |
tree | 9cc10854101862a9d6d056cc0f7b001b12414d4d /apps/user_ldap/lib | |
parent | 9d61acb27d11c5a892670ed9e803d3723635fa55 (diff) | |
download | nextcloud-server-4ac283ecd364829d7d4c8a631182770c49dfbd6f.tar.gz nextcloud-server-4ac283ecd364829d7d4c8a631182770c49dfbd6f.zip |
Move Command namespace to PSR-4
Diffstat (limited to 'apps/user_ldap/lib')
-rw-r--r-- | apps/user_ldap/lib/Command/CheckUser.php | 134 | ||||
-rw-r--r-- | apps/user_ldap/lib/Command/CreateEmptyConfig.php | 73 | ||||
-rw-r--r-- | apps/user_ldap/lib/Command/DeleteConfig.php | 68 | ||||
-rw-r--r-- | apps/user_ldap/lib/Command/Search.php | 127 | ||||
-rw-r--r-- | apps/user_ldap/lib/Command/SetConfig.php | 84 | ||||
-rw-r--r-- | apps/user_ldap/lib/Command/ShowConfig.php | 108 | ||||
-rw-r--r-- | apps/user_ldap/lib/Command/ShowRemnants.php | 92 | ||||
-rw-r--r-- | apps/user_ldap/lib/Command/TestConfig.php | 90 |
8 files changed, 776 insertions, 0 deletions
diff --git a/apps/user_ldap/lib/Command/CheckUser.php b/apps/user_ldap/lib/Command/CheckUser.php new file mode 100644 index 00000000000..b984c9fa5c1 --- /dev/null +++ b/apps/user_ldap/lib/Command/CheckUser.php @@ -0,0 +1,134 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +use OCA\User_LDAP\lib\User\DeletedUsersIndex; +use OCA\User_LDAP\Mapping\UserMapping; +use OCA\user_ldap\lib\Helper as LDAPHelper; +use OCA\user_ldap\User_Proxy; + +class CheckUser extends Command { + /** @var \OCA\user_ldap\User_Proxy */ + protected $backend; + + /** @var \OCA\User_LDAP\lib\Helper */ + protected $helper; + + /** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */ + protected $dui; + + /** @var \OCA\User_LDAP\Mapping\UserMapping */ + protected $mapping; + + /** + * @param User_Proxy $uBackend + * @param LDAPHelper $helper + * @param DeletedUsersIndex $dui + * @param UserMapping $mapping + */ + public function __construct(User_Proxy $uBackend, LDAPHelper $helper, DeletedUsersIndex $dui, UserMapping $mapping) { + $this->backend = $uBackend; + $this->helper = $helper; + $this->dui = $dui; + $this->mapping = $mapping; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('ldap:check-user') + ->setDescription('checks whether a user exists on LDAP.') + ->addArgument( + 'ocName', + InputArgument::REQUIRED, + 'the user name as used in ownCloud' + ) + ->addOption( + 'force', + null, + InputOption::VALUE_NONE, + 'ignores disabled LDAP configuration' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + try { + $uid = $input->getArgument('ocName'); + $this->isAllowed($input->getOption('force')); + $this->confirmUserIsMapped($uid); + $exists = $this->backend->userExistsOnLDAP($uid); + if($exists === true) { + $output->writeln('The user is still available on LDAP.'); + return; + } + + $this->dui->markUser($uid); + $output->writeln('The user does not exists on LDAP anymore.'); + $output->writeln('Clean up the user\'s remnants by: ./occ user:delete "' + . $uid . '"'); + } catch (\Exception $e) { + $output->writeln('<error>' . $e->getMessage(). '</error>'); + } + } + + /** + * checks whether a user is actually mapped + * @param string $ocName the username as used in ownCloud + * @throws \Exception + * @return true + */ + protected function confirmUserIsMapped($ocName) { + $dn = $this->mapping->getDNByName($ocName); + if ($dn === false) { + throw new \Exception('The given user is not a recognized LDAP user.'); + } + + return true; + } + + /** + * checks whether the setup allows reliable checking of LDAP user existence + * @throws \Exception + * @return true + */ + protected function isAllowed($force) { + if($this->helper->haveDisabledConfigurations() && !$force) { + throw new \Exception('Cannot check user existence, because ' + . 'disabled LDAP configurations are present.'); + } + + // we don't check ldapUserCleanupInterval from config.php because this + // action is triggered manually, while the setting only controls the + // background job. + + return true; + } + +} diff --git a/apps/user_ldap/lib/Command/CreateEmptyConfig.php b/apps/user_ldap/lib/Command/CreateEmptyConfig.php new file mode 100644 index 00000000000..9ce665e049c --- /dev/null +++ b/apps/user_ldap/lib/Command/CreateEmptyConfig.php @@ -0,0 +1,73 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Martin Konrad <konrad@frib.msu.edu> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use \OCA\user_ldap\lib\Helper; +use \OCA\user_ldap\lib\Configuration; + +class CreateEmptyConfig extends Command { + /** @var \OCA\User_LDAP\lib\Helper */ + protected $helper; + + /** + * @param Helper $helper + */ + public function __construct(Helper $helper) { + $this->helper = $helper; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('ldap:create-empty-config') + ->setDescription('creates an empty LDAP configuration') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $configPrefix = $this->getNewConfigurationPrefix(); + $output->writeln("Created new configuration with configID '{$configPrefix}'"); + + $configHolder = new Configuration($configPrefix); + $configHolder->saveConfiguration(); + } + + protected function getNewConfigurationPrefix() { + $serverConnections = $this->helper->getServerConfigurationPrefixes(); + + // first connection uses no prefix + if(sizeof($serverConnections) == 0) { + return ''; + } + + sort($serverConnections); + $lastKey = array_pop($serverConnections); + $lastNumber = intval(str_replace('s', '', $lastKey)); + $nextPrefix = 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); + return $nextPrefix; + } +} diff --git a/apps/user_ldap/lib/Command/DeleteConfig.php b/apps/user_ldap/lib/Command/DeleteConfig.php new file mode 100644 index 00000000000..5a7986d1303 --- /dev/null +++ b/apps/user_ldap/lib/Command/DeleteConfig.php @@ -0,0 +1,68 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Martin Konrad <info@martin-konrad.net> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use \OCA\user_ldap\lib\Helper; + +class DeleteConfig extends Command { + /** @var \OCA\User_LDAP\lib\Helper */ + protected $helper; + + /** + * @param Helper $helper + */ + public function __construct(Helper $helper) { + $this->helper = $helper; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('ldap:delete-config') + ->setDescription('deletes an existing LDAP configuration') + ->addArgument( + 'configID', + InputArgument::REQUIRED, + 'the configuration ID' + ) + ; + } + + + protected function execute(InputInterface $input, OutputInterface $output) { + $configPrefix = $input->getArgument('configID'); + + $success = $this->helper->deleteServerConfiguration($configPrefix); + + if($success) { + $output->writeln("Deleted configuration with configID '{$configPrefix}'"); + } else { + $output->writeln("Cannot delete configuration with configID '{$configPrefix}'"); + } + } +} diff --git a/apps/user_ldap/lib/Command/Search.php b/apps/user_ldap/lib/Command/Search.php new file mode 100644 index 00000000000..f5352ae482a --- /dev/null +++ b/apps/user_ldap/lib/Command/Search.php @@ -0,0 +1,127 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +use OCA\user_ldap\User_Proxy; +use OCA\user_ldap\Group_Proxy; +use OCA\user_ldap\lib\Helper; +use OCA\user_ldap\lib\LDAP; +use OCP\IConfig; + +class Search extends Command { + /** @var \OCP\IConfig */ + protected $ocConfig; + + /** + * @param \OCP\IConfig $ocConfig + */ + public function __construct(IConfig $ocConfig) { + $this->ocConfig = $ocConfig; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('ldap:search') + ->setDescription('executes a user or group search') + ->addArgument( + 'search', + InputArgument::REQUIRED, + 'the search string (can be empty)' + ) + ->addOption( + 'group', + null, + InputOption::VALUE_NONE, + 'searches groups instead of users' + ) + ->addOption( + 'offset', + null, + InputOption::VALUE_REQUIRED, + 'The offset of the result set. Needs to be a multiple of limit. defaults to 0.', + 0 + ) + ->addOption( + 'limit', + null, + InputOption::VALUE_REQUIRED, + 'limit the results. 0 means no limit, defaults to 15', + 15 + ) + ; + } + + /** + * Tests whether the offset and limit options are valid + * @param int $offset + * @param int $limit + * @throws \InvalidArgumentException + */ + protected function validateOffsetAndLimit($offset, $limit) { + if($limit < 0) { + throw new \InvalidArgumentException('limit must be 0 or greater'); + } + if($offset < 0) { + throw new \InvalidArgumentException('offset must be 0 or greater'); + } + if($limit === 0 && $offset !== 0) { + throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0'); + } + if($offset > 0 && ($offset % $limit !== 0)) { + throw new \InvalidArgumentException('offset must be a multiple of limit'); + } + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $helper = new Helper(); + $configPrefixes = $helper->getServerConfigurationPrefixes(true); + $ldapWrapper = new LDAP(); + + $offset = intval($input->getOption('offset')); + $limit = intval($input->getOption('limit')); + $this->validateOffsetAndLimit($offset, $limit); + + if($input->getOption('group')) { + $proxy = new Group_Proxy($configPrefixes, $ldapWrapper); + $getMethod = 'getGroups'; + $printID = false; + } else { + $proxy = new User_Proxy($configPrefixes, $ldapWrapper, $this->ocConfig); + $getMethod = 'getDisplayNames'; + $printID = true; + } + + $result = $proxy->$getMethod($input->getArgument('search'), $limit, $offset); + foreach($result as $id => $name) { + $line = $name . ($printID ? ' ('.$id.')' : ''); + $output->writeln($line); + } + } +} diff --git a/apps/user_ldap/lib/Command/SetConfig.php b/apps/user_ldap/lib/Command/SetConfig.php new file mode 100644 index 00000000000..7e2e2f19f66 --- /dev/null +++ b/apps/user_ldap/lib/Command/SetConfig.php @@ -0,0 +1,84 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use \OCA\user_ldap\lib\Helper; +use \OCA\user_ldap\lib\Configuration; + +class SetConfig extends Command { + + protected function configure() { + $this + ->setName('ldap:set-config') + ->setDescription('modifies an LDAP configuration') + ->addArgument( + 'configID', + InputArgument::REQUIRED, + 'the configuration ID' + ) + ->addArgument( + 'configKey', + InputArgument::REQUIRED, + 'the configuration key' + ) + ->addArgument( + 'configValue', + InputArgument::REQUIRED, + 'the new configuration value' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $helper = new Helper(); + $availableConfigs = $helper->getServerConfigurationPrefixes(); + $configID = $input->getArgument('configID'); + if(!in_array($configID, $availableConfigs)) { + $output->writeln("Invalid configID"); + return; + } + + $this->setValue( + $configID, + $input->getArgument('configKey'), + $input->getArgument('configValue') + ); + } + + /** + * save the configuration value as provided + * @param string $configID + * @param string $configKey + * @param string $configValue + */ + protected function setValue($configID, $key, $value) { + $configHolder = new Configuration($configID); + $configHolder->$key = $value; + $configHolder->saveConfiguration(); + } +} diff --git a/apps/user_ldap/lib/Command/ShowConfig.php b/apps/user_ldap/lib/Command/ShowConfig.php new file mode 100644 index 00000000000..ea0ad3fed05 --- /dev/null +++ b/apps/user_ldap/lib/Command/ShowConfig.php @@ -0,0 +1,108 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Laurens Post <Crote@users.noreply.github.com> + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use \OCA\user_ldap\lib\Helper; +use \OCA\user_ldap\lib\Configuration; + +class ShowConfig extends Command { + /** @var \OCA\User_LDAP\lib\Helper */ + protected $helper; + + /** + * @param Helper $helper + */ + public function __construct(Helper $helper) { + $this->helper = $helper; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('ldap:show-config') + ->setDescription('shows the LDAP configuration') + ->addArgument( + 'configID', + InputArgument::OPTIONAL, + 'will show the configuration of the specified id' + ) + ->addOption( + 'show-password', + null, + InputOption::VALUE_NONE, + 'show ldap bind password' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $availableConfigs = $this->helper->getServerConfigurationPrefixes(); + $configID = $input->getArgument('configID'); + if(!is_null($configID)) { + $configIDs[] = $configID; + if(!in_array($configIDs[0], $availableConfigs)) { + $output->writeln("Invalid configID"); + return; + } + } else { + $configIDs = $availableConfigs; + } + + $this->renderConfigs($configIDs, $output, $input->getOption('show-password')); + } + + /** + * prints the LDAP configuration(s) + * @param string[] configID(s) + * @param OutputInterface $output + * @param bool $withPassword Set to TRUE to show plaintext passwords in output + */ + protected function renderConfigs($configIDs, $output, $withPassword) { + foreach($configIDs as $id) { + $configHolder = new Configuration($id); + $configuration = $configHolder->getConfiguration(); + ksort($configuration); + + $table = $this->getHelperSet()->get('table'); + $table->setHeaders(array('Configuration', $id)); + $rows = array(); + foreach($configuration as $key => $value) { + if($key === 'ldapAgentPassword' && !$withPassword) { + $value = '***'; + } + if(is_array($value)) { + $value = implode(';', $value); + } + $rows[] = array($key, $value); + } + $table->setRows($rows); + $table->render($output); + } + } +} diff --git a/apps/user_ldap/lib/Command/ShowRemnants.php b/apps/user_ldap/lib/Command/ShowRemnants.php new file mode 100644 index 00000000000..de435ec4327 --- /dev/null +++ b/apps/user_ldap/lib/Command/ShowRemnants.php @@ -0,0 +1,92 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author scolebrook <scolebrook@mac.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +use OCA\user_ldap\lib\user\DeletedUsersIndex; +use OCP\IDateTimeFormatter; + +class ShowRemnants extends Command { + /** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */ + protected $dui; + + /** @var \OCP\IDateTimeFormatter */ + protected $dateFormatter; + + /** + * @param DeletedUsersIndex $dui + * @param IDateTimeFormatter $dateFormatter + */ + public function __construct(DeletedUsersIndex $dui, IDateTimeFormatter $dateFormatter) { + $this->dui = $dui; + $this->dateFormatter = $dateFormatter; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('ldap:show-remnants') + ->setDescription('shows which users are not available on LDAP anymore, but have remnants in ownCloud.') + ->addOption('json', null, InputOption::VALUE_NONE, 'return JSON array instead of pretty table.'); + } + + /** + * executes the command, i.e. creeates and outputs a table of LDAP users marked as deleted + * + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) { + /** @var \Symfony\Component\Console\Helper\Table $table */ + $table = $this->getHelperSet()->get('table'); + $table->setHeaders(array( + 'ownCloud name', 'Display Name', 'LDAP UID', 'LDAP DN', 'Last Login', + 'Dir', 'Sharer')); + $rows = array(); + $resultSet = $this->dui->getUsers(); + foreach($resultSet as $user) { + $hAS = $user->getHasActiveShares() ? 'Y' : 'N'; + $lastLogin = ($user->getLastLogin() > 0) ? + $this->dateFormatter->formatDate($user->getLastLogin()) : '-'; + $rows[] = array('ocName' => $user->getOCName(), + 'displayName' => $user->getDisplayName(), + 'uid' => $user->getUID(), + 'dn' => $user->getDN(), + 'lastLogin' => $lastLogin, + 'homePath' => $user->getHomePath(), + 'sharer' => $hAS + ); + } + + if ($input->getOption('json')) { + $output->writeln(json_encode($rows)); + } else { + $table->setRows($rows); + $table->render($output); + } + } +} diff --git a/apps/user_ldap/lib/Command/TestConfig.php b/apps/user_ldap/lib/Command/TestConfig.php new file mode 100644 index 00000000000..790b9df6637 --- /dev/null +++ b/apps/user_ldap/lib/Command/TestConfig.php @@ -0,0 +1,90 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use \OCA\user_ldap\lib\Helper; +use \OCA\user_ldap\lib\Connection; + +class TestConfig extends Command { + + 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) { + $helper = new Helper(); + $availableConfigs = $helper->getServerConfigurationPrefixes(); + $configID = $input->getArgument('configID'); + if(!in_array($configID, $availableConfigs)) { + $output->writeln("Invalid configID"); + return; + } + + $result = $this->testConfig($configID); + if($result === 0) { + $output->writeln('The configuration is valid and the connection could be established!'); + } else if($result === 1) { + $output->writeln('The configuration is invalid. Please have a look at the logs for further details.'); + } else if($result === 2) { + $output->writeln('The configuration is valid, but the Bind failed. Please check the server settings and credentials.'); + } else { + $output->writeln('Your LDAP server was kidnapped by aliens.'); + } + } + + /** + * tests the specified connection + * @param string $configID + * @return int + */ + protected function testConfig($configID) { + $lw = new \OCA\user_ldap\lib\LDAP(); + $connection = new Connection($lw, $configID); + + //ensure validation is run before we attempt the bind + $connection->getConfiguration(); + + if(!$connection->setConfiguration(array( + 'ldap_configuration_active' => 1, + ))) { + return 1; + } + if($connection->bind()) { + return 0; + } + return 2; + } +} |