2017-01-13 19:50:35 +01:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
|
2017-04-18 02:09:16 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +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
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiName Add APIKey
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiGroup System
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiDescription This path creates a new APIKey.
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiPermission staff3
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +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
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiSuccess {String} data Token of the APIKey.
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-01-13 19:50:35 +01:00
|
|
|
class AddAPIKeyController extends Controller {
|
|
|
|
const PATH = '/add-api-key';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2017-01-13 19:50:35 +01:00
|
|
|
|
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'staff_3',
|
|
|
|
'requestData' => [
|
|
|
|
'name' => [
|
2020-01-31 18:19:48 +01:00
|
|
|
'validation' => DataValidator::notBlank()->length(2, 55)->alnum(),
|
2017-01-13 19:50:35 +01:00
|
|
|
'error' => ERRORS::INVALID_NAME
|
2019-11-16 20:07:02 +01:00
|
|
|
],
|
|
|
|
'type' => [
|
|
|
|
'validation' => DataValidator::in(APIKey::TYPES),
|
|
|
|
'error' => ERRORS::INVALID_API_KEY_TYPE
|
2017-01-13 19:50:35 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
|
|
|
$apiInstance = new APIKey();
|
|
|
|
|
|
|
|
$name = Controller::request('name');
|
2019-11-16 20:07:02 +01:00
|
|
|
$type = Controller::request('type');
|
2017-01-13 19:50:35 +01:00
|
|
|
|
|
|
|
$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,
|
2017-01-13 19:50:35 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$apiInstance->store();
|
|
|
|
Response::respondSuccess($token);
|
|
|
|
} else {
|
2018-11-20 23:41:00 +01:00
|
|
|
throw new RequestException(ERRORS::NAME_ALREADY_USED);
|
2017-01-13 19:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|