|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Security\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Security\Permission; |
|
6
|
|
|
use SilverStripe\Security\Member; |
|
7
|
|
|
use SilverStripe\Security\PermissionCheckboxSetField; |
|
8
|
|
|
use SilverStripe\Core\Config\Config; |
|
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @skipUpgrade |
|
13
|
|
|
*/ |
|
14
|
|
|
class PermissionTest extends SapphireTest |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
protected static $fixture_file = 'PermissionTest.yml'; |
|
18
|
|
|
|
|
19
|
|
|
public function testGetCodesGrouped() |
|
20
|
|
|
{ |
|
21
|
|
|
$codes = Permission::get_codes(); |
|
22
|
|
|
$this->assertArrayNotHasKey('SITETREE_VIEW_ALL', $codes); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testGetCodesUngrouped() |
|
26
|
|
|
{ |
|
27
|
|
|
$codes = Permission::get_codes(false); |
|
28
|
|
|
$this->assertArrayHasKey('SITETREE_VIEW_ALL', $codes); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testDirectlyAppliedPermissions() |
|
32
|
|
|
{ |
|
33
|
|
|
$member = $this->objFromFixture(Member::class, 'author'); |
|
34
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL")); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testCMSAccess() |
|
38
|
|
|
{ |
|
39
|
|
|
$members = Member::get()->byIDs($this->allFixtureIDs(Member::class)); |
|
40
|
|
|
foreach ($members as $member) { |
|
41
|
|
|
$this->assertTrue(Permission::checkMember($member, 'CMS_ACCESS')); |
|
42
|
|
|
$this->assertTrue(Permission::checkMember($member, ['CMS_ACCESS', 'CMS_ACCESS_Security'])); |
|
43
|
|
|
$this->assertTrue(Permission::checkMember($member, ['CMS_ACCESS_Security', 'CMS_ACCESS'])); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$member = new Member(); |
|
47
|
|
|
$member->update( |
|
48
|
|
|
[ |
|
49
|
|
|
'FirstName' => 'No CMS', |
|
50
|
|
|
'Surname' => 'Access', |
|
51
|
|
|
'Email' => '[email protected]', |
|
52
|
|
|
] |
|
53
|
|
|
); |
|
54
|
|
|
$member->write(); |
|
55
|
|
|
$this->assertFalse(Permission::checkMember($member, 'CMS_ACCESS')); |
|
56
|
|
|
$this->assertFalse(Permission::checkMember($member, ['CMS_ACCESS', 'CMS_ACCESS_Security'])); |
|
57
|
|
|
$this->assertFalse(Permission::checkMember($member, ['CMS_ACCESS_Security', 'CMS_ACCESS'])); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testLeftAndMainAccessAll() |
|
61
|
|
|
{ |
|
62
|
|
|
//add user and group |
|
63
|
|
|
$member = $this->objFromFixture(Member::class, 'leftandmain'); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
|
66
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
|
67
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testPermissionAreInheritedFromOneRole() |
|
71
|
|
|
{ |
|
72
|
|
|
$member = $this->objFromFixture(Member::class, 'author'); |
|
73
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
|
74
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
|
75
|
|
|
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testPermissionAreInheritedFromMultipleRoles() |
|
79
|
|
|
{ |
|
80
|
|
|
$member = $this->objFromFixture(Member::class, 'access'); |
|
81
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
|
82
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
|
83
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
|
84
|
|
|
$this->assertTrue(Permission::checkMember($member, "EDIT_PERMISSIONS")); |
|
85
|
|
|
$this->assertFalse(Permission::checkMember($member, "SITETREE_VIEW_ALL")); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testPermissionsForMember() |
|
89
|
|
|
{ |
|
90
|
|
|
$member = $this->objFromFixture(Member::class, 'access'); |
|
91
|
|
|
$permissions = Permission::permissions_for_member($member->ID); |
|
92
|
|
|
$this->assertEquals(4, count($permissions ?? [])); |
|
93
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_MyAdmin', $permissions ?? [])); |
|
94
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_AssetAdmin', $permissions ?? [])); |
|
95
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_SecurityAdmin', $permissions ?? [])); |
|
96
|
|
|
$this->assertTrue(in_array('EDIT_PERMISSIONS', $permissions ?? [])); |
|
97
|
|
|
|
|
98
|
|
|
$group = $this->objFromFixture("SilverStripe\\Security\\Group", "access"); |
|
99
|
|
|
|
|
100
|
|
|
Permission::deny($group->ID, "CMS_ACCESS_MyAdmin"); |
|
101
|
|
|
$permissions = Permission::permissions_for_member($member->ID); |
|
102
|
|
|
$this->assertEquals(3, count($permissions ?? [])); |
|
103
|
|
|
$this->assertFalse(in_array('CMS_ACCESS_MyAdmin', $permissions ?? [])); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testRolesAndPermissionsFromParentGroupsAreInherited() |
|
107
|
|
|
{ |
|
108
|
|
|
$member = $this->objFromFixture(Member::class, 'globalauthor'); |
|
109
|
|
|
|
|
110
|
|
|
// Check that permissions applied to the group are there |
|
111
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_EDIT_ALL")); |
|
112
|
|
|
|
|
113
|
|
|
// Check that roles from parent groups are there |
|
114
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
|
115
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
|
116
|
|
|
|
|
117
|
|
|
// Check that permissions from parent groups are there |
|
118
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL")); |
|
119
|
|
|
|
|
120
|
|
|
// Check that a random permission that shouldn't be there isn't |
|
121
|
|
|
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
|
122
|
|
|
} |
|
123
|
|
|
/** |
|
124
|
|
|
* Ensure the the get_*_by_permission functions are permission role aware |
|
125
|
|
|
*/ |
|
126
|
|
|
public function testGettingMembersByPermission() |
|
127
|
|
|
{ |
|
128
|
|
|
$accessMember = $this->objFromFixture(Member::class, 'access'); |
|
129
|
|
|
$accessAuthor = $this->objFromFixture(Member::class, 'author'); |
|
130
|
|
|
|
|
131
|
|
|
$result = Permission::get_members_by_permission(['CMS_ACCESS_SecurityAdmin']); |
|
132
|
|
|
$resultIDs = $result ? $result->column() : []; |
|
133
|
|
|
|
|
134
|
|
|
$this->assertContains( |
|
135
|
|
|
$accessMember->ID, |
|
136
|
|
|
$resultIDs, |
|
137
|
|
|
'Member is found via a permission attached to a role' |
|
138
|
|
|
); |
|
139
|
|
|
$this->assertNotContains($accessAuthor->ID, $resultIDs); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
public function testHiddenPermissions() |
|
144
|
|
|
{ |
|
145
|
|
|
$permissionCheckboxSet = new PermissionCheckboxSetField('Permissions', 'Permissions', Permission::class, 'GroupID'); |
|
146
|
|
|
$this->assertStringContainsString('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field()); |
|
147
|
|
|
|
|
148
|
|
|
Config::modify()->merge(Permission::class, 'hidden_permissions', ['CMS_ACCESS_LeftAndMain']); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertStringNotContainsString('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field()); |
|
151
|
|
|
|
|
152
|
|
|
Config::inst()->remove(Permission::class, 'hidden_permissions'); |
|
|
|
|
|
|
153
|
|
|
$this->assertStringContainsString('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testEmptyMemberFails() |
|
157
|
|
|
{ |
|
158
|
|
|
$member = new Member(); |
|
159
|
|
|
$this->assertFalse($member->exists()); |
|
160
|
|
|
|
|
161
|
|
|
$this->logInWithPermission('ADMIN'); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertFalse(Permission::checkMember($member, 'ADMIN')); |
|
164
|
|
|
$this->assertFalse(Permission::checkMember($member, 'CMS_ACCESS_LeftAndMain')); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testGrantPermission() |
|
168
|
|
|
{ |
|
169
|
|
|
$id = rand(15, 20); |
|
170
|
|
|
|
|
171
|
|
|
Permission::grant($id, 'CMS_ACCESS_CMSMain'); |
|
172
|
|
|
Permission::grant($id, 'CMS_ACCESS_AssetAdmin'); |
|
173
|
|
|
Permission::grant($id, 'CMS_ACCESS_ReportAdmin'); |
|
174
|
|
|
|
|
175
|
|
|
$groupPermission = Permission::get()->filter(['GroupID' => $id]); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertEquals(3, $groupPermission->count()); |
|
178
|
|
|
$this->assertEquals(0, $groupPermission->first()->Arg); |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
Permission::grant($id, 'CMS_ACCESS_CMSMain', 'all'); |
|
182
|
|
|
Permission::grant($id, 'CMS_ACCESS_AssetAdmin', 'all'); |
|
183
|
|
|
Permission::grant($id, 'CMS_ACCESS_ReportAdmin', 'all'); |
|
184
|
|
|
|
|
185
|
|
|
$groupPermission = Permission::get()->filter(['GroupID' => $id]); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertEquals(3, $groupPermission->count()); |
|
188
|
|
|
$this->assertEquals(-1, $groupPermission->first()->Arg); |
|
189
|
|
|
|
|
190
|
|
|
Permission::grant($id, 'CMS_ACCESS_CMSMain', 'any'); |
|
191
|
|
|
Permission::grant($id, 'CMS_ACCESS_AssetAdmin', 'any'); |
|
192
|
|
|
Permission::grant($id, 'CMS_ACCESS_ReportAdmin', 'any'); |
|
193
|
|
|
|
|
194
|
|
|
$groupPermission = Permission::get()->filter(['GroupID' => $id]); |
|
195
|
|
|
|
|
196
|
|
|
$this->assertEquals(3, $groupPermission->count()); |
|
197
|
|
|
$this->assertEquals(-1, $groupPermission->first()->Arg); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|