This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Controller.php
175 lines (149 loc) · 5 KB
/
Controller.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
*
* @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace BjyAuthorize\Guard;
use BjyAuthorize\Exception\UnAuthorizedException;
use BjyAuthorize\Provider\Rule\ProviderInterface as RuleProviderInterface;
use BjyAuthorize\Provider\Resource\ProviderInterface as ResourceProviderInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Mvc\ApplicationInterface;
use BjyAuthorize\Service\Authorize;
use Zend\Http\Request as HttpRequest;
/**
* Controller Guard listener, allows checking of permissions
* during {@see \Zend\Mvc\MvcEvent::EVENT_DISPATCH}
*
* @author Ben Youngblood <bx.youngblood@gmail.com>
*/
class Controller implements GuardInterface, RuleProviderInterface, ResourceProviderInterface
{
/**
* Marker for invalid route errors
*/
const ERROR = 'error-unauthorized-controller';
/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator;
/**
* @var array[]
*/
protected $rules = array();
/**
* @var \Zend\Stdlib\CallbackHandler[]
*/
protected $listeners = array();
/**
* @param array $rules
* @param ServiceLocatorInterface $serviceLocator
*/
public function __construct(array $rules, ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
foreach ($rules as $rule) {
if (!is_array($rule['roles'])) {
$rule['roles'] = array($rule['roles']);
}
$rule['action'] = isset($rule['action']) ? (array) $rule['action'] : array(null);
foreach ((array) $rule['controller'] as $controller) {
foreach ($rule['action'] as $action) {
$this->rules[$this->getResourceName($controller, $action)] = $rule['roles'];
}
}
}
}
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'onDispatch'), -1000);
}
/**
* {@inheritDoc}
*/
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}
/**
* {@inheritDoc}
*/
public function getResources()
{
$resources = array();
foreach (array_keys($this->rules) as $resource) {
$resources[] = $resource;
}
return $resources;
}
/**
* {@inheritDoc}
*/
public function getRules()
{
$rules = array();
foreach ($this->rules as $resource => $roles) {
$rules[] = array($roles, $resource);
}
return array('allow' => $rules);
}
/**
* Retrieves the resource name for a given controller
*
* @param string $controller
* @param string $action
*
* @return string
*/
public function getResourceName($controller, $action = null)
{
if (isset($action)) {
return sprintf('controller/%s:%s', $controller, $action);
}
return sprintf('controller/%s', $controller);
}
/**
* Event callback to be triggered on dispatch, causes application error triggering
* in case of failed authorization check
*
* @param MvcEvent $event
*
* @return void
*/
public function onDispatch(MvcEvent $event)
{
/* @var $service \BjyAuthorize\Service\Authorize */
$service = $this->serviceLocator->get('BjyAuthorize\Service\Authorize');
$match = $event->getRouteMatch();
$controller = $match->getParam('controller');
$action = $match->getParam('action');
$request = $event->getRequest();
$method = $request instanceof HttpRequest ? strtolower($request->getMethod()) : null;
$authorized = $service->isAllowed($this->getResourceName($controller))
|| $service->isAllowed($this->getResourceName($controller, $action))
|| ($method && $service->isAllowed($this->getResourceName($controller, $method)));
if ($authorized) {
return;
}
$event->setError(static::ERROR);
$event->setParam('identity', $service->getIdentity());
$event->setParam('controller', $controller);
$event->setParam('action', $action);
$errorMessage = sprintf("You are not authorized to access %s:%s", $controller, $action);
$event->setParam('exception', new UnAuthorizedException($errorMessage));
/* @var $app \Zend\Mvc\ApplicationInterface */
$app = $event->getTarget();
$app->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
}
}