1
0

ConfigAPIController.php 8.6 KB

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