1
0

wizard.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. // Check user and app status
  8. \OC_JSON::checkAdminUser();
  9. \OC_JSON::checkAppEnabled('user_ldap');
  10. \OC_JSON::callCheck();
  11. $l = \OCP\Util::getL10N('user_ldap');
  12. if (!isset($_POST['action'])) {
  13. \OC_JSON::error(['message' => $l->t('No action specified')]);
  14. }
  15. $action = (string)$_POST['action'];
  16. if (!isset($_POST['ldap_serverconfig_chooser'])) {
  17. \OC_JSON::error(['message' => $l->t('No configuration specified')]);
  18. }
  19. $prefix = (string)$_POST['ldap_serverconfig_chooser'];
  20. $ldapWrapper = new \OCA\User_LDAP\LDAP();
  21. $configuration = new \OCA\User_LDAP\Configuration($prefix);
  22. $con = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix, null);
  23. $con->setConfiguration($configuration->getConfiguration());
  24. $con->ldapConfigurationActive = (string)true;
  25. $con->setIgnoreValidation(true);
  26. $factory = \OC::$server->get(\OCA\User_LDAP\AccessFactory::class);
  27. $access = $factory->get($con);
  28. $wizard = new \OCA\User_LDAP\Wizard($configuration, $ldapWrapper, $access);
  29. switch ($action) {
  30. case 'guessPortAndTLS':
  31. case 'guessBaseDN':
  32. case 'detectEmailAttribute':
  33. case 'detectUserDisplayNameAttribute':
  34. case 'determineGroupMemberAssoc':
  35. case 'determineUserObjectClasses':
  36. case 'determineGroupObjectClasses':
  37. case 'determineGroupsForUsers':
  38. case 'determineGroupsForGroups':
  39. case 'determineAttributes':
  40. case 'getUserListFilter':
  41. case 'getUserLoginFilter':
  42. case 'getGroupFilter':
  43. case 'countUsers':
  44. case 'countGroups':
  45. case 'countInBaseDN':
  46. try {
  47. $result = $wizard->$action();
  48. if ($result !== false) {
  49. \OC_JSON::success($result->getResultArray());
  50. exit;
  51. }
  52. } catch (\Exception $e) {
  53. \OC_JSON::error(['message' => $e->getMessage(), 'code' => $e->getCode()]);
  54. exit;
  55. }
  56. \OC_JSON::error();
  57. exit;
  58. break;
  59. case 'testLoginName': {
  60. try {
  61. $loginName = $_POST['ldap_test_loginname'];
  62. $result = $wizard->$action($loginName);
  63. if ($result !== false) {
  64. \OC_JSON::success($result->getResultArray());
  65. exit;
  66. }
  67. } catch (\Exception $e) {
  68. \OC_JSON::error(['message' => $e->getMessage()]);
  69. exit;
  70. }
  71. \OC_JSON::error();
  72. exit;
  73. break;
  74. }
  75. case 'save':
  76. $key = $_POST['cfgkey'] ?? false;
  77. $val = $_POST['cfgval'] ?? null;
  78. if ($key === false || is_null($val)) {
  79. \OC_JSON::error(['message' => $l->t('No data specified')]);
  80. exit;
  81. }
  82. if (is_array($key)) {
  83. \OC_JSON::error(['message' => $l->t('Invalid data specified')]);
  84. exit;
  85. }
  86. $cfg = [$key => $val];
  87. $setParameters = [];
  88. $configuration->setConfiguration($cfg, $setParameters);
  89. if (!in_array($key, $setParameters)) {
  90. \OC_JSON::error(['message' => $l->t($key.
  91. ' Could not set configuration %s', $setParameters[0])]);
  92. exit;
  93. }
  94. $configuration->saveConfiguration();
  95. //clear the cache on save
  96. $connection = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix);
  97. $connection->clearCache();
  98. \OC_JSON::success();
  99. break;
  100. default:
  101. \OC_JSON::error(['message' => $l->t('Action does not exist')]);
  102. break;
  103. }