Bardzo dobrym rozwiązaniem jest zastosowanie Pluginow (takich jak w zendzie)
robisz sobie we FrontControllerze:
<?php
...
public function execute()
{
$this->_oDispatcher = new Dispatcher();
$this->_oActionCollectioner = new ActionCollectioner;
$this->_oRouter = new Router($this->_oDispatcher, $this->_oActionCollectioner);
$aError404Action = $this->getAppConfig()->get('Errors','Error404');
$this->_oPluginManager->routeStartup(); //filtr przed startem routera
$oActionToken = $this->_oRouter->route();
$oActionToken = $this->_oPluginManager->routeShutdown($oActionToken); //filtr po starcie
$this->_oActionCollectioner->pushAct($oActionToken);
foreach ($this->_oActionCollectioner as $iKey => $oActionToken)
{
$oActionToken = $this->_oPluginManager->preDispatch($oActionToken);
if(Dispatcher::isDispatchable($oActionToken))
{
NyssSystem::setLibraryPath($oActionToken->getControllerName(),NyssSystem::formatControllerPath($oActionToken->getControllerName()));
$sController = $oActionToken->getControllerName();
$oCurrentController = new $sController;
if(!method_exists($oCurrentController, $oActionToken->getActionName()))
$oActionToken->setActionName(_DEFAULT_ACT_NAME);
unset($oCurrentController); }
else
$oActionToken = new DispatcherToken($aError404Action['Controller'], $aError404Action['Action'], $this->_oRouter->getActionParams());
if (!Dispatcher::isDispatchable($oActionToken)) {
throw new FrontControllerException('Request could not be mapped to a route and Error404 Controller has not been crea
ted.');
}
$oActionToken = $this->_oPluginManager->preExecution($oActionToken); //przed execute I TO WAZNE
$this->_CurrentAction = $oActionToken->getActionName();
$this->_oDispatcher->executeAction($oActionToken);
}
$this->_oPluginManager->dispatchLoopShutdown(); //Filtr po zakonczeniu petli, ktora sprawdza akcje
}
...
?>
Dalej resjestrujemy sobie nowy plugin - w indeksie
<?php
...
$oFrontController->registerPlugin(new AclPlugin);
...
?>
No i sam plugin wyglada nastepujaco:
<?php
class AclPlugin extends PluginLayer implements IPlugin
{
public function preExecution(IDispatcherToken $oToken)
{
$oACL = new ACL;
$sController = $oToken->getControllerName();
$sAction = $oToken->getActionName();
NyssSystem::setLibraryPath($sController, NyssSystem::formatControllerPath($oToken->getControllerName()));
$sController = ucfirst($sController);
$oController = new $sController;
$oConfig = $oController->getConfig();
/**
* If security section in configuration has not been defined,
* leave the plugin.
*/
if(!$oConfig->has('Actions', $sAction, 'Security'))
return $oToken;
/**
* Add allow groups to the current container.
*/
if($oConfig->has('Actions', $sAction, 'Security', 'Groups', 'Allow'))
{
$aAllow = $oConfig->get('Actions', $sAction, 'Security', 'Groups', 'Allow');
{
$sError = sprintf('Allow groups for action %s in controller %s must be an array', $sAction, $sController);
throw new ACLPluginException($sError);
}
$oAccess = new Access;
foreach($aAllow as $sGroup)
$oAccess->AddAllowGroup($sGroup);
$oACL->add($oAccess, $sController, $sAction);
}
/**
* Add deny groups to the current container.
*/
if($oConfig->has('Actions', $sAction, 'Security', 'Groups', 'Deny'))
{
$aDeny = $oConfig->get('Actions', $sAction, 'Security', 'Groups', 'Deny');
{
$sError = sprintf('Deny groups for action %s in controller %s must be an array', $sAction, $sController);
throw new ACLPluginException($sError);
}
$oDeny = new Access;
foreach($aDeny as $sGroup)
$oDeny->deny($sGroup);
$oACL->add($oDeny, $sController, $sAction);
}
/**
* Make the access control test and forward to the another action,
* if user has not got enough privilages.
*/
$oConfig = FrontController::getInstance()->getAppConfig();
$Error403Controller = $oConfig->get('Error403','Controller') ? $oConfig->get('Error403','Controller') : 'Errors';
$Error403Action = $oConfig->get('Error403','Action') ? $oConfig->get('Error403','Action') : 'Error403';
if(!$oACL->access(new User, $sController, $sAction))
{
$oToken = new DispatcherToken($Error403Controller,$Error403Action);
}
return $oToken;
}
}
?>
Czyli to powyzej wczytuje konfig sprawdza jaka grupe musi miec user, zeby dostac sie do danej akcji, a nastepnie jesli nie ma dostepu wstawia tokena Error403 i zwraca do petli we FrontControllerze.