* @throws OCSException * * 200: Config created successfully */ #[AuthorizedAdminSetting(settings: Admin::class)] public function create() { try { $configPrefix = $this->ldapHelper->getNextServerConfigurationPrefix(); $configHolder = new Configuration($configPrefix); $configHolder->ldapConfigurationActive = false; $configHolder->saveConfiguration(); } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); throw new OCSException('An issue occurred when creating the new config.'); } return new DataResponse(['configID' => $configPrefix]); } /** * Delete a LDAP configuration * * @param string $configID ID of the config * @return DataResponse, array{}> * @throws OCSException * @throws OCSNotFoundException Config not found * * 200: Config deleted successfully */ #[AuthorizedAdminSetting(settings: Admin::class)] public function delete($configID) { try { $this->ensureConfigIDExists($configID); if (!$this->ldapHelper->deleteServerConfiguration($configID)) { throw new OCSException('Could not delete configuration'); } } catch (OCSException $e) { throw $e; } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); throw new OCSException('An issue occurred when deleting the config.'); } return new DataResponse(); } /** * Modify a configuration * * @param string $configID ID of the config * @param array $configData New config * @return DataResponse, array{}> * @throws OCSException * @throws OCSBadRequestException Modifying config is not possible * @throws OCSNotFoundException Config not found * * 200: Config returned */ #[AuthorizedAdminSetting(settings: Admin::class)] public function modify($configID, $configData) { try { $this->ensureConfigIDExists($configID); if (!is_array($configData)) { throw new OCSBadRequestException('configData is not properly set'); } $configuration = new Configuration($configID); $configKeys = $configuration->getConfigTranslationArray(); foreach ($configKeys as $i => $key) { if (isset($configData[$key])) { $configuration->$key = $configData[$key]; } } $configuration->saveConfiguration(); $this->connectionFactory->get($configID)->clearCache(); } catch (OCSException $e) { throw $e; } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); throw new OCSException('An issue occurred when modifying the config.'); } return new DataResponse(); } /** * Get a configuration * * Output can look like this: * * * * ok * 200 * OK * * * ldaps://my.ldap.server * 7770 * * * ou=small,dc=my,dc=ldap,dc=server * ou=users,ou=small,dc=my,dc=ldap,dc=server * ou=small,dc=my,dc=ldap,dc=server * cn=root,dc=my,dc=ldap,dc=server * clearTextWithShowPassword=1 * 1 * 0 * * displayname * uid * inetOrgPerson * * (&(objectclass=nextcloudUser)(nextcloudEnabled=TRUE)) * 1 * (&(|(objectclass=nextcloudGroup))) * 0 * nextcloudGroup * * cn * memberUid * (&(|(objectclass=inetOrgPerson))(uid=%uid)) * 0 * 0 * 1 * * * * mail * 20 * auto * auto * * 1 * uid;sn;givenname * * 0 * * * 1 * uid * uid * * 0 * 0 * 500 * 1 * * * * * @param string $configID ID of the config * @param bool $showPassword Whether to show the password * @return DataResponse, array{}> * @throws OCSException * @throws OCSNotFoundException Config not found * * 200: Config returned */ #[AuthorizedAdminSetting(settings: Admin::class)] public function show($configID, $showPassword = false) { try { $this->ensureConfigIDExists($configID); $config = new Configuration($configID); $data = $config->getConfiguration(); if (!$showPassword) { $data['ldapAgentPassword'] = '***'; } foreach ($data as $key => $value) { if (is_array($value)) { $value = implode(';', $value); $data[$key] = $value; } } } catch (OCSException $e) { throw $e; } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); throw new OCSException('An issue occurred when modifying the config.'); } return new DataResponse($data); } /** * If the given config ID is not available, an exception is thrown * * @param string $configID * @throws OCSNotFoundException */ #[AuthorizedAdminSetting(settings: Admin::class)] private function ensureConfigIDExists($configID): void { $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(); if (!in_array($configID, $prefixes, true)) { throw new OCSNotFoundException('Config ID not found'); } } }