|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Authorization; |
|
6
|
|
|
|
|
7
|
|
|
use Casbin\Enforcer; |
|
8
|
|
|
use Casbin\Exceptions\CasbinException; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class IntegrationTest |
|
13
|
|
|
* These test mainly exist to avoid surprises during vendor upgrades |
|
14
|
|
|
* |
|
15
|
|
|
* @package AbterPhp\Framework\Authorization |
|
16
|
|
|
*/ |
|
17
|
|
|
class CasbinTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Example from https://github.com/php-casbin/casbin-tutorials/blob/master/tutorials/Get-Started.md |
|
21
|
|
|
* |
|
22
|
|
|
* @throws CasbinException |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testAcl(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$enforcer = new Enforcer(__DIR__ . "/fixtures/acl_model.conf", __DIR__ . "/fixtures/acl_policy.csv"); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertTrue($enforcer->enforce('alice', 'data1', 'read')); |
|
29
|
|
|
$this->assertFalse($enforcer->enforce('alice', 'data2', 'write')); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertFalse($enforcer->enforce('bob', 'data1', 'read')); |
|
32
|
|
|
$this->assertTrue($enforcer->enforce('bob', 'data2', 'write')); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Example from https://github.com/php-casbin/casbin-tutorials/blob/master/tutorials/ABAC-with-Casbin.md |
|
37
|
|
|
* |
|
38
|
|
|
* @throws CasbinException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testAbac(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$enforcer = new Enforcer(__DIR__ . "/fixtures/abac_model.conf"); |
|
43
|
|
|
|
|
44
|
|
|
$data1 = new \stdClass(); |
|
45
|
|
|
$data1->name = 'data1'; |
|
46
|
|
|
$data1->owner = 'alice'; |
|
47
|
|
|
|
|
48
|
|
|
$data2 = new \stdClass(); |
|
49
|
|
|
$data2->name = 'data2'; |
|
50
|
|
|
$data2->owner = 'bob'; |
|
51
|
|
|
|
|
52
|
|
|
$this->assertTrue($enforcer->enforce('alice', $data1, 'read')); |
|
53
|
|
|
$this->assertFalse($enforcer->enforce('alice', $data2, 'write')); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertFalse($enforcer->enforce('bob', $data1, 'read')); |
|
56
|
|
|
$this->assertTrue($enforcer->enforce('bob', $data2, 'write')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Example from https://github.com/php-casbin/casbin-tutorials/blob/master/tutorials/RBAC-with-Casbin.md |
|
61
|
|
|
* |
|
62
|
|
|
* @throws CasbinException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testRbac(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$enforcer = new Enforcer(__DIR__ . "/fixtures/rbac_model.conf"); |
|
67
|
|
|
|
|
68
|
|
|
// alice has the admin role |
|
69
|
|
|
$enforcer->addRoleForUser('alice', 'admin'); |
|
70
|
|
|
// bob has the member role |
|
71
|
|
|
$enforcer->addRoleForUser('bob', 'member'); |
|
72
|
|
|
|
|
73
|
|
|
$enforcer->addPermissionForUser('member', '/foo', 'GET'); |
|
74
|
|
|
$enforcer->addPermissionForUser('member', '/foo/:id', 'GET'); |
|
75
|
|
|
|
|
76
|
|
|
// admin inherits all permissions of member |
|
77
|
|
|
$enforcer->addRoleForUser('admin', 'member'); |
|
78
|
|
|
|
|
79
|
|
|
$enforcer->addPermissionForUser('admin', '/foo', 'POST'); |
|
80
|
|
|
$enforcer->addPermissionForUser('admin', '/foo/:id', 'PUT'); |
|
81
|
|
|
$enforcer->addPermissionForUser('admin', '/foo/:id', 'DELETE'); |
|
82
|
|
|
|
|
83
|
|
|
$data1 = new \stdClass(); |
|
84
|
|
|
$data1->name = 'data1'; |
|
85
|
|
|
$data1->owner = 'alice'; |
|
86
|
|
|
|
|
87
|
|
|
$data2 = new \stdClass(); |
|
88
|
|
|
$data2->name = 'data2'; |
|
89
|
|
|
$data2->owner = 'bob'; |
|
90
|
|
|
|
|
91
|
|
|
// Alice is an admin, so she can do everything that was already set up |
|
92
|
|
|
$this->assertTrue($enforcer->enforce('alice', '/foo', 'GET')); |
|
93
|
|
|
$this->assertTrue($enforcer->enforce('alice', '/foo', 'GET')); |
|
94
|
|
|
$this->assertTrue($enforcer->enforce('alice', '/foo', 'POST')); |
|
95
|
|
|
$this->assertTrue($enforcer->enforce('alice', '/foo/1', 'PUT')); |
|
96
|
|
|
$this->assertTrue($enforcer->enforce('alice', '/foo/1', 'DELETE')); |
|
97
|
|
|
|
|
98
|
|
|
// Stuff that has not been set up, Alice will still not be allowed to do |
|
99
|
|
|
$this->assertFalse($enforcer->enforce('alice', '/foo/1', 'PATCH')); |
|
100
|
|
|
$this->assertFalse($enforcer->enforce('alice', '/bar', 'GET')); |
|
101
|
|
|
|
|
102
|
|
|
// Bob is just a member, so he can not do everything that was already set up |
|
103
|
|
|
$this->assertTrue($enforcer->enforce('bob', '/foo', 'GET')); |
|
104
|
|
|
$this->assertTrue($enforcer->enforce('bob', '/foo', 'GET')); |
|
105
|
|
|
$this->assertFalse($enforcer->enforce('bob', '/foo', 'POST')); |
|
106
|
|
|
$this->assertFalse($enforcer->enforce('bob', '/foo/1', 'PUT')); |
|
107
|
|
|
$this->assertFalse($enforcer->enforce('bob', '/foo/1', 'DELETE')); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Example from https://casbin.org/docs/en/rbac-with-domains |
|
112
|
|
|
* |
|
113
|
|
|
* @throws CasbinException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testRbacWithDomains(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$enforcer = new Enforcer( |
|
118
|
|
|
__DIR__ . "/fixtures/rbac_with_domains_model.conf", |
|
119
|
|
|
__DIR__ . "/fixtures/rbac_with_domains_policy.csv", |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
// Alice is an Admin in domain1 and a user in domain2 |
|
123
|
|
|
$this->assertTrue($enforcer->enforce('alice', 'domain1', 'data1', 'read')); |
|
124
|
|
|
$this->assertTrue($enforcer->enforce('alice', 'domain1', 'data1', 'write')); |
|
125
|
|
|
$this->assertTrue($enforcer->enforce('alice', 'domain2', 'data2', 'read')); |
|
126
|
|
|
$this->assertFalse($enforcer->enforce('alice', 'domain2', 'data2', 'write')); |
|
127
|
|
|
|
|
128
|
|
|
// Bob is an Admin in domain2 only |
|
129
|
|
|
$this->assertFalse($enforcer->enforce('bob', 'domain1', 'data1', 'read')); |
|
130
|
|
|
$this->assertFalse($enforcer->enforce('bob', 'domain1', 'data1', 'write')); |
|
131
|
|
|
$this->assertTrue($enforcer->enforce('bob', 'domain2', 'data2', 'read')); |
|
132
|
|
|
$this->assertTrue($enforcer->enforce('bob', 'domain2', 'data2', 'read')); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Example from https://casbin.org/docs/en/priority-model |
|
137
|
|
|
* |
|
138
|
|
|
* @throws CasbinException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testPriority(): void |
|
141
|
|
|
{ |
|
142
|
|
|
$enforcer = new Enforcer( |
|
143
|
|
|
__DIR__ . "/fixtures/priority_model.conf", |
|
144
|
|
|
__DIR__ . "/fixtures/priority_policy.csv", |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
// Alice is allowed access, but is in data1_deny_group, which has higher priority |
|
148
|
|
|
$this->assertFalse($enforcer->enforce('alice', 'data1', 'read')); |
|
149
|
|
|
$this->assertFalse($enforcer->enforce('alice', 'data1', 'write')); |
|
150
|
|
|
$this->assertFalse($enforcer->enforce('alice', 'data2', 'read')); |
|
151
|
|
|
$this->assertFalse($enforcer->enforce('alice', 'data2', 'write')); |
|
152
|
|
|
// Bob is denied access, but is in data2_allow_group, which has higher priority |
|
153
|
|
|
$this->assertTrue($enforcer->enforce('bob', 'data2', 'read')); |
|
154
|
|
|
$this->assertTrue($enforcer->enforce('bob', 'data2', 'write')); |
|
155
|
|
|
$this->assertFalse($enforcer->enforce('bob', 'data1', 'read')); |
|
156
|
|
|
$this->assertFalse($enforcer->enforce('bob', 'data1', 'write')); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|