add LDAP OCS Api for modifying a configuration

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2017-01-19 11:09:04 +01:00
parent 18a75bec0d
commit 01d469dfea
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
4 changed files with 101 additions and 11 deletions

View File

@ -41,6 +41,7 @@ $application = new \OCP\AppFramework\App('user_ldap');
$application->registerRoutes($this, [
'ocs' => [
['name' => 'ConfigAPI#create', 'url' => '/api/v1/config', 'verb' => 'POST'],
['name' => 'ConfigAPI#modify', 'url' => '/api/v1/config/{configID}', 'verb' => 'PUT'],
['name' => 'ConfigAPI#delete', 'url' => '/api/v1/config/{configID}', 'verb' => 'DELETE'],
]
]);

View File

@ -145,7 +145,7 @@ class ConfigAPIController extends OCSController {
* <data/>
* </ocs>
*
* @param $configID
* @param string $configID
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSException
@ -158,10 +158,7 @@ class ConfigAPIController extends OCSController {
}
try {
$prefixes = $this->ldapHelper->getServerConfigurationPrefixes();
if(!in_array($configID, $prefixes)) {
throw new OCSNotFoundException('Config ID not found');
}
$this->ensureConfigIDExists($configID);
if(!$this->ldapHelper->deleteServerConfiguration($configID)) {
throw new OCSException('Could not delete configuration');
}
@ -174,4 +171,66 @@ class ConfigAPIController extends OCSController {
return new DataResponse();
}
/**
* modifies a configuration
*
* Example:
* curl -X PUT -d "key=ldapHost&value=ldaps://my.ldap.server" \
* -H "OCS-APIREQUEST: true" -u $admin:$password \
* https://nextcloud.server/ocs/v1.php/apps/user_ldap/api/v1/config/s60
*
* <?xml version="1.0"?>
* <ocs>
* <meta>
* <status>ok</status>
* <statuscode>100</statuscode>
* <message>OK</message>
* <totalitems></totalitems>
* <itemsperpage></itemsperpage>
* </meta>
* <data/>
* </ocs>
*
* @param string $configID
* @param string $key
* @param string $value
* @return DataResponse
* @throws OCSException
*/
public function modify($configID, $key, $value) {
$this->ensureConfigIDExists($configID);
try {
$config = new Configuration($configID);
$configKeys = $config->getConfigTranslationArray();
if(!isset($configKeys[$key]) && !in_array($key, $configKeys, true)) {
throw new OCSBadRequestException('Invalid config key');
}
$config->$key = $value;
$config->saveConfiguration();
} catch(OCSException $e) {
throw $e;
} catch (\Exception $e) {
$this->logger->logException($e);
throw new OCSException('An issue occurred when modifying the config.');
}
return new DataResponse();
}
/**
* if the given config ID is not available, an exception is thrown
*
* @param string $configID
* @throws OCSNotFoundException
*/
private function ensureConfigIDExists($configID) {
$prefixes = $this->ldapHelper->getServerConfigurationPrefixes();
if(!in_array($configID, $prefixes)) {
throw new OCSNotFoundException('Config ID not found');
}
}
}

View File

@ -40,9 +40,9 @@ class LDAPContext implements Context {
}
/**
* @Given /^creating a configuration at "([^"]*)"$/
* @Given /^creating an LDAP configuration at "([^"]*)"$/
*/
public function creatingAConfigurationAt($apiUrl) {
public function creatingAnLDAPConfigurationAt($apiUrl) {
$this->apiUrl = $apiUrl;
$this->sendingToWith('POST', $this->apiUrl, null);
$configElements = $this->response->xml()->data[0]->configID;
@ -50,9 +50,20 @@ class LDAPContext implements Context {
}
/**
* @When /^deleting the configuration$/
* @When /^deleting the LDAP configuration$/
*/
public function deletingTheConfiguration() {
public function deletingTheLDAPConfiguration() {
$this->sendingToWith('DELETE', $this->apiUrl . '/' . $this->configID, null);
}
/**
* @When /^setting "([^"]*)" of the LDAP configuration to "([^"]*)"$/
*/
public function settingOfTheLDAPConfigurationTo($key, $value) {
$this->sendingToWith(
'PUT',
$this->apiUrl . '/' . $this->configID,
new \Behat\Gherkin\Node\TableNode([['key', $key], ['value', $value]])
);
}
}

View File

@ -21,7 +21,26 @@ Feature: LDAP
Scenario: Create and delete a configuration
Given As an "admin"
And creating a configuration at "/apps/user_ldap/api/v1/config"
When deleting the configuration
And creating an LDAP configuration at "/apps/user_ldap/api/v1/config"
When deleting the LDAP configuration
Then the OCS status code should be "100"
And the HTTP status code should be "200"
Scenario: Create and modify a configuration
Given As an "admin"
And creating an LDAP configuration at "/apps/user_ldap/api/v1/config"
When setting "ldapHost" of the LDAP configuration to "ldaps://my.ldap.server"
Then the OCS status code should be "100"
And the HTTP status code should be "200"
# Testing an invalid config key
When setting "crack0r" of the LDAP configuration to "foobar"
Then the OCS status code should be "400"
And the HTTP status code should be "200"
Scenario: Modiying a non-existing configuration
Given As an "admin"
When sending "PUT" to "/apps/user_ldap/api/v1/config/s666" with
| key | ldapHost |
| value | ldaps://my.ldap.server |
Then the OCS status code should be "404"
And the HTTP status code should be "200"