Created
November 17, 2014 01:17
-
-
Save bcho/fa1e1e21c0bb4ea62461 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // user is a subject | |
| $user = UserService::getCurrentUser(); | |
| // role creation | |
| $role = RbacService::createRole('post', [ | |
| // Permission: | |
| // | |
| // -1: Deny | |
| // 1: Allow | |
| // 0: Inherit | |
| 'post.create' => 1, | |
| 'post.update' => 1, | |
| 'post.delete' => 1, | |
| 'post.view' => 1 | |
| ]); | |
| // RbacService::getRoleById(1); | |
| // RbacService::getRoleByName('admin'); | |
| $role->getName(); | |
| $role->hasPermission(); | |
| $role->addUser(UserService::getById(5)); | |
| $user->addRole($role); // $role->addUser($user); | |
| if ($user->hasPermission('post.create')) | |
| { | |
| // should check user's permission | |
| PostService::create([ | |
| 'title' => 'Awesome Post', | |
| 'content' => 'Hello, world!' | |
| ]); | |
| } | |
| // assertation | |
| $post = PostService::getById(1); | |
| $assert = function($user) use($post) { | |
| return $user->id === $post->author->id; | |
| }; | |
| if ($user->hasPermission('post.create', true, $assert)) | |
| { | |
| PostService::editById($post->id, [ | |
| 'title' => 'Awesome Updated Post' | |
| ]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment