wizard.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Roger Szabo <roger.szabo@web.de>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. // Check user and app status
  32. \OC_JSON::checkAdminUser();
  33. \OC_JSON::checkAppEnabled('user_ldap');
  34. \OC_JSON::callCheck();
  35. $l = \OC::$server->getL10N('user_ldap');
  36. if(!isset($_POST['action'])) {
  37. \OC_JSON::error(['message' => $l->t('No action specified')]);
  38. }
  39. $action = (string)$_POST['action'];
  40. if(!isset($_POST['ldap_serverconfig_chooser'])) {
  41. \OC_JSON::error(['message' => $l->t('No configuration specified')]);
  42. }
  43. $prefix = (string)$_POST['ldap_serverconfig_chooser'];
  44. $ldapWrapper = new \OCA\User_LDAP\LDAP();
  45. $configuration = new \OCA\User_LDAP\Configuration($prefix);
  46. $con = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix, null);
  47. $con->setConfiguration($configuration->getConfiguration());
  48. $con->ldapConfigurationActive = true;
  49. $con->setIgnoreValidation(true);
  50. $userManager = new \OCA\User_LDAP\User\Manager(
  51. \OC::$server->getConfig(),
  52. new \OCA\User_LDAP\FilesystemHelper(),
  53. new \OCA\User_LDAP\LogWrapper(),
  54. \OC::$server->getAvatarManager(),
  55. new \OCP\Image(),
  56. \OC::$server->getDatabaseConnection(),
  57. \OC::$server->getUserManager(),
  58. \OC::$server->getNotificationManager());
  59. $access = new \OCA\User_LDAP\Access(
  60. $con,
  61. $ldapWrapper,
  62. $userManager,
  63. new \OCA\User_LDAP\Helper(\OC::$server->getConfig()),
  64. \OC::$server->getConfig(),
  65. \OC::$server->getUserManager()
  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. }