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
|
2019-10-08 01:21:43 +02:00
|
|
|
* @apiVersion 4.5.0
|
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.
|
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
|
|
|
*
|
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' => [
|
2017-01-13 21:06:49 +01:00
|
|
|
'validation' => DataValidator::length(2, 55)->alnum(),
|
2017-01-13 19:50:35 +01:00
|
|
|
'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,
|
2017-01-13 21:06:49 +01:00
|
|
|
'token' => $token
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|