Created
September 25, 2012 18:47
-
-
Save bjyoungblood/3783670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Dashboard\View; | |
use SmsCommon\Acl\NotAllowedException; | |
use Zend\EventManager\EventManagerInterface; | |
use Zend\EventManager\ListenerAggregateInterface; | |
use Zend\Http\Response as HttpResponse; | |
use Zend\Mvc\Application; | |
use Zend\Mvc\MvcEvent; | |
use Zend\Stdlib\ResponseInterface as Response; | |
use Zend\View\Model\ViewModel; | |
class NotAllowedExceptionStrategy implements ListenerAggregateInterface | |
{ | |
/** | |
* @var string | |
*/ | |
protected $template = 'error/not-allowed'; | |
/** | |
* @var \Zend\Stdlib\CallbackHandler[] | |
*/ | |
protected $listeners = array(); | |
public function attach(EventManagerInterface $events) | |
{ | |
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -2500); | |
} | |
public function detach(EventManagerInterface $events) | |
{ | |
foreach ($this->listeners as $index => $listener) { | |
if ($events->detach($listener)) { | |
unset($this->listeners[$index]); | |
} | |
} | |
} | |
public function setTemplate($template) | |
{ | |
$this->template = $template; | |
} | |
public function getTemplate() | |
{ | |
return $this->template; | |
} | |
public function onDispatchError(MvcEvent $e) | |
{ | |
// Do nothing if the result is a response object | |
$result = $e->getResult(); | |
if ($result instanceof Response) { | |
return; | |
} | |
// Common view variables | |
$viewVariables = array( | |
'error' => $e->getParam('error'), | |
'identity' => $e->getParam('identity'), | |
); | |
$error = $e->getError(); | |
if ($error == 'error-exception') { | |
$params = $e->getParams(); | |
$exception = $params['exception']; | |
if (!$exception instanceof NotAllowedException) { | |
return false; | |
} | |
} else { | |
return; | |
} | |
$model = new ViewModel($viewVariables); | |
$model->setTemplate($this->getTemplate()); | |
$e->getViewModel()->addChild($model); | |
$response = $e->getResponse(); | |
if (!$response) { | |
$response = new HttpResponse(); | |
$e->setResponse($response); | |
} | |
$response->setStatusCode(403); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment