2017-01-13 19:50:35 +01:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
|
2017-04-18 02:09:16 +02:00
|
|
|
/**
|
|
|
|
* @api {post} /system/add-api-key Create a new api-key.
|
|
|
|
*
|
|
|
|
* @apiName Add api-key
|
|
|
|
*
|
|
|
|
* @apiGroup system
|
|
|
|
*
|
|
|
|
* @apiDescription This path create a new api-key.
|
|
|
|
*
|
|
|
|
* @apiPermission Staff level 3
|
|
|
|
*
|
|
|
|
* @apiParam {string} name Number of the new api-key.
|
|
|
|
*
|
|
|
|
* @apiError {String} message
|
|
|
|
*
|
|
|
|
* @apiSuccess {Object} data
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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 {
|
|
|
|
Response::respondError(ERRORS::NAME_ALREADY_USED);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|