Guillermo - registration api keys [skip ci]

This commit is contained in:
AntonyAntonio 2017-01-13 15:50:35 -03:00
parent 01ac3e432d
commit f79913de97
6 changed files with 117 additions and 0 deletions

View File

@ -12,6 +12,9 @@ require_once 'system/recover-mail-template.php';
require_once 'system/get-stats.php';
require_once 'system/disable-registration.php';
require_once 'system/enable-registration.php';
require_once 'system/add-api-key.php';
require_once 'system/delete-api-key.php';
require_once 'system/get-all-keys.php';
$systemControllerGroup = new ControllerGroup();
$systemControllerGroup->setGroupPath('/system');
@ -29,5 +32,8 @@ $systemControllerGroup->addController(new RecoverMailTemplateController);
$systemControllerGroup->addController(new DisableRegistrationController);
$systemControllerGroup->addController(new EnableRegistrationController);
$systemControllerGroup->addController(new GetStatsController);
$systemControllerGroup->addController(new AddAPIKeyController);
$systemControllerGroup->addController(new DeleteAPIKeyController);
$systemControllerGroup->addController(new GetAllKeyController);
$systemControllerGroup->finalize();

View File

@ -0,0 +1,41 @@
<?php
use Respect\Validation\Validator as DataValidator;
class AddAPIKeyController extends Controller {
const PATH = '/add-api-key';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'name' => [
'validation' => DataValidator::length(2, 55)->alpha(),
'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,
'key' => $token
]);
$apiInstance->store();
Response::respondSuccess($token);
} else {
Response::respondError(ERRORS::NAME_ALREADY_USED);
}
}
}

View File

@ -0,0 +1,32 @@
<?php
use Respect\Validation\Validator as DataValidator;
class DeleteAPIKeyController extends Controller {
const PATH = '/delete-api-key';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'name' => [
'validation' => DataValidator::length(2, 55)->alpha(),
'error' => ERRORS::INVALID_NAME
]
]
];
}
public function handler() {
$name = Controller::request('name');
$keyInstance = APIKey::getDataStore($name, 'name');
if($keyInstance->isNull()) {
Response::respondError(ERRORS::INVALID_NAME);
return;
}
$keyInstance->delete();
Response::respondSuccess();
}
}

View File

@ -0,0 +1,19 @@
<?php
use Respect\Validation\Validator as DataValidator;
class GetAllKeyController extends Controller {
const PATH = '/get-all-keys';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => []
];
}
public function handler() {
$apiList = APIKey::getAll();
Response::respondSuccess($apiList->toArray());
}
}

View File

@ -36,4 +36,5 @@ class ERRORS {
const INVALID_SUBJECT = 'INVALID_SUBJECT';
const INVALID_BODY = 'INVALID_BODY';
const INVALID_PERIOD = 'INVALID_PERIOD';
const NAME_ALREADY_USED = 'NAME_ALREADY_USED';
}

18
server/models/APIKey.php Normal file
View File

@ -0,0 +1,18 @@
<?php
class APIKey extends DataStore {
const TABLE = 'apikey';
public static function getProps() {
return [
'name',
'key'
];
}
public function toArray() {
return [
'name' => $this->name,
'key' => $this->key
];
}
}