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
|
<?php
namespace Psecio\Gatekeeper;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
class PolicyModel extends \Psecio\Gatekeeper\Model\Mysql
{
/**
* Database table name
* @var string
*/
protected $tableName = 'policies';
/**
* Model properties
* @var array
*/
protected $properties = array(
'id' => array(
'description' => 'User ID',
'column' => 'id',
'type' => 'integer'
),
'expression' => array(
'description' => 'Policy Expression',
'column' => 'expression',
'type' => 'string'
),
'description' => array(
'description' => 'Policy Description',
'column' => 'description',
'type' => 'string'
),
'name' => array(
'description' => 'Policy Name',
'column' => 'name',
'type' => 'string'
),
'created' => array(
'description' => 'Date Created',
'column' => 'created',
'type' => 'datetime'
),
'updated' => array(
'description' => 'Date Updated',
'column' => 'updated',
'type' => 'datetime'
),
);
public function evaluate($data, $expression = null)
{
if ($this->id === null) {
throw new \InvalidArgumentException('Policy not loaded!');
}
$expression = ($expression === null) ? $this->expression : $expression;
if (!is_array($data)) {
$data = array($data);
}
$context = array();
foreach ($data as $index => $item) {
if (is_numeric($index)) {
// Resolve it to a class name
$ns = explode('\\', get_class($item));
$index = str_replace('Model', '', array_pop($ns));
}
$context[strtolower($index)] = $item;
}
$language = new ExpressionLanguage();
try {
return $language->evaluate($expression, $context);
} catch (\Exception $e) {
throw new Exception\InvalidExpressionException($e->getMessage());
}
}
}
|