-
Notifications
You must be signed in to change notification settings - Fork 4
/
Manager.php
196 lines (168 loc) · 6.03 KB
/
Manager.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace SamIT\abac\connectors\yii2;
use SamIT\abac\AuthorizableDummy;
use SamIT\abac\interfaces\Authorizable;
use SamIT\abac\interfaces\Rule;
class Manager extends \SamIT\abac\Manager implements \yii\rbac\CheckAccessInterface, \yii\base\Configurable
{
/**
* @var bool whether to log detailed information on decision making based on rules.
*/
public $debug = YII_DEBUG;
/**
* @var string The ActiveRecord class for users.
*/
protected $userClass;
public function isAllowed(Authorizable $source, Authorizable $target, string $permission)
{
$logName = function(Authorizable $authorizable) {
return array_slice(explode('\\', $authorizable->getAuthName()), -1, 1)[0] . "({$authorizable->getId()})";
};
if ($this->debug) {
$message = str_repeat('>', $this->depth) . strtr("[source] requesting [permission] permission on [target]", [
'[permission]' => $permission,
'[target]' => $logName($target),
'[source]' => $logName($source)
]);
if ($this->depth > 0) {
\Yii::trace($message, 'abac');
} else {
\Yii::info($message, 'abac');
}
}
$result = parent::isAllowed($source, $target, $permission);
if ($this->debug) {
$message = str_repeat('>', $this->depth) . ($result ? 'Allowing' : 'Denying') . strtr(" [source] [permission] permission on [target]", [
'[permission]' => $permission,
'[target]' => $logName($target),
'[source]' => $logName($source)
]);
if ($this->depth > 0) {
\Yii::trace($message, 'abac');
} else {
\Yii::info($message, 'abac');
}
}
return $result;
}
protected function execute(Rule $rule, Authorizable $source, Authorizable $target, string $permission): bool
{
$result = parent::execute($rule, $source, $target, $permission);
if ($this->debug) {
$ruleName = get_class($rule);
$template = ($result ? 'TRUE:' : 'FALSE: ') . "You can [permission] the [target] if {$rule->getDescription()} ($ruleName)";
$message = strtr($template, [
'[permission]' => $permission,
'[target]' => "{$target->getAuthName()}({$target->getId()})",
]);
\Yii::trace($message, 'abac');
}
return $result;
}
public function __construct($config = [])
{
parent::__construct();
foreach($config as $key => $value) {
$this->$key = $value;
}
$this->init();
}
public function init()
{
if (!isset($this->userClass)) {
throw new \yii\base\InvalidConfigException("userClass must be configured.");
}
}
/**
* @inheritdoc
*/
protected function grantInternal(string $sourceName, string $sourceId, string $targetName, string $targetId, string $permission): void
{
try {
$perm = new Permission();
$perm->source_name = $sourceName;
$perm->source_id = $sourceId;
$perm->target_name = $targetName;
$perm->target_id = $targetId;
$perm->permission = $permission;
if (!$perm->save()) {
throw new \Exception("Failed to grant permission.");
}
} catch (\yii\db\Exception $e) {
throw $e;
throw new \Exception("Failed to grant permission.", $e);
}
}
/**
* @inheritdoc
*/
protected function revokeInternal(string $sourceName, string $sourceId, string $targetName, string $targetId, string $permission): void
{
Permission::deleteAll([
'source_name' => $sourceName,
'source_id' => $sourceId,
'target_name' => $targetName,
'target_id' => $targetId,
'permission' => $permission
]);
}
/**
* @return \ArrayObject
*/
protected function getEnvironment()
{
return new \ArrayObject([
'app' => \Yii::$app,
'identity' => \Yii::$app->user->identity
]);
}
/**
* @inheritdoc
*/
protected function isAllowedExplicit(string $sourceName, string $sourceId, string $targetName, string $targetId, string $permission): bool
{
return Permission::isAllowedById($sourceName, $sourceId, $targetName, $targetId, $permission);
}
protected function getUser($id)
{
if (\Yii::$app->user->id == $id) {
return \Yii::$app->user->identity;
}
return forward_static_call([$this->userClass, 'findOne'], $id);
}
public function checkAccess($userId, $permissionName, $params = [])
{
$source = $this->getUser($userId);
return $this->isAllowed($source, $params['target'] ?? new AuthorizableDummy(), $permissionName);
}
/**
* This function should return an array of associative arrays with grants.
* Each param maybe NULL, which implies "don't care".
* An empty string is not the same and must be matched exactly.
* @param string|null $sourceName
* @param string|null $sourceId
* @param string|null $targetName
* @param string|null $targetId
* @param string|null $permission
* @return array
*/
public function findExplicit(
string $sourceName = null,
string $sourceId = null,
string $targetName = null,
string $targetId = null,
string $permission = null
): array {
// We use array_filter to remove NULL, not empty strings.
return Permission::find()
->andWhere(array_filter([
'source_name' => $sourceName,
'source_id' => $sourceId,
'target_name' => $targetName,
'target_id' => $targetId,
'permission' => $permission
], function($e) { return $e !== null; }))
->all();
}
}