-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.php
75 lines (57 loc) · 1.94 KB
/
run.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
<?php
/**
* This php script demonstrates how you can:
* 1) Create roles
* 2) Create permissions.
* 3) Assign roles to users.
* 4) Build roles tree.
*/
use Potievdev\SlimRbac\Component\AuthManager;
use Potievdev\SlimRbac\Models\Entity\Permission;
use Potievdev\SlimRbac\Models\Entity\Role;
use Potievdev\SlimRbac\Structure\AuthOptions;
// Autoload
require_once 'vendor/autoload.php';
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = require 'config/sr-config.php';
/**
* @var AuthOptions $authOptions
* It will pass as argument for AuthManager constructor
*/
$authOptions = new AuthOptions();
// Setting entity manager instance
$authOptions->setEntityManager($entityManager);
// Initialize AuthManager instance
$authManager = new AuthManager($authOptions);
// Clear all roles, permissions, role assigns, role tree
$authManager->removeAll();
// Creating /edit permission
$edit = new Permission();
$edit->setName('/edit');
$authManager->addPermission($edit);
// Creating /write permission
$write = new Permission();
$write->setName('/write');
$authManager->addPermission($write);
// Creating moderator role
$moderator = new Role();
$moderator->setName('moderator');
$authManager->addRole($moderator);
// Creating admin role
$admin = new Role();
$admin->setName('admin');
$authManager->addRole($admin);
// Adding /edit permission to moderator role
$authManager->addChildPermission($moderator, $edit);
// Adding /write permission to admin role
$authManager->addChildPermission($admin, $write);
// Adding moderator role to admin as child. After this admin role obtain all moderator permissions
$authManager->addChildRole($admin, $moderator);
// Demo moderator user identifier
$moderatorUserId = 1;
// Demo admin user identifier
$adminUserId = 2;
// Assigning moderator role to user (user.id = 1)
$authManager->assign($moderator, $moderatorUserId);
// Assigning admin role to user (user.id = 1)
$authManager->assign($admin, $adminUserId);