2020-02-10 12:16:34 +01:00
|
|
|
<?php
|
|
|
|
|
2020-04-02 21:19:06 +02:00
|
|
|
namespace Api\ApiKey;
|
2020-02-10 12:16:34 +01:00
|
|
|
|
2020-04-02 21:19:06 +02:00
|
|
|
use \Api\Request;
|
2020-04-03 15:56:04 +02:00
|
|
|
|
2020-04-02 21:19:06 +02:00
|
|
|
class Create extends Request {
|
2020-02-10 12:16:34 +01:00
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array());
|
2020-02-10 12:16:34 +01:00
|
|
|
$this->apiKeyAllowed = false;
|
2020-06-14 19:39:52 +02:00
|
|
|
$this->csrfTokenRequired = true;
|
2020-02-10 12:16:34 +01:00
|
|
|
$this->loginRequired = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($values = array()) {
|
|
|
|
|
|
|
|
if(!parent::execute($values)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$apiKey = generateRandomString(64);
|
2020-04-02 00:02:51 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
$validUntil = (new \DateTime())->modify("+30 DAY");
|
|
|
|
|
|
|
|
$this->success = $sql->insert("ApiKey", array("user_id", "api_key", "valid_until"))
|
|
|
|
->addRow($this->user->getId(), $apiKey, $validUntil)
|
2020-04-02 01:48:46 +02:00
|
|
|
->returning("uid")
|
2020-04-02 00:02:51 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
2020-04-02 16:31:17 +02:00
|
|
|
$this->result["api_key"] = array(
|
|
|
|
"api_key" => $apiKey,
|
|
|
|
"valid_until" => $validUntil->getTimestamp(),
|
|
|
|
"uid" => $sql->getLastInsertId(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->result["api_key"] = null;
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
2020-02-10 12:16:34 +01:00
|
|
|
return $this->success;
|
|
|
|
}
|
2020-04-03 15:56:04 +02:00
|
|
|
}
|