|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Exceptions\AccessDeniedException; |
|
6
|
|
|
use Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Access |
|
10
|
|
|
*/ |
|
11
|
|
|
class CrudPanelAccessTest extends BaseCrudPanel |
|
12
|
|
|
{ |
|
13
|
|
|
private $unknownPermission = 'unknownPermission'; |
|
14
|
|
|
|
|
15
|
|
|
private $defaultAccessList = []; |
|
16
|
|
|
|
|
17
|
|
|
private $fullAccessList = [ |
|
18
|
|
|
'list', |
|
19
|
|
|
'create', |
|
20
|
|
|
'update', |
|
21
|
|
|
'delete', |
|
22
|
|
|
'bulkDelete', |
|
23
|
|
|
'revisions', |
|
24
|
|
|
'reorder', |
|
25
|
|
|
'show', |
|
26
|
|
|
'clone', |
|
27
|
|
|
'bulkClone', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
public function testHasAccess() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->crudPanel->allowAccess('list'); |
|
33
|
|
|
$this->assertTrue($this->crudPanel->hasAccess('list')); |
|
34
|
|
|
$this->assertFalse($this->crudPanel->hasAccess('create')); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testAllowAccess() |
|
38
|
|
|
{ |
|
39
|
|
|
$permission = 'reorder'; |
|
40
|
|
|
|
|
41
|
|
|
$this->crudPanel->allowAccess($permission); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertTrue($this->crudPanel->hasAccess($permission)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testAllowAccessToUnknownPermission() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->crudPanel->allowAccess($this->unknownPermission); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertTrue($this->crudPanel->hasAccess($this->unknownPermission)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testDenyAccess() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->crudPanel->denyAccess('delete'); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertFalse($this->crudPanel->hasAccess('delete')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testDenyAccessToUnknownPermission() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->crudPanel->denyAccess($this->unknownPermission); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertFalse($this->crudPanel->hasAccess($this->unknownPermission)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testHasAccessToAny() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->crudPanel->allowAccess('create'); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertTrue($this->crudPanel->hasAccessToAny($this->fullAccessList)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testHasAccessToAnyDenied() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->assertFalse($this->crudPanel->hasAccessToAny(array_diff($this->fullAccessList, $this->defaultAccessList))); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testHasAccessToAll() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->crudPanel->allowAccess($this->fullAccessList); |
|
82
|
|
|
$this->assertTrue($this->crudPanel->hasAccessToAll($this->fullAccessList)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testHasAccessToAllDenied() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->assertFalse($this->crudPanel->hasAccessToAll($this->fullAccessList)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testHasAccessOrFail() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->crudPanel->allowAccess($this->fullAccessList); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($this->fullAccessList as $permission) { |
|
95
|
|
|
$this->assertTrue($this->crudPanel->hasAccessOrFail($permission)); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testHasAccessOrFailDenied() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->expectException(AccessDeniedException::class); |
|
102
|
|
|
|
|
103
|
|
|
$this->crudPanel->hasAccessOrFail($this->unknownPermission); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testItCanUseAClosureToResolveAccess() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->crudPanel->setAccessCondition('list', function() { |
|
109
|
|
|
return true; |
|
110
|
|
|
}); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertTrue($this->crudPanel->getAccessCondition('list') instanceof \Closure); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertTrue($this->crudPanel->hasAccess('list')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testItCanUseAClosureToResolveAccessForMultipleOperations() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->crudPanel->setAccessCondition(['list','create'], function() { |
|
120
|
|
|
return true; |
|
121
|
|
|
}); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertTrue($this->crudPanel->getAccessCondition('list') instanceof \Closure); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertTrue($this->crudPanel->hasAccess('list')); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testItCanCheckIfAnOperationHasAccessConditions() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->crudPanel->setAccessCondition(['list','create'], function() { |
|
131
|
|
|
return true; |
|
132
|
|
|
}); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertTrue($this->crudPanel->hasAccessCondition('list')); |
|
135
|
|
|
$this->assertFalse($this->crudPanel->hasAccessCondition('delete')); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testItCanCheckAccessToAll() |
|
139
|
|
|
{ |
|
140
|
|
|
$this->crudPanel->allowAccess(['list', 'create'], function() { |
|
|
|
|
|
|
141
|
|
|
return true; |
|
142
|
|
|
}); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertTrue($this->crudPanel->hasAccessToAll(['list', 'create'])); |
|
145
|
|
|
$this->assertFalse($this->crudPanel->hasAccessToAll(['list', 'create', 'delete'])); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function testItCanAllowAccessToSomeSpecificOperationWhileDenyingOthers() |
|
149
|
|
|
{ |
|
150
|
|
|
$this->crudPanel->allowAccess(['list', 'create'], function() { |
|
|
|
|
|
|
151
|
|
|
return true; |
|
152
|
|
|
}); |
|
153
|
|
|
|
|
154
|
|
|
$this->assertTrue($this->crudPanel->hasAccessToAll(['list', 'create'])); |
|
155
|
|
|
|
|
156
|
|
|
$this->crudPanel->allowAccessOnlyTo('list'); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertTrue($this->crudPanel->hasAccess('list')); |
|
159
|
|
|
$this->assertFalse($this->crudPanel->hasAccess('create')); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.