<?php
/**
* Library plugin to check whether the user has access to the current page
*
* @uses Test_Controller_Plugin_HasAccess
* @copyright
* @license
* @version 1.0
* @author Rathinasamy
* @created Sep 30, 2011
*/
class Zaah_Controller_Plugin_HasAccess extends Zend_Controller_Plugin_Abstract
{
public function preDispatch( Zend_Controller_Request_Abstract $request )
{
$isLoggedIn = Zend_Auth::getInstance()->hasIdentity();
// Save some cycles if we're already logged in
if( $isLoggedIn ) {
return;
}
$config = $this->_getConfig();
$action = $request->getParam( 'action' );
$controller = $request->getParam( 'controller' );
// Make sure we don't end up in a loop
if( $controller == $config->loginController && $action == $config->loginAction ){
return;
}
$secure = $this->_checkIsSecure($request->getParam( 'action' ), $request->getParam( 'controller' ));
if( $secure ) {
$url = Zend_Controller_Action_HelperBroker::getStaticHelper('url');
$v = $url->url(array('controller'=>$config->loginController,'action'=>$config->loginAction));
$this->getResponse()->setHttpResponseCode(403);
$request->setParam( 'ref', $request->getPathInfo() );
$request->setControllerName( $config->loginController );
$request->setActionName( $config->loginAction );
$this->getResponse()->setHeader("Location",$v);
$request->setDispatched( true );
/*$url = Zend_Controller_Action_HelperBroker::getStaticHelper('url');
$v = $url->url(array('controller'=>$config->loginController,'action'=>$config->loginAction));
$this->getResponse()->setRedirect($v,403); */
return;
}
}
/**
* Load the configuration.
*
* @return Zend_Config_Ini
*/
protected function _getConfig()
{
static $config = null;
if( null === $config ) {
$config = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/access.ini' , 'global' );
}
return $config;
}
protected function _checkIsSecure( $action, $controller, $module = 'default' )
{
$config = $this->_getConfig();
// If no match is found, what should be the default?
$public = ( isset( $config->defaultAccess ) && $config->defaultAccess == 'public' );
// Check the action level, then controller
if( isset( $config->controllers->$controller->actions->$action->access ) ) {
$public = ( $config->controllers->$controller->actions->$action->access == 'public' );
} elseif( isset( $config->controllers->$controller->access ) ) {
$public = ( $config->controllers->$controller->access == 'public' );
}
return !$public;
}
}
/**
* Library plugin to check whether the user has access to the current page
*
* @uses Test_Controller_Plugin_HasAccess
* @copyright
* @license
* @version 1.0
* @author Rathinasamy
* @created Sep 30, 2011
*/
class Zaah_Controller_Plugin_HasAccess extends Zend_Controller_Plugin_Abstract
{
public function preDispatch( Zend_Controller_Request_Abstract $request )
{
$isLoggedIn = Zend_Auth::getInstance()->hasIdentity();
// Save some cycles if we're already logged in
if( $isLoggedIn ) {
return;
}
$config = $this->_getConfig();
$action = $request->getParam( 'action' );
$controller = $request->getParam( 'controller' );
// Make sure we don't end up in a loop
if( $controller == $config->loginController && $action == $config->loginAction ){
return;
}
$secure = $this->_checkIsSecure($request->getParam( 'action' ), $request->getParam( 'controller' ));
if( $secure ) {
$url = Zend_Controller_Action_HelperBroker::getStaticHelper('url');
$v = $url->url(array('controller'=>$config->loginController,'action'=>$config->loginAction));
$this->getResponse()->setHttpResponseCode(403);
$request->setParam( 'ref', $request->getPathInfo() );
$request->setControllerName( $config->loginController );
$request->setActionName( $config->loginAction );
$this->getResponse()->setHeader("Location",$v);
$request->setDispatched( true );
/*$url = Zend_Controller_Action_HelperBroker::getStaticHelper('url');
$v = $url->url(array('controller'=>$config->loginController,'action'=>$config->loginAction));
$this->getResponse()->setRedirect($v,403); */
return;
}
}
/**
* Load the configuration.
*
* @return Zend_Config_Ini
*/
protected function _getConfig()
{
static $config = null;
if( null === $config ) {
$config = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/access.ini' , 'global' );
}
return $config;
}
protected function _checkIsSecure( $action, $controller, $module = 'default' )
{
$config = $this->_getConfig();
// If no match is found, what should be the default?
$public = ( isset( $config->defaultAccess ) && $config->defaultAccess == 'public' );
// Check the action level, then controller
if( isset( $config->controllers->$controller->actions->$action->access ) ) {
$public = ( $config->controllers->$controller->actions->$action->access == 'public' );
} elseif( isset( $config->controllers->$controller->access ) ) {
$public = ( $config->controllers->$controller->access == 'public' );
}
return !$public;
}
}
No comments:
Post a Comment