Skip to content

Instantly share code, notes, and snippets.

@macnibblet
Created July 21, 2012 11:44
Show Gist options
  • Save macnibblet/3155587 to your computer and use it in GitHub Desktop.
Save macnibblet/3155587 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Antoine Hedgecock
*/
/**
* @namespace
*/
namespace Application;
use Zend\Acl\Acl,
Zend\View\Helper,
Zend\Mvc\MvcEvent,
Zend\Mvc\Router\RouteMatch,
Zend\Log\LoggerAwareInterface,
Zend\ServiceManager\ServiceManager,
Zend\ServiceManager\ServiceLocatorAwareInterface,
Zend\ModuleManager\Feature\ConfigProviderInterface,
Zend\ModuleManager\Feature\ServiceProviderInterface,
Zend\ModuleManager\Feature\AutoloaderProviderInterface,
Zend\ModuleManager\Feature\ViewHelperProviderInterface;
class Module implements ConfigProviderInterface, AutoloaderProviderInterface, ViewHelperProviderInterface, ServiceProviderInterface
{
/**
* @var \Zend\ServiceManager\ServiceManager
*/
protected $serviceManager;
/**
* @param \Zend\Mvc\MvcEvent $e
*/
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()
->getEventManager()
->attach(MvcEvent::EVENT_ROUTE, array($this, 'selectLayoutBasedOnRoute'));
}
/**
* @param \Zend\Mvc\MvcEvent $e
*
* @return mixed
*/
public function selectLayoutBasedOnRoute(MvcEvent $e)
{
// Get the current route match
$match = $e->getRouteMatch();
// Get the current user
$user = $e->getApplication()
->getServiceManager()
->get('user_service_auth')
->getIdentity();
if ($match instanceof RouteMatch) {
$exp = explode('/', $match->getMatchedRouteName());
if ($exp[0] == 'admin') {
if ($user->getRole() != 'administrator') {
return $e->getResponse()
->setStatusCode(401)
->getHeaders()
->addHeaderLine('location', '/');
}
$e->getViewModel()
->setTemplate('layout/admin');
// Get the view manager
$vm = $e->getApplication()
->getServiceManager()
->get('view_manager');
var_dump(get_class_methods($vm));
exit;
}
}
}
/**
* Expected to return \Zend\ServiceManager\Configuration object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Configuration
*/
public function getServiceConfiguration()
{
return array(
'initializers' => array(
function($instance, $sm) {
if ($instance instanceof LoggerAwareInterface) {
$instance->setLogger($sm->get('mcn.logger'));
}
}
),
'factories' => array(
'acl' => function() {
$acl = new Acl();
$acl->addRole('guest')
->addRole('user', 'guest')
->addRole('administrator', 'user');
$acl->addResource('guest-only')
->allow('guest', 'guest-only')
->deny('user', 'guest-only');
$acl->addResource('user')
->allow('user', 'user');
$acl->addResource('administrator')
->allow('administrator', 'administrator');
return $acl;
}
)
);
}
/**
* Expected to return \Zend\ServiceManager\Configuration object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Configuration
*/
public function getViewHelperConfiguration()
{
return array(
'initializers' => array(
function($instance, $viewServiceManager) {
if ($instance instanceof ServiceLocatorAwareInterface) {
$instance->setServiceLocator($viewServiceManager->getServiceLocator());
}
}
),
'factories' => array(
'navigation' => function($sm) {
$navigation = $sm->get('Zend\View\Helper\Navigation');
$navigation->setAcl($sm->getServiceLocator()->get('acl'));
$navigation->setRole($sm->getServiceLocator()->get('user_service_auth')->getIdentity()->getRole());
return $navigation;
}
)
);
}
/**
* Return an array for passing to Zend\Loader\AutoloaderFactory.
*
* @return array
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
/**
* Returns configuration to merge with application configuration
*
* @return array|\Traversable
*/
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment