wizard.php 3.1 KB

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