1
0

ConfigAPIController.php 8.6 KB

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