|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Security; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Security\Exception\PermissionException; |
|
16
|
|
|
use Spiral\Security\Exception\RoleException; |
|
17
|
|
|
use Spiral\Security\PermissionManager; |
|
18
|
|
|
use Spiral\Security\Rule\AllowRule; |
|
19
|
|
|
use Spiral\Security\Rule\ForbidRule; |
|
20
|
|
|
use Spiral\Security\RuleInterface; |
|
21
|
|
|
use Spiral\Security\RulesInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class PermissionManagerTest |
|
25
|
|
|
* |
|
26
|
|
|
* @package Spiral\Tests\Security |
|
27
|
|
|
*/ |
|
28
|
|
|
class PermissionManagerTest extends TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
public const ROLE = 'test'; |
|
31
|
|
|
public const PERMISSION = 'permission'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var RulesInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $rules; |
|
37
|
|
|
|
|
38
|
|
|
public function setUp(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->rules = $this->createMock(RulesInterface::class); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testRoles(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$manager = new PermissionManager($this->rules); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertFalse($manager->hasRole(static::ROLE)); |
|
48
|
|
|
$this->assertEquals($manager, $manager->addRole(static::ROLE)); |
|
49
|
|
|
$this->assertTrue($manager->hasRole(static::ROLE)); |
|
50
|
|
|
$this->assertEquals($manager, $manager->removeRole(static::ROLE)); |
|
51
|
|
|
$this->assertFalse($manager->hasRole(static::ROLE)); |
|
52
|
|
|
|
|
53
|
|
|
$manager->addRole('one'); |
|
54
|
|
|
$manager->addRole('two'); |
|
55
|
|
|
$this->assertEquals(['one', 'two'], $manager->getRoles()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testAddRoleException(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$manager = new PermissionManager($this->rules); |
|
61
|
|
|
|
|
62
|
|
|
$this->expectException(RoleException::class); |
|
63
|
|
|
$manager->addRole(static::ROLE); |
|
64
|
|
|
$manager->addRole(static::ROLE); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testRemoveRoleException(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$manager = new PermissionManager($this->rules); |
|
70
|
|
|
|
|
71
|
|
|
$this->expectException(RoleException::class); |
|
72
|
|
|
$manager->removeRole(static::ROLE); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testAssociation(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$allowRule = new AllowRule(); |
|
78
|
|
|
$forbidRule = new ForbidRule(); |
|
79
|
|
|
|
|
80
|
|
|
$this->rules->method('has')->willReturn(true); |
|
|
|
|
|
|
81
|
|
|
$this->rules->method('get') |
|
82
|
|
|
->withConsecutive([AllowRule::class], [AllowRule::class], [ForbidRule::class]) |
|
83
|
|
|
->willReturn($allowRule, $allowRule, $forbidRule); |
|
84
|
|
|
|
|
85
|
|
|
$manager = new PermissionManager($this->rules); |
|
86
|
|
|
$manager->addRole(static::ROLE); |
|
87
|
|
|
|
|
88
|
|
|
// test simple permission |
|
89
|
|
|
$this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION, AllowRule::class)); |
|
90
|
|
|
$this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION)); |
|
91
|
|
|
|
|
92
|
|
|
// test pattern permission |
|
93
|
|
|
$this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION . '.*', AllowRule::class)); |
|
94
|
|
|
$this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION . '.' . static::PERMISSION)); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals($manager, $manager->deassociate(static::ROLE, static::PERMISSION)); |
|
97
|
|
|
$this->assertEquals($forbidRule, $manager->getRule(static::ROLE, static::PERMISSION)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testGetRuleRoleException(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$manager = new PermissionManager($this->rules); |
|
103
|
|
|
|
|
104
|
|
|
$this->expectException(RoleException::class); |
|
105
|
|
|
$manager->getRule(static::ROLE, static::PERMISSION); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testRulesForRoleException(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->rules->method('has')->willReturn(true); |
|
111
|
|
|
$manager = new PermissionManager($this->rules); |
|
112
|
|
|
|
|
113
|
|
|
$this->expectException(RoleException::class); |
|
114
|
|
|
$manager->getPermissions('admin'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testRulesForRole(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->rules->method('has')->willReturn(true); |
|
120
|
|
|
|
|
121
|
|
|
$manager = new PermissionManager($this->rules); |
|
122
|
|
|
|
|
123
|
|
|
$manager->addRole('admin'); |
|
124
|
|
|
$manager->associate('admin', 'post.edit', AllowRule::class); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertSame([ |
|
127
|
|
|
'post.edit' => AllowRule::class |
|
128
|
|
|
], $manager->getPermissions('admin')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testGetFallbackRule(): void |
|
132
|
|
|
{ |
|
133
|
|
|
$manager = new PermissionManager($this->rules); |
|
134
|
|
|
$manager->addRole(static::ROLE); |
|
135
|
|
|
|
|
136
|
|
|
$this->rules->method('get') |
|
137
|
|
|
->withConsecutive([ForbidRule::class]) |
|
138
|
|
|
->willReturn(new ForbidRule()); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertInstanceOf( |
|
141
|
|
|
ForbidRule::class, |
|
142
|
|
|
$manager->getRule(static::ROLE, static::PERMISSION) |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testAssociateRoleException(): void |
|
147
|
|
|
{ |
|
148
|
|
|
$manager = new PermissionManager($this->rules); |
|
149
|
|
|
|
|
150
|
|
|
$this->expectException(RoleException::class); |
|
151
|
|
|
$manager->associate(static::ROLE, static::PERMISSION); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testAssociatePermissionException(): void |
|
155
|
|
|
{ |
|
156
|
|
|
$this->expectException(PermissionException::class); |
|
157
|
|
|
|
|
158
|
|
|
$manager = new PermissionManager($this->rules); |
|
159
|
|
|
$manager->addRole(static::ROLE); |
|
160
|
|
|
$manager->associate(static::ROLE, static::PERMISSION); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..