From f79913de975f2418dca3be8330e6c8785ad60330 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Fri, 13 Jan 2017 15:50:35 -0300 Subject: [PATCH] Guillermo - registration api keys [skip ci] --- server/controllers/system.php | 6 +++ server/controllers/system/add-api-key.php | 41 ++++++++++++++++++++ server/controllers/system/delete-api-key.php | 32 +++++++++++++++ server/controllers/system/get-all-keys.php | 19 +++++++++ server/data/ERRORS.php | 1 + server/models/APIKey.php | 18 +++++++++ 6 files changed, 117 insertions(+) create mode 100644 server/controllers/system/add-api-key.php create mode 100644 server/controllers/system/delete-api-key.php create mode 100644 server/controllers/system/get-all-keys.php create mode 100644 server/models/APIKey.php diff --git a/server/controllers/system.php b/server/controllers/system.php index 8bc393cb..1d8fa889 100644 --- a/server/controllers/system.php +++ b/server/controllers/system.php @@ -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(); \ No newline at end of file diff --git a/server/controllers/system/add-api-key.php b/server/controllers/system/add-api-key.php new file mode 100644 index 00000000..8bbb4b78 --- /dev/null +++ b/server/controllers/system/add-api-key.php @@ -0,0 +1,41 @@ + '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); + } + + } +} \ No newline at end of file diff --git a/server/controllers/system/delete-api-key.php b/server/controllers/system/delete-api-key.php new file mode 100644 index 00000000..8ee03152 --- /dev/null +++ b/server/controllers/system/delete-api-key.php @@ -0,0 +1,32 @@ + '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(); + } +} \ No newline at end of file diff --git a/server/controllers/system/get-all-keys.php b/server/controllers/system/get-all-keys.php new file mode 100644 index 00000000..35206374 --- /dev/null +++ b/server/controllers/system/get-all-keys.php @@ -0,0 +1,19 @@ + 'staff_3', + 'requestData' => [] + ]; + } + + public function handler() { + $apiList = APIKey::getAll(); + + Response::respondSuccess($apiList->toArray()); + } +} \ No newline at end of file diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index 4b7507cf..06211649 100644 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -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'; } diff --git a/server/models/APIKey.php b/server/models/APIKey.php new file mode 100644 index 00000000..c0b751fa --- /dev/null +++ b/server/models/APIKey.php @@ -0,0 +1,18 @@ + $this->name, + 'key' => $this->key + ]; + } +} \ No newline at end of file