opensupports/server/controllers/system/add-api-key.php

72 lines
1.8 KiB
PHP
Raw Normal View History

<?php
use Respect\Validation\Validator as DataValidator;
2017-04-18 02:09:16 +02:00
/**
* @api {post} /system/add-api-key Add APIKey
2020-02-05 21:15:32 +01:00
* @apiVersion 4.6.1
2017-04-18 02:09:16 +02:00
*
* @apiName Add APIKey
2017-04-18 02:09:16 +02:00
*
* @apiGroup System
2017-04-18 02:09:16 +02:00
*
* @apiDescription This path creates a new APIKey.
2017-04-18 02:09:16 +02:00
*
* @apiPermission staff3
2017-04-18 02:09:16 +02:00
*
* @apiParam {String} name Name of the new APIKey.
2020-01-07 18:44:18 +01:00
* @apiParam {String} type Type of APIKey: "REGISTRATION" or "TICKET_CREATE"
2017-04-18 02:09:16 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiUse NO_PERMISSION
* @apiUse INVALID_NAME
* @apiUse NAME_ALREADY_USED
2019-11-16 20:07:02 +01:00
* @apiUse INVALID_API_KEY_TYPE
2017-04-18 02:09:16 +02:00
*
* @apiSuccess {String} data Token of the APIKey.
2017-04-18 02:09:16 +02:00
*
*/
class AddAPIKeyController extends Controller {
const PATH = '/add-api-key';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'name' => [
'validation' => DataValidator::notBlank()->length(2, 55)->alnum(),
'error' => ERRORS::INVALID_NAME
2019-11-16 20:07:02 +01:00
],
'type' => [
'validation' => DataValidator::in(APIKey::TYPES),
'error' => ERRORS::INVALID_API_KEY_TYPE
]
]
];
}
public function handler() {
$apiInstance = new APIKey();
$name = Controller::request('name');
2019-11-16 20:07:02 +01:00
$type = Controller::request('type');
$keyInstance = APIKey::getDataStore($name, 'name');
if($keyInstance->isNull()){
$token = Hashing::generateRandomToken();
$apiInstance->setProperties([
'name' => $name,
2019-11-16 20:07:02 +01:00
'token' => $token,
'type' => $type,
]);
$apiInstance->store();
Response::respondSuccess($token);
} else {
2018-11-20 23:41:00 +01:00
throw new RequestException(ERRORS::NAME_ALREADY_USED);
}
}
}