correctly validate that a domain has not a certificate in Certificates.add(), refs #722

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2019-10-08 18:44:42 +02:00
parent b4999fcc83
commit 26cb53c8fb

View File

@@ -63,10 +63,19 @@ class Certificates extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resou
$ssl_cert_chainfile = $this->getParam('ssl_cert_chainfile', true, ''); $ssl_cert_chainfile = $this->getParam('ssl_cert_chainfile', true, '');
// validate whether the domain does not already have an entry // validate whether the domain does not already have an entry
$result = $this->apiCall('Certificates.get', array( $has_cert = true;
'id' => $domainid try {
)); $this->apiCall('Certificates.get', array(
if (empty($result)) { 'id' => $domainid
));
} catch (\Exception $e) {
if ($e->getCode() == 412) {
$has_cert = false;
} else {
throw $e;
}
}
if (!$has_cert) {
$this->addOrUpdateCertificate($domain['id'], $ssl_cert_file, $ssl_key_file, $ssl_ca_file, $ssl_cert_chainfile, true); $this->addOrUpdateCertificate($domain['id'], $ssl_cert_file, $ssl_key_file, $ssl_ca_file, $ssl_cert_chainfile, true);
$this->logger()->logAction($this->isAdmin() ? \Froxlor\FroxlorLogger::ADM_ACTION : \Froxlor\FroxlorLogger::USR_ACTION, LOG_INFO, "[API] added ssl-certificate for '" . $domain['domain'] . "'"); $this->logger()->logAction($this->isAdmin() ? \Froxlor\FroxlorLogger::ADM_ACTION : \Froxlor\FroxlorLogger::USR_ACTION, LOG_INFO, "[API] added ssl-certificate for '" . $domain['domain'] . "'");
$result = $this->apiCall('Certificates.get', array( $result = $this->apiCall('Certificates.get', array(
@@ -111,7 +120,7 @@ class Certificates extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resou
"domainid" => $domainid "domainid" => $domainid
)); ));
if (! $result) { if (! $result) {
throw new \Exception("Domain '" . $domain['domain'] . "' does not have a certificate.", 404); throw new \Exception("Domain '" . $domain['domain'] . "' does not have a certificate.", 412);
} }
return $this->response(200, "successfull", $result); return $this->response(200, "successfull", $result);
} }