forked from nette/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermission.CMSExample.phpt
More file actions
172 lines (133 loc) · 7.19 KB
/
Copy pathPermission.CMSExample.phpt
File metadata and controls
172 lines (133 loc) · 7.19 KB
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
<?php
/**
* Test: Nette\Security\Permission Ensures that an example for a content management system is operable.
*/
declare(strict_types=1);
use Nette\Security\Permission;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
$acl = new Permission;
$acl->addRole('guest');
$acl->addRole('staff', 'guest'); // staff inherits permissions from guest
$acl->addRole('editor', 'staff'); // editor inherits permissions from staff
$acl->addRole('administrator');
// Guest may only view content
$acl->allow('guest', null, 'view');
// Staff inherits view privilege from guest, but also needs additional privileges
$acl->allow('staff', null, ['edit', 'submit', 'revise']);
// Editor inherits view, edit, submit, and revise privileges, but also needs additional privileges
$acl->allow('editor', null, ['publish', 'archive', 'delete']);
// Administrator inherits nothing but is allowed all privileges
$acl->allow('administrator');
// Access control checks based on above permission sets
Assert::true($acl->isAllowed('guest', null, 'view'));
Assert::false($acl->isAllowed('guest', null, 'edit'));
Assert::false($acl->isAllowed('guest', null, 'submit'));
Assert::false($acl->isAllowed('guest', null, 'revise'));
Assert::false($acl->isAllowed('guest', null, 'publish'));
Assert::false($acl->isAllowed('guest', null, 'archive'));
Assert::false($acl->isAllowed('guest', null, 'delete'));
Assert::false($acl->isAllowed('guest', null, 'unknown'));
Assert::false($acl->isAllowed('guest'));
Assert::true($acl->isAllowed('staff', null, 'view'));
Assert::true($acl->isAllowed('staff', null, 'edit'));
Assert::true($acl->isAllowed('staff', null, 'submit'));
Assert::true($acl->isAllowed('staff', null, 'revise'));
Assert::false($acl->isAllowed('staff', null, 'publish'));
Assert::false($acl->isAllowed('staff', null, 'archive'));
Assert::false($acl->isAllowed('staff', null, 'delete'));
Assert::false($acl->isAllowed('staff', null, 'unknown'));
Assert::false($acl->isAllowed('staff'));
Assert::true($acl->isAllowed('editor', null, 'view'));
Assert::true($acl->isAllowed('editor', null, 'edit'));
Assert::true($acl->isAllowed('editor', null, 'submit'));
Assert::true($acl->isAllowed('editor', null, 'revise'));
Assert::true($acl->isAllowed('editor', null, 'publish'));
Assert::true($acl->isAllowed('editor', null, 'archive'));
Assert::true($acl->isAllowed('editor', null, 'delete'));
Assert::false($acl->isAllowed('editor', null, 'unknown'));
Assert::false($acl->isAllowed('editor'));
Assert::true($acl->isAllowed('administrator', null, 'view'));
Assert::true($acl->isAllowed('administrator', null, 'edit'));
Assert::true($acl->isAllowed('administrator', null, 'submit'));
Assert::true($acl->isAllowed('administrator', null, 'revise'));
Assert::true($acl->isAllowed('administrator', null, 'publish'));
Assert::true($acl->isAllowed('administrator', null, 'archive'));
Assert::true($acl->isAllowed('administrator', null, 'delete'));
Assert::true($acl->isAllowed('administrator', null, 'unknown'));
Assert::true($acl->isAllowed('administrator'));
// Some checks on specific areas, which inherit access controls from the root ACL node
$acl->addResource('newsletter');
$acl->addResource('pending', 'newsletter');
$acl->addResource('gallery');
$acl->addResource('profiles', 'gallery');
$acl->addResource('config');
$acl->addResource('hosts', 'config');
Assert::true($acl->isAllowed('guest', 'pending', 'view'));
Assert::true($acl->isAllowed('staff', 'profiles', 'revise'));
Assert::true($acl->isAllowed('staff', 'pending', 'view'));
Assert::true($acl->isAllowed('staff', 'pending', 'edit'));
Assert::false($acl->isAllowed('staff', 'pending', 'publish'));
Assert::false($acl->isAllowed('staff', 'pending'));
Assert::false($acl->isAllowed('editor', 'hosts', 'unknown'));
Assert::true($acl->isAllowed('administrator', 'pending'));
// Add a new group, marketing, which bases its permissions on staff
$acl->addRole('marketing', 'staff');
// Refine the privilege sets for more specific needs
// Allow marketing to publish and archive newsletters
$acl->allow('marketing', 'newsletter', ['publish', 'archive']);
// Allow marketing to publish and archive latest news
$acl->addResource('news');
$acl->addResource('latest', 'news');
$acl->allow('marketing', 'latest', ['publish', 'archive']);
// Deny staff (and marketing, by inheritance) rights to revise latest news
$acl->deny('staff', 'latest', 'revise');
// Deny everyone access to archive news announcements
$acl->addResource('announcement', 'news');
$acl->deny(null, 'announcement', 'archive');
// Access control checks for the above refined permission sets
Assert::true($acl->isAllowed('marketing', null, 'view'));
Assert::true($acl->isAllowed('marketing', null, 'edit'));
Assert::true($acl->isAllowed('marketing', null, 'submit'));
Assert::true($acl->isAllowed('marketing', null, 'revise'));
Assert::false($acl->isAllowed('marketing', null, 'publish'));
Assert::false($acl->isAllowed('marketing', null, 'archive'));
Assert::false($acl->isAllowed('marketing', null, 'delete'));
Assert::false($acl->isAllowed('marketing', null, 'unknown'));
Assert::false($acl->isAllowed('marketing'));
Assert::true($acl->isAllowed('marketing', 'newsletter', 'publish'));
Assert::false($acl->isAllowed('staff', 'pending', 'publish'));
Assert::true($acl->isAllowed('marketing', 'pending', 'publish'));
Assert::true($acl->isAllowed('marketing', 'newsletter', 'archive'));
Assert::false($acl->isAllowed('marketing', 'newsletter', 'delete'));
Assert::false($acl->isAllowed('marketing', 'newsletter'));
Assert::true($acl->isAllowed('marketing', 'latest', 'publish'));
Assert::true($acl->isAllowed('marketing', 'latest', 'archive'));
Assert::false($acl->isAllowed('marketing', 'latest', 'delete'));
Assert::false($acl->isAllowed('marketing', 'latest', 'revise'));
Assert::false($acl->isAllowed('marketing', 'latest'));
Assert::false($acl->isAllowed('marketing', 'announcement', 'archive'));
Assert::false($acl->isAllowed('staff', 'announcement', 'archive'));
Assert::false($acl->isAllowed('administrator', 'announcement', 'archive'));
Assert::false($acl->isAllowed('staff', 'latest', 'publish'));
Assert::false($acl->isAllowed('editor', 'announcement', 'archive'));
// Remove some previous permission specifications
// Marketing can no longer publish and archive newsletters
$acl->removeAllow('marketing', 'newsletter', ['publish', 'archive']);
// Marketing can no longer archive the latest news
$acl->removeAllow('marketing', 'latest', 'archive');
// Now staff (and marketing, by inheritance) may revise latest news
$acl->removeDeny('staff', 'latest', 'revise');
// Access control checks for the above refinements
Assert::false($acl->isAllowed('marketing', 'newsletter', 'publish'));
Assert::false($acl->isAllowed('marketing', 'newsletter', 'archive'));
Assert::false($acl->isAllowed('marketing', 'latest', 'archive'));
Assert::true($acl->isAllowed('staff', 'latest', 'revise'));
Assert::true($acl->isAllowed('marketing', 'latest', 'revise'));
// Grant marketing all permissions on the latest news
$acl->allow('marketing', 'latest');
// Access control checks for the above refinement
Assert::true($acl->isAllowed('marketing', 'latest', 'archive'));
Assert::true($acl->isAllowed('marketing', 'latest', 'publish'));
Assert::true($acl->isAllowed('marketing', 'latest', 'edit'));
Assert::true($acl->isAllowed('marketing', 'latest'));