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

64 lines
1.4 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
2019-10-08 01:21:43 +02:00
* @apiVersion 4.5.0
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.
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
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::length(2, 55)->alnum(),
'error' => ERRORS::INVALID_NAME
]
]
];
}
public function handler() {
$apiInstance = new APIKey();
$name = Controller::request('name');
$keyInstance = APIKey::getDataStore($name, 'name');
if($keyInstance->isNull()){
$token = Hashing::generateRandomToken();
$apiInstance->setProperties([
'name' => $name,
'token' => $token
]);
$apiInstance->store();
Response::respondSuccess($token);
} else {
2018-11-20 23:41:00 +01:00
throw new RequestException(ERRORS::NAME_ALREADY_USED);
}
}
}