From f8c70df0bcbeac413b3ea7ee46d562b21d16962e Mon Sep 17 00:00:00 2001 From: bison-paolo Date: Fri, 14 Oct 2016 13:33:22 +0200 Subject: [PATCH] - Renamed PCRMLib to CallerLib :( - Splitted the controller PCRM in two different controllers: CallerLibrary and CallerModel --- .../api/v1/system/CallerLibrary.php | 77 +++++++++++++++++++ .../v1/system/{PCRM.php => CallerModel.php} | 16 ++-- .../libraries/{PCRMLib.php => CallerLib.php} | 58 ++++++++++---- 3 files changed, 129 insertions(+), 22 deletions(-) create mode 100644 application/controllers/api/v1/system/CallerLibrary.php rename application/controllers/api/v1/system/{PCRM.php => CallerModel.php} (66%) rename application/libraries/{PCRMLib.php => CallerLib.php} (82%) diff --git a/application/controllers/api/v1/system/CallerLibrary.php b/application/controllers/api/v1/system/CallerLibrary.php new file mode 100644 index 000000000..42f2e785e --- /dev/null +++ b/application/controllers/api/v1/system/CallerLibrary.php @@ -0,0 +1,77 @@ +load->library('CallerLib'); + } + + /** + * Manages a HTTP get call + */ + public function getCall() + { + // Start me up! + $result = $this->callerlib->callLibrary($this->get(), PermissionLib::SELECT_RIGHT); + + // Print the result + $this->response($result, REST_Controller::HTTP_OK); + } + + /** + * @return void + */ + public function postCall() + { + // Start me up! + $result = $this->callerlib->callLibrary($this->post(), PermissionLib::UPDATE_RIGHT); + + // Print the result + $this->response($result, REST_Controller::HTTP_OK); + } + + /** + * @return void + */ + public function putCall() + { + // Start me up! + $result = $this->callerlib->callLibrary($this->put(), PermissionLib::INSERT_RIGHT); + + // Print the result + $this->response($result, REST_Controller::HTTP_OK); + } + + /** + * @return void + */ + public function deleteCall() + { + // Start me up! + $result = $this->callerlib->callLibrary($this->delete(), PermissionLib::DELETE_RIGHT); + + // Print the result + $this->response($result, REST_Controller::HTTP_OK); + } +} \ No newline at end of file diff --git a/application/controllers/api/v1/system/PCRM.php b/application/controllers/api/v1/system/CallerModel.php similarity index 66% rename from application/controllers/api/v1/system/PCRM.php rename to application/controllers/api/v1/system/CallerModel.php index fda65796d..9c3ee8815 100644 --- a/application/controllers/api/v1/system/PCRM.php +++ b/application/controllers/api/v1/system/CallerModel.php @@ -12,9 +12,9 @@ */ // ------------------------------------------------------------------------ -if (!defined("BASEPATH")) exit("No direct script access allowed"); +if (!defined('BASEPATH')) exit('No direct script access allowed'); -class PCRM extends APIv1_Controller +class CallerModel extends APIv1_Controller { /** * API constructor @@ -23,8 +23,8 @@ class PCRM extends APIv1_Controller { parent::__construct(); - // Loads the PCRMLib - $this->load->library("PCRMLib"); + // Loads the CallerLib + $this->load->library('CallerLib'); } /** @@ -33,7 +33,7 @@ class PCRM extends APIv1_Controller public function getCall() { // Start me up! - $result = $this->pcrmlib->start($this->get(), PermissionLib::SELECT_RIGHT); + $result = $this->callerlib->callModel($this->get(), PermissionLib::SELECT_RIGHT); // Print the result $this->response($result, REST_Controller::HTTP_OK); @@ -45,7 +45,7 @@ class PCRM extends APIv1_Controller public function postCall() { // Start me up! - $result = $this->pcrmlib->start($this->post(), PermissionLib::UPDATE_RIGHT); + $result = $this->callerlib->callModel($this->post(), PermissionLib::UPDATE_RIGHT); // Print the result $this->response($result, REST_Controller::HTTP_OK); @@ -57,7 +57,7 @@ class PCRM extends APIv1_Controller public function putCall() { // Start me up! - $result = $this->pcrmlib->start($this->put(), PermissionLib::INSERT_RIGHT); + $result = $this->callerlib->callModel($this->put(), PermissionLib::INSERT_RIGHT); // Print the result $this->response($result, REST_Controller::HTTP_OK); @@ -69,7 +69,7 @@ class PCRM extends APIv1_Controller public function deleteCall() { // Start me up! - $result = $this->pcrmlib->start($this->delete(), PermissionLib::DELETE_RIGHT); + $result = $this->callerlib->callModel($this->delete(), PermissionLib::DELETE_RIGHT); // Print the result $this->response($result, REST_Controller::HTTP_OK); diff --git a/application/libraries/PCRMLib.php b/application/libraries/CallerLib.php similarity index 82% rename from application/libraries/PCRMLib.php rename to application/libraries/CallerLib.php index aa5293765..b544d1706 100644 --- a/application/libraries/PCRMLib.php +++ b/application/libraries/CallerLib.php @@ -3,9 +3,9 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); /** - * + * Library used to call a method of a model or a library */ -class PCRMLib +class CallerLib { const RESOURCE_PARAMETER = 'resource'; const FUNCTION_PARAMETER = 'function'; @@ -17,10 +17,11 @@ class PCRMLib // Black list of resources that are no allowed to be used private static $RESOURCES_BLACK_LIST = array( - 'PCRMLib', // disabled self loading + 'CallerLib', // disabled self loading 'LogLib', // hardly usefull and virtually dangerous 'MigrationLib', // virtually dangerous, DB manipulation - 'FilesystemLib' // virtually dangerous, direct access to file system + 'FilesystemLib', // virtually dangerous, direct access to file system + 'PermissionLib' // usefull? ); /** @@ -34,13 +35,30 @@ class PCRMLib // Loads helper message to manage returning messages $this->ci->load->helper('message'); + // Loads permission library $this->ci->load->library('PermissionLib'); } + /** + * Wrapper method for _call + */ + public function callLibrary($callParameters, $permissionType) + { + return $this->_call($callParameters, $permissionType); + } + + /** + * Wrapper method for _call + */ + public function callModel($callParameters, $permissionType) + { + return $this->_call($callParameters, $permissionType); + } + /** * Everything starts here... */ - public function start($callParameters, $permissionType) + private function _call($callParameters, $permissionType) { $result = null; $parameters = $this->_getParameters($callParameters); @@ -51,7 +69,7 @@ class PCRMLib { $loaded = null; // If the given resource is a model - if (strpos($parameters->resourceName, PCRMLib::MODEL_PREFIX) !== false) + if (strpos($parameters->resourceName, CallerLib::MODEL_PREFIX) !== false) { // Try to load the model $result = $this->_loadModel($parameters->resourcePath, $parameters->resourceName); @@ -61,7 +79,7 @@ class PCRMLib } } // If the given resource is a library - else if (strpos($parameters->resourceName, PCRMLib::LIB_PREFIX) !== false) + else if (strpos($parameters->resourceName, CallerLib::LIB_PREFIX) !== false) { // Check if the resource is already loaded, it works only with libraries and drivers $isLoaded = $this->ci->load->is_loaded($parameters->resourceName); @@ -121,7 +139,15 @@ class PCRMLib } /** - * Gets the parameters from the call + * Gets the parameters from the http call + * Search for parameters and + * is the name of the model or of the library + * is the name of the method present in the model/library + * All the others parameters will be given to the method in the same order that + * they are present in the HTTP call + * EX: + * URL: ../system/CallerLibrary/Call?resource=&function=&=&=&= + * will call .(par1, par2, par3) */ private function _getParameters($parametersArray) { @@ -132,15 +158,15 @@ class PCRMLib foreach ($parametersArray as $parameterName => $parameterValue) { // The name of the resource, path included - if ($parameterName == PCRMLib::RESOURCE_PARAMETER) + if ($parameterName == CallerLib::RESOURCE_PARAMETER) { // Separates the resource path from the resource name - $splittedResource = preg_split(PCRMLib::REG_SPLIT_EXPR, $parameterValue); + $splittedResource = preg_split(CallerLib::REG_SPLIT_EXPR, $parameterValue); $parameters->resourceName = $splittedResource[count($splittedResource) - 1]; $parameters->resourcePath = str_replace($parameters->resourceName, '', $parameterValue); } // The name of the function - else if ($parameterName == PCRMLib::FUNCTION_PARAMETER) + else if ($parameterName == CallerLib::FUNCTION_PARAMETER) { $parameters->function = $parameterValue; } @@ -181,7 +207,7 @@ class PCRMLib { return error('Parameters are not specified'); } - if (in_array($parameters->resourceName, PCRMLib::$RESOURCES_BLACK_LIST)) + if (in_array($parameters->resourceName, CallerLib::$RESOURCES_BLACK_LIST)) { return error('You are trying to access to unauthorized resources'); } @@ -217,6 +243,10 @@ class PCRMLib return $result; } + /** + * Search for a valid permission for this library that should be present with this format: + * '..' => '' + */ private function checkLibraryPermission($resourcePath, $resourceName, $function, $permissionType) { $result = null; @@ -265,8 +295,8 @@ class PCRMLib $found = null; for ($i = 0; $i < count($packagePaths) && is_null($found); $i++) { - $file = $packagePaths[$i] . PCRMLib::LIBS_PATH . DIRECTORY_SEPARATOR . - $resourcePath . $resourceName . PCRMLib::LIB_FILE_EXTENSION; + $file = $packagePaths[$i] . CallerLib::LIBS_PATH . DIRECTORY_SEPARATOR . + $resourcePath . $resourceName . CallerLib::LIB_FILE_EXTENSION; if (file_exists($file)) { $found = $file;