Bootloader file
<?PHP
try{
// Load config
$Config = require '../app/config/config.php';
// Register an autoloader
$Loader = new \Phalcon\Loader();
$Loader->registerDirs($Config->phalcon->loadDirs)->register();
// Create a Dependency Injection container
$DI = new \Phalcon\DI\FactoryDefault();
// Set up the Dispatcher
$DI->set('dispatcher',function() use ($DI){
// Get the standard eventsManager
$EM = $DI->getShared('eventsManager');
// Instantiate the Security plugin
$Security = new Security($DI);
// Listen for events produced by the Dispatcher using the Security plugin
$EM->attach('dispatch',$Security);
// Set the dispatcher to the custom dispatcher with the newly modified Events Manager
$Dispatcher = new Phalcon\Mvc\Dispatcher();
$Dispatcher->setEventsManager($EM);
return $Dispatcher;
});
// Set up the View component
$DI->set('view',function() use ($Config){
$View = new \Phalcon\Mvc\View();
$View->setViewsDir($Config->app->appDir.'/views/');
return $View;
});
#
# User object is created in Security
#
# Initialize Session
$Session = new Phalcon\Session\Adapter\Files();
$Session->start();
$Session->config = $Config;
$DI->setShared('session',$Session);
// Handle the requested URL
$App = new \Phalcon\Mvc\Application($DI);
// Echo the output
echo $App->handle()->getContent();
}
catch(\Phalcon\Exception $e){
echo 'Phalcon Exception: ',$e->getMessage();
}
Relevant parts of the Security plugin
<?PHP
#
#
# Relevant parts of the "Security" plugin
#
#
// "beforeExecuteRoute" is the name of the event for which we want this function to be called
public function beforeExecuteRoute($Event,$Dispatcher){
$role = $this->getRole();
$controller = $Dispatcher->getControllerName();
$action = $Dispatcher->getActionName();
$allowed = $this->getAcl()->isAllowed($role,$controller,$action);
if($allowed != Phalcon\Acl::ALLOW){
$this->flash->error('You are not allowed to view this page.');
$Dispatcher->forward(['controller'=>'index','action'=>'index']);
// Returning false will tell the Dispatcher to stop the current operation
return FALSE;
}
else
{
$User = new User( [ 'level' => RemoteAuth::$level,
'name' => RemoteAuth::$name,
'ccid' => RemoteAuth::$ccid,
'role' => $role
]
);
$DI = $Dispatcher->getDI();
$DI->setShared('user',$User);
}
}
###
# Get the role for the logged in user
###
private function getRole(){
/* RemoteAuth is the library used by all organizational web apps for central authentication.
Consequently, it doesn't tie in with Phalcon 100%. Creating a new RemoteAuth object
with no session, will cause the "You have no session" page to appear, rather than
proper routing via Phalcon
*/
$RemoteAuth = new RemoteAuth('media',999);
$level = RemoteAuth::$level;
//need to convert access levels to "roles" for proper ACL integration
$roles = $this->session->config->roles;
$role = (isset($roles[$level])) ? $roles[$level] : FALSE;
$this->session->role = $role;
return $role;
}