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

62 lines
1.5 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 Revoke 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
"id" => new Parameter("id", Parameter::TYPE_INT),
));
$this->loginRequired = true;
2020-06-14 19:39:52 +02:00
$this->csrfTokenRequired = true;
2020-02-10 12:16:34 +01:00
}
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($aValues = array()) {
if(!parent::execute($aValues)) {
return false;
}
$id = $this->getParam("id");
if(!$this->apiKeyExists())
return false;
2020-04-02 00:02:51 +02:00
$sql = $this->user->getSQL();
$this->success = $sql->update("ApiKey")
->set("active", false)
->where(new Compare("uid", $id))
->where(new Compare("user_id", $this->user->getId()))
->execute();
$this->lastError = $sql->getLastError();
2020-02-10 12:16:34 +01:00
return $this->success;
}
2020-04-03 15:56:04 +02:00
}