-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
85 lines (71 loc) · 2.97 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace CivAccess;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$eventManager = $app->getEventManager();
$serviceManager = $app->getServiceManager();
$guards = $serviceManager->get('CivAccess\Guards');
$strategy = $serviceManager->get('CivAccess\DeniedStrategy');
$config = $serviceManager->get('config')['CivAccess'];
// Register a "render" event, at high priority (so it executes prior
// to the view attempting to render)
$eventManager->attach('render', array($this, 'registerJsonStrategy'), 100);
// Attach the guards as listeners.
foreach ($guards as $guard) {
$eventManager->attach($guard);
}
// Attach a strategy to take if access is denied.
$eventManager->attach($strategy);
// listen for new roles (if configured)
if ($config['new_role_event_id'] != '') {
$param = $config['new_role_event_param'];
$sharedEventManager = $eventManager->getSharedManager();
$sharedEventManager->attach($config['new_role_event_id'], $config['new_role_event'], function($e) use($serviceManager, $param) {
$role = $e->getParam($param)->getId();
$service = $serviceManager->get('CivAccess\AclService');
$service->addRole($role, 'user');
}, 100);
}
// listen for deleted roles (if configured)
if ($config['old_role_event_id'] != '') {
$param = $config['old_role_event_param'];
$sharedEventManager = $eventManager->getSharedManager();
$sharedEventManager->attach($config['old_role_event_id'], $config['old_role_event'], function($e) use($serviceManager, $param) {
$role = $e->getParam($param);
$service = $serviceManager->get('CivAccess\AclService');
$service->deleteRoleByRole($role);
}, 100);
}
}
/**
* @param \Zend\Mvc\MvcEvent $e The MvcEvent instance
* @return void
*/
public function registerJsonStrategy($e)
{
$app = $e->getTarget();
$locator = $app->getServiceManager();
$view = $locator->get('Zend\View\View');
$jsonStrategy = $locator->get('ViewJsonStrategy');
// Attach strategy, which is a listener aggregate, at high priority
$view->getEventManager()->attach($jsonStrategy, 100);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}