- Moved all the logic from controller PCRM to PCRMLib

- Extended functionality to others HTTP methods
- Added checking permissions if the requested resource is a library
(permissions are automatically handled by models) using PermissionLib
- Extended PermissionLib
This commit is contained in:
paolo
2016-08-01 14:07:40 +02:00
parent 1c01faba5f
commit c8c580131c
3 changed files with 453 additions and 225 deletions
+26 -211
View File
@@ -12,159 +12,28 @@
*/
// ------------------------------------------------------------------------
if(!defined("BASEPATH")) exit("No direct script access allowed");
if (!defined("BASEPATH")) exit("No direct script access allowed");
class PCRM extends APIv1_Controller
{
// Black list of resources that are no allowed to be used
private static $RESOURCES_BLACK_LIST = array("LogLib", "FilesystemLib", "MigrationLib", "REST_Controller");
/**
* Message API constructor.
* API constructor
*/
public function __construct()
{
parent::__construct();
// Loads the PCRMLib
$this->load->library("PCRMLib");
}
/**
* @return void
* Manages a HTTP get call
*/
public function getCall()
{
$parameters = $this->_getParameters($this->get());
$validation = $this->_validateCall($parameters);
// If the validation was passed
if($validation->error == EXIT_SUCCESS)
{
$loaded = null;
// Check if the resource is already loaded, it works only with libraries and drivers
if (($loaded = $this->load->is_loaded($parameters->resourceName)) === false)
{
// If the given resource is a model
if(strpos($parameters->resourceName, "_model") !== false)
{
try
{
// Try to load it
$loaded = $this->load->model($parameters->resourcePath . $parameters->resourceName);
}
catch(Exception $e)
{
// Errors while loading the model
$loaded = null;
$result = $this->_error($e->getMessage());
}
}
// If the given resource is a library
else if(strpos($parameters->resourceName, "Lib") !== false)
{
// The method "library" of the class CI_Loader provided by CI has some limitations,
// so to be able to check errors was used this workaround
try
{
// Gets all the configured resources paths
$packagePaths = $this->load->get_package_paths();
// Looking for a file in every paths with the same name of the resource
$found = null;
for ($i = 0; $i < count($packagePaths) && is_null($found); $i++)
{
if (file_exists($packagePaths[$i] . "libraries/" . $parameters->resourcePath . $parameters->resourceName . ".php"))
{
$found = $packagePaths[$i] . "libraries/" . $parameters->resourcePath . $parameters->resourceName . ".php";
}
}
// If the file was found
if (!is_null($found))
{
// Load the file
$loaded = $this->load->file($found);
// If the resource is not present inside the file
if (!class_exists($parameters->resourceName))
{
$loaded = null;
// Same phrase error as load->model()
$result = $this->_error($found . " exists, but doesn't declare class " . $parameters->resourceName);
}
}
else
{
$loaded = null;
// Same phrase error as load->model()
$result = $this->_error("Unable to load the requested class: " . $parameters->resourceName);
}
}
catch(Exception $e)
{
$result = $this->_error($e->getMessage());
}
}
// Wrong selection!
else
{
$result = $this->_error("Neither a lib nor model: " . $parameters->resourcePath . $parameters->resourceName);
}
}
// If the resource was found and loaded
if(!is_null($loaded))
{
try
{
// Get informations about the function
$reflectionMethod = new ReflectionMethod($parameters->resourceName, $parameters->function);
// If the number of given parameters is equal to the number of parameters required by the function
if ($reflectionMethod->getNumberOfRequiredParameters() == count($parameters->parameters))
{
// If the function is static
if ($reflectionMethod->isStatic() === true)
{
$classMethod = $parameters->resourceName . "::" . $parameters->function;
}
// If the function is not static
else
{
$classMethod = array(new $parameters->resourceName(), $parameters->function);
}
// If the function of that resource is callable
if(is_callable($classMethod))
{
// Call resource->function()
$resultCall = @call_user_func_array($classMethod, $parameters->parameters);
// If errors occurred while running it
if($resultCall === false)
{
$result = $this->_error("Error running " . $parameters->resourceName . "->" . $parameters->function . "()");
}
// Returns the result of resource->function()
else
{
$result = $resultCall;
}
}
else
{
$result = $this->_error($parameters->resourceName . "->" . $parameters->function . "() is not callable!");
}
}
else
{
$result = $this->_error("Wrong parameters number");
}
}
catch(Exception $e)
{
$result = $this->_error($e->getMessage());
}
}
}
else
{
$result = $validation;
}
// Start me up!
$result = $this->pcrmlib->start($this->get(), PermissionLib::SELECT_RIGHT);
// Print the result
$this->response($result, REST_Controller::HTTP_OK);
@@ -175,88 +44,34 @@ class PCRM extends APIv1_Controller
*/
public function postCall()
{
$validation = $this->_validatePostMessage($this->post());
// Start me up!
$result = $this->pcrmlib->start($this->post(), PermissionLib::UPDATE_RIGHT);
if(is_object($validation) && $validation->error == EXIT_SUCCESS)
{
$result = $this->messagelib->sendMessage(
$this->post()['person_id'], $this->post()['subject'], $this->post()['body'], PRIORITY_NORMAL, $this->post()['relationmessage_id'], $this->post()['oe_kurzbz']
);
$this->response($result, REST_Controller::HTTP_OK);
}
else
{
$this->response($validation, REST_Controller::HTTP_OK);
}
// Print the result
$this->response($result, REST_Controller::HTTP_OK);
}
/**
* Gets the parameters from the call
* @return void
*/
private function _getParameters($parametersArray)
public function putCall()
{
$parameters = new stdClass();
$parameters->parameters = array();
$count = 0;
// Start me up!
$result = $this->pcrmlib->start($this->put(), PermissionLib::INSERT_RIGHT);
foreach($parametersArray as $parameterName => $parameterValue)
{
// The name of the resource, path included
if($parameterName == "resource")
{
// Separates the resource path from the resource name
$splittedResource = preg_split("/\//", $parameterValue);
$parameters->resourceName = $splittedResource[count($splittedResource) - 1];
$parameters->resourcePath = str_replace($parameters->resourceName, "", $parameterValue);
}
// The name of the function
else if($parameterName == "function")
{
$parameters->function = $parameterValue;
}
// It is assumed that all other parameters are parameters to be passed to the function
// They will be passed to the function in the same order in which they are passed to
// this controller
else
{
$parameters->parameters[$count++] = $parameterValue;
}
}
return $parameters;
// Print the result
$this->response($result, REST_Controller::HTTP_OK);
}
/**
* Validate the given parameters
* @return void
*/
private function _validateCall($parameters)
public function deleteCall()
{
if (!is_object($parameters))
{
return $this->_error("Parameter is not an object");
}
if (!isset($parameters->resourcePath))
{
return $this->_error("Resource path is not specified");
}
if (!isset($parameters->resourceName))
{
return $this->_error("Resource name is not specified");
}
if (!isset($parameters->function))
{
return $this->_error("Function is not specified");
}
if (!is_array($parameters->parameters))
{
return $this->_error("Parameters are not specified");
}
if (in_array($parameters->resourceName, PCRM::$RESOURCES_BLACK_LIST))
{
return $this->_error("You are trying to access to unauthorized resources");
}
// Start me up!
$result = $this->pcrmlib->start($this->delete(), PermissionLib::DELETE_RIGHT);
return $this->_success("Input data are valid");
// Print the result
$this->response($result, REST_Controller::HTTP_OK);
}
}