Skip to content

Instantly share code, notes, and snippets.

@bcho
Created November 17, 2014 01:17
Show Gist options
  • Select an option

  • Save bcho/fa1e1e21c0bb4ea62461 to your computer and use it in GitHub Desktop.

Select an option

Save bcho/fa1e1e21c0bb4ea62461 to your computer and use it in GitHub Desktop.
<?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