forked from jamalsa/minium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.php
48 lines (40 loc) · 1.45 KB
/
filters.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
<?php
use lithium\action\Dispatcher;
use lithium\net\http\Router;
use lithium\action\Response;
use lithium\security\Auth;
// Filter all request
Dispatcher::applyFilter('run', function($self, $params, $chain) {
// Do something before
$result = $chain->next($self, $params, $chain);
// Do something after
return $result;
});
// Filter GET request
Dispatcher::applyFilter('run', function($self, $params, $chain) {
if($params['request']->method == 'GET') {
// Do something before
}
$result = $chain->next($self, $params, $chain);
if($params['request']->method == 'GET') {
// Do something after
}
return $result;
});
// Protect some routes from unauthorized user
Dispatcher::applyFilter('run', function($self, $params, $chain) {
// First, define our list of protected actions
$blacklist = array(
'/users/report',
'/users/home'
);
// Inspect the request to get the URL for the route the request matches
$matches = in_array(Router::match($params['request']->params, $params['request']), $blacklist);
// If this is a match, check it against an Auth configuration.
if($matches && !Auth::check('default', $params['request'])) {
// If the Auth check can't verify the user, redirect.
return new Response(array('location' => '/users/login'));
}
// Important: return the results of the next filter in the chain.
return $chain->next($self, $params, $chain);
});