ConfigAPIController.php 9.3 KB

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