TestConfig.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP\Command;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use \OCA\User_LDAP\Helper;
  32. use \OCA\User_LDAP\Connection;
  33. class TestConfig extends Command {
  34. protected function configure() {
  35. $this
  36. ->setName('ldap:test-config')
  37. ->setDescription('tests an LDAP configuration')
  38. ->addArgument(
  39. 'configID',
  40. InputArgument::REQUIRED,
  41. 'the configuration ID'
  42. )
  43. ;
  44. }
  45. protected function execute(InputInterface $input, OutputInterface $output) {
  46. $helper = new Helper(\OC::$server->getConfig());
  47. $availableConfigs = $helper->getServerConfigurationPrefixes();
  48. $configID = $input->getArgument('configID');
  49. if(!in_array($configID, $availableConfigs)) {
  50. $output->writeln("Invalid configID");
  51. return;
  52. }
  53. $result = $this->testConfig($configID);
  54. if($result === 0) {
  55. $output->writeln('The configuration is valid and the connection could be established!');
  56. } else if($result === 1) {
  57. $output->writeln('The configuration is invalid. Please have a look at the logs for further details.');
  58. } else if($result === 2) {
  59. $output->writeln('The configuration is valid, but the Bind failed. Please check the server settings and credentials.');
  60. } else {
  61. $output->writeln('Your LDAP server was kidnapped by aliens.');
  62. }
  63. }
  64. /**
  65. * tests the specified connection
  66. * @param string $configID
  67. * @return int
  68. */
  69. protected function testConfig($configID) {
  70. $lw = new \OCA\User_LDAP\LDAP();
  71. $connection = new Connection($lw, $configID);
  72. //ensure validation is run before we attempt the bind
  73. $connection->getConfiguration();
  74. if(!$connection->setConfiguration(array(
  75. 'ldap_configuration_active' => 1,
  76. ))) {
  77. return 1;
  78. }
  79. if($connection->bind()) {
  80. return 0;
  81. }
  82. return 2;
  83. }
  84. }