-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathPolicyExtension.php
More file actions
62 lines (50 loc) · 1.31 KB
/
PolicyExtension.php
File metadata and controls
62 lines (50 loc) · 1.31 KB
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
<?php
namespace ProcessMaker;
use ProcessMaker\Models\ProcessMakerModel;
use ProcessMaker\Models\User;
class PolicyExtension
{
private $extensions;
public function __construct()
{
$this->extensions = [];
}
private function key(string $action, string $class)
{
return $action . '-' . $class;
}
public function has(string $action, string $class)
{
return array_key_exists(
$this->key($action, $class),
$this->extensions
);
}
public function add(string $action, string $class, callable $policy)
{
$key = $this->key($action, $class);
if (!$this->has($action, $class)) {
$this->extensions[$key] = [];
}
$this->extensions[$key][] = $policy;
}
public function authorize(string $action, User $user, ProcessMakerModel $model)
{
$class = get_class($model);
if (!$this->has($action, $class)) {
return false;
}
$ok = false;
foreach ($this->extensions[$this->key($action, $class)] as $extension) {
$ok = $extension($user, $model);
if ($ok) {
break;
}
}
return $ok;
}
public function getExtensions()
{
return array_keys($this->extensions);
}
}