mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-20 16:32:20 +00:00
Merge branch 'master' into permissions
- Added new core controller called Auth_Controller that extends FHC_Controller and manage the authentication - All the controllers that were extending the CI_Controller now they extend the FHC_Controller - All the controllers that were extending the FHC_Controller now they extend the Auth_Controller - Added the method isAllowed to the FiltersLib to check if the authenticated user has the required permissions - FilterWidget and controller Filters are using the method isAllowed from the FiltersLib
This commit is contained in:
@@ -4,45 +4,101 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class FHC_Controller extends CI_Controller
|
||||
{
|
||||
const FHC_CONTROLLER_ID = 'fhc_controller_id'; // name of the parameter used to identify uniquely a call to a controller
|
||||
|
||||
private $_controllerId; // contains the unique identifier of a call to a controller
|
||||
|
||||
/**
|
||||
* Standard construct for all the controllers, loads the authentication system
|
||||
* Checks the caller permissions
|
||||
* Standard construct for all the controllers
|
||||
*/
|
||||
public function __construct($requiredPermissions)
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Loads authentication helper
|
||||
$this->load->helper('fhcauth');
|
||||
|
||||
// Loads permission lib
|
||||
$this->load->library('PermissionLib');
|
||||
|
||||
$this->_isAllowed($requiredPermissions);
|
||||
// Set _controllerId as null by default
|
||||
$this->_controllerId = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 _checkPermissions
|
||||
*/
|
||||
private function _isAllowed($requiredPermissions)
|
||||
{
|
||||
if (!$this->permissionlib->isEntitled($requiredPermissions, $this->router->method))
|
||||
{
|
||||
header('HTTP/1.0 401 Unauthorized');
|
||||
echo 'You are not allowed to access to this content';
|
||||
exit;
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Public methods
|
||||
|
||||
/**
|
||||
* Wrapper to load phrases using the PhrasesLib
|
||||
* NOTE: The library is loaded with the alias 'p', so must me used with this alias in the rest of the code.
|
||||
* EX: $this->p->t(<category>, <phrase name>)
|
||||
*/
|
||||
public function loadPhrases($categories, $language = null)
|
||||
protected function loadPhrases($categories, $language = null)
|
||||
{
|
||||
$this->load->library('PhrasesLib', array($categories, $language), 'p');
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Protected methods
|
||||
|
||||
/**
|
||||
* Sets the unique id for the called controller
|
||||
* NOTE: it is only working with HTTP GET request, not neeaded with POST
|
||||
* because the first call to the controller is via HTTP GET,
|
||||
* therefore a fhc_controller_id is already generated
|
||||
*/
|
||||
protected function setControllerId()
|
||||
{
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET')
|
||||
{
|
||||
$this->_controllerId = $this->input->get(self::FHC_CONTROLLER_ID);
|
||||
|
||||
if (!isset($this->_controllerId) || empty($this->_controllerId))
|
||||
{
|
||||
$this->_controllerId = uniqid(); // generate a unique id
|
||||
// Redirect to the same URL, but giving FHC_CONTROLLER_ID as HTTP GET parameter
|
||||
header(
|
||||
sprintf(
|
||||
'Location: %s%s%s=%s',
|
||||
$_SERVER['REQUEST_URI'],
|
||||
strpos($_SERVER['REQUEST_URI'], '?') === false ? '?' : '&', // place the corret character to divide parameters
|
||||
self::FHC_CONTROLLER_ID,
|
||||
$this->_controllerId
|
||||
)
|
||||
);
|
||||
exit; // terminate immediately the execution of this controller
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the property _controllerId
|
||||
*/
|
||||
protected function getControllerId()
|
||||
{
|
||||
return $this->_controllerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to output a success using JSON as content type
|
||||
* Wraps the private method _outputJson
|
||||
*/
|
||||
protected function outputJsonSuccess($mixed)
|
||||
{
|
||||
$this->_outputJson(success($mixed));
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to output an error using JSON as content type
|
||||
* Wraps the private method _outputJson
|
||||
*/
|
||||
protected function outputJsonError($mixed)
|
||||
{
|
||||
$this->_outputJson(error($mixed));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Private methods
|
||||
|
||||
/**
|
||||
* Utility method to output using JSON as content type
|
||||
*/
|
||||
private function _outputJson($mixed)
|
||||
{
|
||||
$this->output->set_content_type('application/json')->set_output(json_encode($mixed));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user