From f65f3dacebf4620486890bea15f3bd86f8c57d49 Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 25 Apr 2019 17:02:43 +0200 Subject: [PATCH] - Added private property $_requiredPermissions to APIv1_Controller - Removed method _isAllowed from APIv1_Controller - Added public method _remap to APIv1_Controller - PermissionLib loading moved from constructor to _remap in APIv1_Controller - Changed method basicAuthentication in AuthLib, now calls loginLDAP --- application/core/APIv1_Controller.php | 33 +++++++++++++++++++-------- application/core/REST_Controller.php | 2 +- application/libraries/AuthLib.php | 2 +- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/application/core/APIv1_Controller.php b/application/core/APIv1_Controller.php index 9dd7b4ae8..6432c87db 100644 --- a/application/core/APIv1_Controller.php +++ b/application/core/APIv1_Controller.php @@ -5,6 +5,8 @@ */ class APIv1_Controller extends REST_Controller { + private $_requiredPermissions; + /** * Standard constructor for all the RESTful resources */ @@ -12,24 +14,35 @@ class APIv1_Controller extends REST_Controller { parent::__construct(); - // Loads permission lib - $this->load->library('PermissionLib'); + $this->_requiredPermissions = $requiredPermissions; log_message('debug', 'Called API: '.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']); - - $this->_isAllowed($requiredPermissions); } /** - * Checks if the caller is allowed to access to this content with the given permissions - * If it is not allowed will set the HTTP header with code 401 - * Wrapper for permissionlib->isEntitled + * This method is automatically called by CodeIgniter after the execution of the constructor is completed + * - Cheks if the AuthLib was loaded, if not it means that the authentication failed + * - Loads the permsission lib and calls permissionlib->isEntitled + * - Checks if the caller is allowed to access to this content with the given permissions + * if it is not allowed will set the HTTP header with code 401 + * - Calls the parent (REST_Controller) _remap method to performs other checks */ - private function _isAllowed($requiredPermissions) + public function _remap($object_called, $arguments) { - if (!$this->permissionlib->isEntitled($requiredPermissions, $this->router->method)) + if (isset($this->authlib)) // if set then the authentication is ok { - $this->response(error('You are not allowed to access to this content'), REST_Controller::HTTP_UNAUTHORIZED); + // Loads permission lib + $this->load->library('PermissionLib'); + + // Cheks if the user has the permission to call a method + if (!$this->permissionlib->isEntitled($this->_requiredPermissions, $this->router->method)) + { + // If not... + $this->response(error('You are not allowed to access to this content'), REST_Controller::HTTP_UNAUTHORIZED); + } } + + // Finally calls the parent _remap to perform other checks + parent::_remap($object_called, $arguments); } } diff --git a/application/core/REST_Controller.php b/application/core/REST_Controller.php index 488892b60..062640c49 100644 --- a/application/core/REST_Controller.php +++ b/application/core/REST_Controller.php @@ -627,7 +627,7 @@ abstract class REST_Controller extends CI_Controller { { $this->_log_request(); } -//echo 'RestKey: '.$this->rest->key; + $this->response([ $this->config->item('rest_status_field_name') => FALSE, $this->config->item('rest_message_field_name') => sprintf($this->lang->line('text_rest_invalid_api_key'), $this->rest->key) diff --git a/application/libraries/AuthLib.php b/application/libraries/AuthLib.php index fe6261106..43b8bd2d6 100644 --- a/application/libraries/AuthLib.php +++ b/application/libraries/AuthLib.php @@ -237,7 +237,7 @@ class AuthLib */ public function basicAuthentication($username, $password) { - return isSuccess($this->_checkLDAPAuthentication($username, $password)); + return isSuccess($this->loginLDAP($username, $password)); } /**