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

53 lines
1.3 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-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
use DateTime;
2020-04-02 00:02:51 +02:00
use \Driver\SQL\Condition\Compare;
2020-04-03 17:39:58 +02:00
use Exception;
2020-04-02 00:02:51 +02:00
2020-04-02 21:19:06 +02:00
class Fetch 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->loginRequired = true;
2020-06-14 19:39:52 +02:00
$this->csrfTokenRequired = true;
2020-02-10 12:16:34 +01:00
}
public function execute($values = array()) {
if(!parent::execute($values)) {
return false;
}
2020-04-02 00:02:51 +02:00
$sql = $this->user->getSQL();
$res = $sql->select("uid", "api_key", "valid_until")
->from("ApiKey")
->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", true))
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
2020-02-10 12:16:34 +01:00
if($this->success) {
2020-04-02 16:31:17 +02:00
$this->result["api_keys"] = array();
foreach($res as $row) {
2020-04-03 15:56:04 +02:00
try {
$validUntil = (new DateTime($row["valid_until"]))->getTimestamp();
2020-04-03 17:39:58 +02:00
} catch (Exception $e) {
2020-04-03 15:56:04 +02:00
$validUntil = $row["valid_until"];
}
2020-04-02 16:31:17 +02:00
$this->result["api_keys"][] = array(
2020-04-03 17:39:58 +02:00
"uid" => intval($row["uid"]),
2020-04-03 15:56:04 +02:00
"api_key" => $row["api_key"],
"valid_until" => $validUntil,
);
2020-04-02 16:31:17 +02:00
}
2020-02-10 12:16:34 +01:00
}
return $this->success;
}
2020-04-03 15:56:04 +02:00
}