wizard.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Roger Szabo <roger.szabo@web.de>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. // Check user and app status
  30. \OC_JSON::checkAdminUser();
  31. \OC_JSON::checkAppEnabled('user_ldap');
  32. \OC_JSON::callCheck();
  33. $l = \OC::$server->getL10N('user_ldap');
  34. if (!isset($_POST['action'])) {
  35. \OC_JSON::error(['message' => $l->t('No action specified')]);
  36. }
  37. $action = (string)$_POST['action'];
  38. if (!isset($_POST['ldap_serverconfig_chooser'])) {
  39. \OC_JSON::error(['message' => $l->t('No configuration specified')]);
  40. }
  41. $prefix = (string)$_POST['ldap_serverconfig_chooser'];
  42. $ldapWrapper = new \OCA\User_LDAP\LDAP();
  43. $configuration = new \OCA\User_LDAP\Configuration($prefix);
  44. $con = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix, null);
  45. $con->setConfiguration($configuration->getConfiguration());
  46. $con->ldapConfigurationActive = true;
  47. $con->setIgnoreValidation(true);
  48. $userManager = new \OCA\User_LDAP\User\Manager(
  49. \OC::$server->getConfig(),
  50. new \OCA\User_LDAP\FilesystemHelper(),
  51. \OC::$server->get(\Psr\Log\LoggerInterface::class),
  52. \OC::$server->getAvatarManager(),
  53. new \OCP\Image(),
  54. \OC::$server->getUserManager(),
  55. \OC::$server->getNotificationManager(),
  56. \OC::$server->get(\OCP\Share\IManager::class)
  57. );
  58. $access = new \OCA\User_LDAP\Access(
  59. $con,
  60. $ldapWrapper,
  61. $userManager,
  62. new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  63. \OC::$server->getConfig(),
  64. \OC::$server->getUserManager(),
  65. \OC::$server->get(\Psr\Log\LoggerInterface::class)
  66. );
  67. $wizard = new \OCA\User_LDAP\Wizard($configuration, $ldapWrapper, $access);
  68. switch ($action) {
  69. case 'guessPortAndTLS':
  70. case 'guessBaseDN':
  71. case 'detectEmailAttribute':
  72. case 'detectUserDisplayNameAttribute':
  73. case 'determineGroupMemberAssoc':
  74. case 'determineUserObjectClasses':
  75. case 'determineGroupObjectClasses':
  76. case 'determineGroupsForUsers':
  77. case 'determineGroupsForGroups':
  78. case 'determineAttributes':
  79. case 'getUserListFilter':
  80. case 'getUserLoginFilter':
  81. case 'getGroupFilter':
  82. case 'countUsers':
  83. case 'countGroups':
  84. case 'countInBaseDN':
  85. try {
  86. $result = $wizard->$action();
  87. if ($result !== false) {
  88. \OC_JSON::success($result->getResultArray());
  89. exit;
  90. }
  91. } catch (\Exception $e) {
  92. \OC_JSON::error(['message' => $e->getMessage(), 'code' => $e->getCode()]);
  93. exit;
  94. }
  95. \OC_JSON::error();
  96. exit;
  97. break;
  98. case 'testLoginName': {
  99. try {
  100. $loginName = $_POST['ldap_test_loginname'];
  101. $result = $wizard->$action($loginName);
  102. if ($result !== false) {
  103. \OC_JSON::success($result->getResultArray());
  104. exit;
  105. }
  106. } catch (\Exception $e) {
  107. \OC_JSON::error(['message' => $e->getMessage()]);
  108. exit;
  109. }
  110. \OC_JSON::error();
  111. exit;
  112. break;
  113. }
  114. case 'save':
  115. $key = isset($_POST['cfgkey']) ? $_POST['cfgkey'] : false;
  116. $val = isset($_POST['cfgval']) ? $_POST['cfgval'] : null;
  117. if ($key === false || is_null($val)) {
  118. \OC_JSON::error(['message' => $l->t('No data specified')]);
  119. exit;
  120. }
  121. $cfg = [$key => $val];
  122. $setParameters = [];
  123. $configuration->setConfiguration($cfg, $setParameters);
  124. if (!in_array($key, $setParameters)) {
  125. \OC_JSON::error(['message' => $l->t($key.
  126. ' Could not set configuration %s', $setParameters[0])]);
  127. exit;
  128. }
  129. $configuration->saveConfiguration();
  130. //clear the cache on save
  131. $connection = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix);
  132. $connection->clearCache();
  133. \OC_JSON::success();
  134. break;
  135. default:
  136. \OC_JSON::error(['message' => $l->t('Action does not exist')]);
  137. break;
  138. }