forked from Kern046/rbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
160 lines (137 loc) · 4.17 KB
/
index.php
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
<?php
require_once('vendor/autoload.php');
use PhpRbac\Rbac;
$dsn = 'mysql:dbname=kilixrbactest;host=localhost';
$user = 'root';
$password = 'vagrant';
$DBConnection = new \PDO($dsn, $user, $password);
$rbac = Rbac::getInstance();
$rbac->init($DBConnection);
$rbac->reset(true);
$commonPerms = [
'intent-to-leave',
'referrals-public-view',
'referrals-teams-view',
];
$role1Perms = [
'access level-individual_aggregate'.
'risk-indicator',
'profile-blocks-1',
'profile-blocks-4',
'profile-blocks-n',
'profile-items-2',
'profile-items-n',
'survey-blocks-2',
'survey-blocks-4',
'survey-blocks-n',
'survey-items-2',
'survey-items-n',
'notes-public-view',
'notes-teams-view',
'contacts-public-view',
'contacts-teams-view',
'booking-public-view',
'booking-teams-view',
];
$role2Perms = [
'access-level_aggregate',
'profile-blocks-2',
'profile-blocks-4',
'profile-blocks-n',
'profile-items-2',
'profile-items-n',
'survey-blocks-2',
'survey-blocks-4',
'survey-blocks-n',
'survey-items-2',
'survey-items-n',
'notes-public-create',
'notes-teams-create',
'contacts-public-view',
'contacts-private-create',
'contacts-teams-view',
'booking-public-create',
'booking-teams-create',
'referrals-private-create',
];
$role3Perms = [
'access-level_individual_aggregate',
'risk-indicator',
'referrals-public-create',
'referrals-private-create',
'referrals-teams-create',
'receive-referrals',
];
$commonRoleId = $rbac->getRbacManager()->getRoleManager()->add('common_role', 'Common permissions for all users');
foreach ($commonPerms as $info) {
$permId = $rbac->getRbacManager()->getPermissionManager()->add($info, 'Default description', $commonRoleId);
$rbac->getRbacManager()->getRoleManager()->assign('common_role', $permId);
}
$role1Id = $rbac->getRbacManager()->getRoleManager()->add('role1', 'Role 1', $commonRoleId);
foreach ($role1Perms as $permName) {
$rbac->getRbacManager()->getPermissionManager()->add($permName, 'Default description', $role1Id);
$rbac->getRbacManager()->getRoleManager()->assign('role1', $permName);
}
$role2Id = $rbac->getRbacManager()->getRoleManager()->add('role2', 'Role 2', $commonRoleId);
foreach ($role2Perms as $permName) {
$rbac->getRbacManager()->getPermissionManager()->add($permName, 'Default description', $role2Id);
$rbac->getRbacManager()->getRoleManager()->assign('role2', $permName);
}
// Create User
$users = [
[
'id' => 1,
'username' => 'admin',
'roles' => [
'role1'
]
],
[
'id' => 2,
'username' => 'editor',
'roles' => [
'common_role',
'role2'
]
],
];
// Assign the proper roles.
foreach ($users as $user) {
if (!empty($user['roles']) && is_array($user['roles'])) {
foreach ($user['roles'] as $role) {
$rbac->getRbacManager()->getUserManager()->assign($role, $user['id']);
}
}
}
$tests = [
$users[0]['id'] => [
'intent to leave' => true,
'profile-blocks-1' => true,
'profile-blocks-2' => false,
'survey items-3' => false,
],
$users[1]['id'] => [
'intent to leave' => true,
'profile-blocks-1' => false,
'profile-blocks-n' => true,
'survey items-3' => false,
],
];
// Validate if a user has a particular permission.
foreach ($tests as $userId => $test) {
foreach ($test as $permission => $hasPermission) {
$permissionVerb = $hasPermission ? 'has' : 'doesn\'t have';
$noPermissionVerb = $hasPermission ? 'doesn\'t have' : 'has';
try {
if ($rbac->check($permission, $userId) === $hasPermission) {
echo "Success: User {$userId} $permissionVerb the '{$permission}' permission.\n";
} else {
echo "Failure! User {$userId} $noPermissionVerb the '{$permission}' permission.\n";
}
}
catch(Exception $e) {
$status = $hasPermission ? 'Failure' : 'Success';
echo "$status! '{$permission}' is an unknown/invalid permission.\n";
}
}
}