ConfigAPIController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Controller;
  7. use OC\CapabilitiesManager;
  8. use OC\Core\Controller\OCSController;
  9. use OC\Security\IdentityProof\Manager;
  10. use OCA\User_LDAP\Configuration;
  11. use OCA\User_LDAP\ConnectionFactory;
  12. use OCA\User_LDAP\Helper;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\AppFramework\OCS\OCSBadRequestException;
  16. use OCP\AppFramework\OCS\OCSException;
  17. use OCP\AppFramework\OCS\OCSNotFoundException;
  18. use OCP\IRequest;
  19. use OCP\IUserManager;
  20. use OCP\IUserSession;
  21. use Psr\Log\LoggerInterface;
  22. class ConfigAPIController extends OCSController {
  23. public function __construct(
  24. string $appName,
  25. IRequest $request,
  26. CapabilitiesManager $capabilitiesManager,
  27. IUserSession $userSession,
  28. IUserManager $userManager,
  29. Manager $keyManager,
  30. private Helper $ldapHelper,
  31. private LoggerInterface $logger,
  32. private ConnectionFactory $connectionFactory
  33. ) {
  34. parent::__construct(
  35. $appName,
  36. $request,
  37. $capabilitiesManager,
  38. $userSession,
  39. $userManager,
  40. $keyManager
  41. );
  42. }
  43. /**
  44. * Create a new (empty) configuration and return the resulting prefix
  45. *
  46. * @AuthorizedAdminSetting(settings=OCA\User_LDAP\Settings\Admin)
  47. * @return DataResponse<Http::STATUS_OK, array{configID: string}, array{}>
  48. * @throws OCSException
  49. *
  50. * 200: Config created successfully
  51. */
  52. public function create() {
  53. try {
  54. $configPrefix = $this->ldapHelper->getNextServerConfigurationPrefix();
  55. $configHolder = new Configuration($configPrefix);
  56. $configHolder->ldapConfigurationActive = false;
  57. $configHolder->saveConfiguration();
  58. } catch (\Exception $e) {
  59. $this->logger->error($e->getMessage(), ['exception' => $e]);
  60. throw new OCSException('An issue occurred when creating the new config.');
  61. }
  62. return new DataResponse(['configID' => $configPrefix]);
  63. }
  64. /**
  65. * Delete a LDAP configuration
  66. *
  67. * @AuthorizedAdminSetting(settings=OCA\User_LDAP\Settings\Admin)
  68. * @param string $configID ID of the config
  69. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  70. * @throws OCSException
  71. * @throws OCSNotFoundException Config not found
  72. *
  73. * 200: Config deleted successfully
  74. */
  75. public function delete($configID) {
  76. try {
  77. $this->ensureConfigIDExists($configID);
  78. if (!$this->ldapHelper->deleteServerConfiguration($configID)) {
  79. throw new OCSException('Could not delete configuration');
  80. }
  81. } catch (OCSException $e) {
  82. throw $e;
  83. } catch (\Exception $e) {
  84. $this->logger->error($e->getMessage(), ['exception' => $e]);
  85. throw new OCSException('An issue occurred when deleting the config.');
  86. }
  87. return new DataResponse();
  88. }
  89. /**
  90. * Modify a configuration
  91. *
  92. * @AuthorizedAdminSetting(settings=OCA\User_LDAP\Settings\Admin)
  93. * @param string $configID ID of the config
  94. * @param array<string, mixed> $configData New config
  95. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  96. * @throws OCSException
  97. * @throws OCSBadRequestException Modifying config is not possible
  98. * @throws OCSNotFoundException Config not found
  99. *
  100. * 200: Config returned
  101. */
  102. public function modify($configID, $configData) {
  103. try {
  104. $this->ensureConfigIDExists($configID);
  105. if (!is_array($configData)) {
  106. throw new OCSBadRequestException('configData is not properly set');
  107. }
  108. $configuration = new Configuration($configID);
  109. $configKeys = $configuration->getConfigTranslationArray();
  110. foreach ($configKeys as $i => $key) {
  111. if (isset($configData[$key])) {
  112. $configuration->$key = $configData[$key];
  113. }
  114. }
  115. $configuration->saveConfiguration();
  116. $this->connectionFactory->get($configID)->clearCache();
  117. } catch (OCSException $e) {
  118. throw $e;
  119. } catch (\Exception $e) {
  120. $this->logger->error($e->getMessage(), ['exception' => $e]);
  121. throw new OCSException('An issue occurred when modifying the config.');
  122. }
  123. return new DataResponse();
  124. }
  125. /**
  126. * Get a configuration
  127. *
  128. * Output can look like this:
  129. * <?xml version="1.0"?>
  130. * <ocs>
  131. * <meta>
  132. * <status>ok</status>
  133. * <statuscode>200</statuscode>
  134. * <message>OK</message>
  135. * </meta>
  136. * <data>
  137. * <ldapHost>ldaps://my.ldap.server</ldapHost>
  138. * <ldapPort>7770</ldapPort>
  139. * <ldapBackupHost></ldapBackupHost>
  140. * <ldapBackupPort></ldapBackupPort>
  141. * <ldapBase>ou=small,dc=my,dc=ldap,dc=server</ldapBase>
  142. * <ldapBaseUsers>ou=users,ou=small,dc=my,dc=ldap,dc=server</ldapBaseUsers>
  143. * <ldapBaseGroups>ou=small,dc=my,dc=ldap,dc=server</ldapBaseGroups>
  144. * <ldapAgentName>cn=root,dc=my,dc=ldap,dc=server</ldapAgentName>
  145. * <ldapAgentPassword>clearTextWithShowPassword=1</ldapAgentPassword>
  146. * <ldapTLS>1</ldapTLS>
  147. * <turnOffCertCheck>0</turnOffCertCheck>
  148. * <ldapIgnoreNamingRules/>
  149. * <ldapUserDisplayName>displayname</ldapUserDisplayName>
  150. * <ldapUserDisplayName2>uid</ldapUserDisplayName2>
  151. * <ldapUserFilterObjectclass>inetOrgPerson</ldapUserFilterObjectclass>
  152. * <ldapUserFilterGroups></ldapUserFilterGroups>
  153. * <ldapUserFilter>(&amp;(objectclass=nextcloudUser)(nextcloudEnabled=TRUE))</ldapUserFilter>
  154. * <ldapUserFilterMode>1</ldapUserFilterMode>
  155. * <ldapGroupFilter>(&amp;(|(objectclass=nextcloudGroup)))</ldapGroupFilter>
  156. * <ldapGroupFilterMode>0</ldapGroupFilterMode>
  157. * <ldapGroupFilterObjectclass>nextcloudGroup</ldapGroupFilterObjectclass>
  158. * <ldapGroupFilterGroups></ldapGroupFilterGroups>
  159. * <ldapGroupDisplayName>cn</ldapGroupDisplayName>
  160. * <ldapGroupMemberAssocAttr>memberUid</ldapGroupMemberAssocAttr>
  161. * <ldapLoginFilter>(&amp;(|(objectclass=inetOrgPerson))(uid=%uid))</ldapLoginFilter>
  162. * <ldapLoginFilterMode>0</ldapLoginFilterMode>
  163. * <ldapLoginFilterEmail>0</ldapLoginFilterEmail>
  164. * <ldapLoginFilterUsername>1</ldapLoginFilterUsername>
  165. * <ldapLoginFilterAttributes></ldapLoginFilterAttributes>
  166. * <ldapQuotaAttribute></ldapQuotaAttribute>
  167. * <ldapQuotaDefault></ldapQuotaDefault>
  168. * <ldapEmailAttribute>mail</ldapEmailAttribute>
  169. * <ldapCacheTTL>20</ldapCacheTTL>
  170. * <ldapUuidUserAttribute>auto</ldapUuidUserAttribute>
  171. * <ldapUuidGroupAttribute>auto</ldapUuidGroupAttribute>
  172. * <ldapOverrideMainServer></ldapOverrideMainServer>
  173. * <ldapConfigurationActive>1</ldapConfigurationActive>
  174. * <ldapAttributesForUserSearch>uid;sn;givenname</ldapAttributesForUserSearch>
  175. * <ldapAttributesForGroupSearch></ldapAttributesForGroupSearch>
  176. * <ldapExperiencedAdmin>0</ldapExperiencedAdmin>
  177. * <homeFolderNamingRule></homeFolderNamingRule>
  178. * <hasMemberOfFilterSupport></hasMemberOfFilterSupport>
  179. * <useMemberOfToDetectMembership>1</useMemberOfToDetectMembership>
  180. * <ldapExpertUsernameAttr>uid</ldapExpertUsernameAttr>
  181. * <ldapExpertUUIDUserAttr>uid</ldapExpertUUIDUserAttr>
  182. * <ldapExpertUUIDGroupAttr></ldapExpertUUIDGroupAttr>
  183. * <lastJpegPhotoLookup>0</lastJpegPhotoLookup>
  184. * <ldapNestedGroups>0</ldapNestedGroups>
  185. * <ldapPagingSize>500</ldapPagingSize>
  186. * <turnOnPasswordChange>1</turnOnPasswordChange>
  187. * <ldapDynamicGroupMemberURL></ldapDynamicGroupMemberURL>
  188. * </data>
  189. * </ocs>
  190. *
  191. * @AuthorizedAdminSetting(settings=OCA\User_LDAP\Settings\Admin)
  192. * @param string $configID ID of the config
  193. * @param bool $showPassword Whether to show the password
  194. * @return DataResponse<Http::STATUS_OK, array<string, mixed>, array{}>
  195. * @throws OCSException
  196. * @throws OCSNotFoundException Config not found
  197. *
  198. * 200: Config returned
  199. */
  200. public function show($configID, $showPassword = false) {
  201. try {
  202. $this->ensureConfigIDExists($configID);
  203. $config = new Configuration($configID);
  204. $data = $config->getConfiguration();
  205. if (!$showPassword) {
  206. $data['ldapAgentPassword'] = '***';
  207. }
  208. foreach ($data as $key => $value) {
  209. if (is_array($value)) {
  210. $value = implode(';', $value);
  211. $data[$key] = $value;
  212. }
  213. }
  214. } catch (OCSException $e) {
  215. throw $e;
  216. } catch (\Exception $e) {
  217. $this->logger->error($e->getMessage(), ['exception' => $e]);
  218. throw new OCSException('An issue occurred when modifying the config.');
  219. }
  220. return new DataResponse($data);
  221. }
  222. /**
  223. * If the given config ID is not available, an exception is thrown
  224. *
  225. * @AuthorizedAdminSetting(settings=OCA\User_LDAP\Settings\Admin)
  226. * @param string $configID
  227. * @throws OCSNotFoundException
  228. */
  229. private function ensureConfigIDExists($configID): void {
  230. $prefixes = $this->ldapHelper->getServerConfigurationPrefixes();
  231. if (!in_array($configID, $prefixes, true)) {
  232. throw new OCSNotFoundException('Config ID not found');
  233. }
  234. }
  235. }