From 6af93506f1702bcf2359ef0d668e3702a2487c55 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 12 Mar 2017 15:58:17 -0400 Subject: [PATCH] Properly handle if the API is disabled --- api/ApplicationContext.php | 4 ++++ api/BusinessLogic/Settings/ApiChecker.php | 21 +++++++++++++++++++ .../Categories/CategoryController.php | 2 +- api/index.php | 16 ++++++++++---- 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 api/BusinessLogic/Settings/ApiChecker.php diff --git a/api/ApplicationContext.php b/api/ApplicationContext.php index 0fc14d58..4672efc0 100644 --- a/api/ApplicationContext.php +++ b/api/ApplicationContext.php @@ -9,6 +9,7 @@ use BusinessLogic\Emails\EmailTemplateRetriever; use BusinessLogic\Emails\MailgunEmailSender; use BusinessLogic\Security\BanRetriever; use BusinessLogic\Security\UserContextBuilder; +use BusinessLogic\Settings\ApiChecker; use BusinessLogic\Tickets\Autoassigner; use BusinessLogic\Tickets\TicketRetriever; use BusinessLogic\Tickets\TicketCreator; @@ -34,6 +35,9 @@ class ApplicationContext { // Settings $this->get[ModsForHeskSettingsGateway::class] = new ModsForHeskSettingsGateway(); + // API Checker + $this->get[ApiChecker::class] = new ApiChecker($this->get[ModsForHeskSettingsGateway::class]); + // Verified Email Checker $this->get[VerifiedEmailGateway::class] = new VerifiedEmailGateway(); $this->get[VerifiedEmailChecker::class] = new VerifiedEmailChecker($this->get[VerifiedEmailGateway::class]); diff --git a/api/BusinessLogic/Settings/ApiChecker.php b/api/BusinessLogic/Settings/ApiChecker.php new file mode 100644 index 00000000..863a6e06 --- /dev/null +++ b/api/BusinessLogic/Settings/ApiChecker.php @@ -0,0 +1,21 @@ +modsForHeskSettingsGateway = $modsForHeskSettingsGateway; + } + + function isApiEnabled($heskSettings) { + $modsForHeskSettings = $this->modsForHeskSettingsGateway->getAllSettings($heskSettings); + + return intval($modsForHeskSettings['public_api']) === 1; + } +} \ No newline at end of file diff --git a/api/Controllers/Categories/CategoryController.php b/api/Controllers/Categories/CategoryController.php index 6265d2fd..bf640a3e 100644 --- a/api/Controllers/Categories/CategoryController.php +++ b/api/Controllers/Categories/CategoryController.php @@ -1,6 +1,6 @@ get[\BusinessLogic\Settings\ApiChecker::class]; + + if (!$apiChecker->isApiEnabled($hesk_settings)) { + http_response_code(404); + die(); + } + + return; } function buildUserContext($xAuthToken) { @@ -83,8 +91,8 @@ Link::before('before'); Link::all(array( // Categories - '/v1/categories' => '\Controllers\Category\CategoryController::printAllCategories', - '/v1/categories/{i}' => '\Controllers\Category\CategoryController', + '/v1/categories' => '\Controllers\Categories\CategoryController::printAllCategories', + '/v1/categories/{i}' => '\Controllers\Categories\CategoryController', // Tickets '/v1/tickets/{i}' => '\Controllers\Tickets\TicketController', '/v1/tickets' => '\Controllers\Tickets\TicketController',