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