wizard.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = \OCP\Util::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. $factory = \OC::$server->get(\OCA\User_LDAP\AccessFactory::class);
  49. $access = $factory->get($con);
  50. $wizard = new \OCA\User_LDAP\Wizard($configuration, $ldapWrapper, $access);
  51. switch ($action) {
  52. case 'guessPortAndTLS':
  53. case 'guessBaseDN':
  54. case 'detectEmailAttribute':
  55. case 'detectUserDisplayNameAttribute':
  56. case 'determineGroupMemberAssoc':
  57. case 'determineUserObjectClasses':
  58. case 'determineGroupObjectClasses':
  59. case 'determineGroupsForUsers':
  60. case 'determineGroupsForGroups':
  61. case 'determineAttributes':
  62. case 'getUserListFilter':
  63. case 'getUserLoginFilter':
  64. case 'getGroupFilter':
  65. case 'countUsers':
  66. case 'countGroups':
  67. case 'countInBaseDN':
  68. try {
  69. $result = $wizard->$action();
  70. if ($result !== false) {
  71. \OC_JSON::success($result->getResultArray());
  72. exit;
  73. }
  74. } catch (\Exception $e) {
  75. \OC_JSON::error(['message' => $e->getMessage(), 'code' => $e->getCode()]);
  76. exit;
  77. }
  78. \OC_JSON::error();
  79. exit;
  80. break;
  81. case 'testLoginName': {
  82. try {
  83. $loginName = $_POST['ldap_test_loginname'];
  84. $result = $wizard->$action($loginName);
  85. if ($result !== false) {
  86. \OC_JSON::success($result->getResultArray());
  87. exit;
  88. }
  89. } catch (\Exception $e) {
  90. \OC_JSON::error(['message' => $e->getMessage()]);
  91. exit;
  92. }
  93. \OC_JSON::error();
  94. exit;
  95. break;
  96. }
  97. case 'save':
  98. $key = $_POST['cfgkey'] ?? false;
  99. $val = $_POST['cfgval'] ?? null;
  100. if ($key === false || is_null($val)) {
  101. \OC_JSON::error(['message' => $l->t('No data specified')]);
  102. exit;
  103. }
  104. if (is_array($key)) {
  105. \OC_JSON::error(['message' => $l->t('Invalid data specified')]);
  106. exit;
  107. }
  108. $cfg = [$key => $val];
  109. $setParameters = [];
  110. $configuration->setConfiguration($cfg, $setParameters);
  111. if (!in_array($key, $setParameters)) {
  112. \OC_JSON::error(['message' => $l->t($key.
  113. ' Could not set configuration %s', $setParameters[0])]);
  114. exit;
  115. }
  116. $configuration->saveConfiguration();
  117. //clear the cache on save
  118. $connection = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix);
  119. $connection->clearCache();
  120. \OC_JSON::success();
  121. break;
  122. default:
  123. \OC_JSON::error(['message' => $l->t('Action does not exist')]);
  124. break;
  125. }