diff --git a/apps/user_ldap/appinfo/routes.php b/apps/user_ldap/appinfo/routes.php index e4e0db48d1..c80d6af76e 100644 --- a/apps/user_ldap/appinfo/routes.php +++ b/apps/user_ldap/appinfo/routes.php @@ -41,5 +41,6 @@ $application = new \OCP\AppFramework\App('user_ldap'); $application->registerRoutes($this, [ 'ocs' => [ ['name' => 'ConfigAPI#create', 'url' => '/api/v1/config', 'verb' => 'POST'], + ['name' => 'ConfigAPI#delete', 'url' => '/api/v1/config/{configID}', 'verb' => 'DELETE'], ] ]); diff --git a/apps/user_ldap/lib/Controller/ConfigAPIController.php b/apps/user_ldap/lib/Controller/ConfigAPIController.php index e136b56cda..5a18b138c3 100644 --- a/apps/user_ldap/lib/Controller/ConfigAPIController.php +++ b/apps/user_ldap/lib/Controller/ConfigAPIController.php @@ -30,7 +30,9 @@ use OC\Security\IdentityProof\Manager; use OCA\User_LDAP\Configuration; use OCA\User_LDAP\Helper; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\ILogger; use OCP\IRequest; use OCP\IUserManager; @@ -123,4 +125,53 @@ class ConfigAPIController extends OCSController { } return new DataResponse(['prefix' => $configPrefix]); } + + /** + * Deletes a LDAP configuration, if present. + * + * Example: + * curl -X DELETE -H "OCS-APIREQUEST: true" -u $admin:$password \ + * https://nextcloud.server/ocs/v1.php/apps/user_ldap/api/v1/config/s60 + * + * + * + * + * ok + * 100 + * OK + * + * + * + * + * + * + * @param $configID + * @return DataResponse + * @throws OCSBadRequestException + * @throws OCSException + */ + public function delete($configID) { + $initial = substr($configID, 0, 1); + $number = substr($configID, 1); + if($initial !== 's' || $number !== strval(intval($number))) { + throw new OCSBadRequestException('Not a valid config ID'); + } + + try { + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(); + if(!in_array($configID, $prefixes)) { + throw new OCSNotFoundException('Config ID not found'); + } + if(!$this->ldapHelper->deleteServerConfiguration($configID)) { + throw new OCSException('Could not delete configuration'); + } + } catch(OCSException $e) { + throw $e; + } catch(\Exception $e) { + $this->logger->logException($e); + throw new OCSException('An issue occurred when deleting the config.'); + } + + return new DataResponse(); + } } diff --git a/build/integration/features/ldap-ocs.feature b/build/integration/features/ldap-ocs.feature index d8586bb75b..6e1c77e24c 100644 --- a/build/integration/features/ldap-ocs.feature +++ b/build/integration/features/ldap-ocs.feature @@ -5,3 +5,17 @@ Feature: LDAP When sending "POST" to "/apps/user_ldap/api/v1/config" Then the OCS status code should be "100" And the HTTP status code should be "200" + + Scenario: Delete a non-existing configuration + Given As an "admin" + When sending "DELETE" to "/apps/user_ldap/api/v1/config/s666" + Then the OCS status code should be "404" + And the HTTP status code should be "200" + + Scenario: Delete an invalid configuration + Given As an "admin" + When sending "DELETE" to "/apps/user_ldap/api/v1/config/hack0r" + Then the OCS status code should be "400" + And the HTTP status code should be "200" + + # TODO: Scenario deleting an existing config ID (needs to be created before)