1
0

ConfigAPIController.php 9.4 KB

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