web-base/core/Api/ApiKey/Refresh.class.php

68 lines
1.6 KiB
PHP
Raw Normal View History

2020-02-10 12:16:34 +01:00
<?php
2020-04-02 21:19:06 +02:00
namespace Api\ApiKey;
2020-04-02 00:02:51 +02:00
2020-04-02 21:19:06 +02:00
use \Api\Request;
2020-02-10 12:16:34 +01:00
use \Api\Parameter\Parameter;
2020-04-02 00:02:51 +02:00
use \Driver\SQL\Condition\Compare;
2020-02-10 12:16:34 +01:00
2020-04-02 21:19:06 +02:00
class Refresh extends Request {
2020-02-10 12:16:34 +01:00
public function __construct($user, $externCall = false) {
parent::__construct($user, $externCall, array(
"id" => new Parameter("id", Parameter::TYPE_INT),
));
$this->loginRequired = true;
}
private function apiKeyExists() {
$id = $this->getParam("id");
2020-04-02 00:02:51 +02:00
$sql = $this->user->getSQL();
2020-04-02 16:31:17 +02:00
$res = $sql->select($sql->count())
2020-04-02 00:02:51 +02:00
->from("ApiKey")
->where(new Compare("uid", $id))
->where(new Compare("user_id", $this->user->getId()))
2020-04-02 15:08:14 +02:00
->where(new Compare("valid_until", $sql->currentTimestamp(), ">"))
2020-04-02 00:02:51 +02:00
->where(new Compare("active", 1))
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
2020-04-02 16:31:17 +02:00
if($this->success && $res[0]["count"] === 0) {
2020-02-10 12:16:34 +01:00
$this->success = false;
$this->lastError = "This API-Key does not exist.";
}
return $this->success;
}
public function execute($values = array()) {
if(!parent::execute($values)) {
return false;
}
$id = $this->getParam("id");
if(!$this->apiKeyExists())
return false;
2020-04-02 00:02:51 +02:00
$validUntil = (new \DateTime)->modify("+30 DAY");
$sql = $this->user->getSQL();
$this->success = $sql->update("ApiKey")
->set("valid_until", $validUntil)
->where(new Compare("uid", $id))
->where(new Compare("user_id", $this->user->getId()))
->execute();
$this->lastError = $sql->getLastError();
if ($this->success) {
$this->result["valid_until"] = $validUntil->getTimestamp();
}
2020-02-10 12:16:34 +01:00
return $this->success;
}
};
?>