Skip to content

Commit

Permalink
Readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Pupinin authored Oct 24, 2019
1 parent a699e69 commit e4a2417
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# h-rbac

Based on native Laravel's 5 abilities. Hierarchical RBAC with callbacks.
Based on native Laravel's 5.\*/6.\* abilities. Hierarchical RBAC with callbacks.

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
Expand All @@ -18,17 +18,17 @@ In the process of creating my own projects I have formed an opinion about the mi

## Install

> Keep in mind it's only for Laravel 5.1 and later.
> Keep in mind it's only for Laravel 5.1 and later (6.\* also supported).
Via Composer

``` bash
$ composer require dlnsk/h-rbac
```

Add the service provider to `config/app.php`
Add the service provider to `config/app.php`. We use auto-discovering feature since Laravel 5.5, so you may don't touch `app.php`.

Dlnsk\HierarchicalRBAC\HRBACServiceProvider::class,
Dlnsk\HierarchicalRBAC\HRBACServiceProvider::class,

Publish some cool stuff:

Expand All @@ -38,7 +38,7 @@ Publish some cool stuff:

with

php artisan vendor:publish --provider="Dlnsk\HierarchicalRBAC\HRBACServiceProvider"
php artisan vendor:publish --provider="Dlnsk\HierarchicalRBAC\HRBACServiceProvider"

Add roles, permissions which you need and callbacks where it needs and have fun!

Expand All @@ -58,7 +58,7 @@ Very common situation is to allow user to change only his own posts. With this p

``` php
public function editOwnPost($user, $post) {
return $user->id === $post->user_id;
return $user->id === $post->user_id;
}
```

Expand Down Expand Up @@ -98,17 +98,17 @@ Storage of roles and permissions is on another level of logic, so DB support may
As I said `h-rbac` is wrapper for [authorization logic](https://laravel.com/docs/5.2/authorization#checking-abilities) of Laravel 5.1 and later. So, you can use any features of it.

```php
if (\Gate::allows('editPost', $post)) { // do something }
if (\Gate::allows('editPost', $post)) { /* do something */ }
...
if (\Gate::denies('editPost', $post)) { abort(403); }
...
if (\Gate::forUser($user)->allows('editPost', $post)) { // do something }
if (\Gate::forUser($user)->allows('editPost', $post)) { /* do something */ }
```

From User model:

```php
if ($request->user()->can('editPost', $post)) { // do something }
if ($request->user()->can('editPost', $post)) { /* do something */ }
...
if ($request->user()->cannot('editPost', $post)) { abort(403); }
```
Expand All @@ -121,18 +121,18 @@ $this->authorize('editPost', $post);

Within Blade

@can('editPost', $post)
<!-- The Current User Can Update The Post -->
@else
<!-- The Current User Can't Update The Post -->
@endcan
@can('editPost', $post)
<!-- The Current User Can Update The Post -->
@else
<!-- The Current User Can't Update The Post -->
@endcan

Also in `h-rbac` we add directive `@role` which you can combine with `@else`


@role('user|manager')
<!-- The current user has any role -->
@endrole
@role('user|manager')
<!-- The current user has any role -->
@endrole

## Configuration

Expand All @@ -147,40 +147,40 @@ use Dlnsk\HierarchicalRBAC\Authorization;

class AuthorizationClass extends Authorization
{
public function getPermissions() {
return [
'editPost' => [
'description' => 'Edit any posts', // optional property
'next' => 'editOwnPost', // used for making chain (hierarchy) of permissions
],
'editOwnPost' => [
'description' => 'Edit own post',
],
'deletePost' => [
'description' => 'Delete any posts',
],
];
}

public function getRoles() {
return [
'manager' => [
'editPost',
'deletePost',
],
'user' => [
'editOwnPost',
],
];
}

////////////// Callbacks ///////////////
public function editOwnPost($user, $post) {
$post = $this->getModel(\App\Post::class, $post); // helper method for geting model

return $user->id === $post->user_id;
}
public function getPermissions() {
return [
'editPost' => [
'description' => 'Edit any posts', // optional property
'next' => 'editOwnPost', // used for making chain (hierarchy) of permissions
],
'editOwnPost' => [
'description' => 'Edit own post',
],
'deletePost' => [
'description' => 'Delete any posts',
],
];
}

public function getRoles() {
return [
'manager' => [
'editPost',
'deletePost',
],
'user' => [
'editOwnPost',
],
];
}

////////////// Callbacks ///////////////

public function editOwnPost($user, $post) {
$post = $this->getModel(\App\Post::class, $post); // helper method for geting model

return $user->id === $post->user_id;
}

}
```
Expand Down

0 comments on commit e4a2417

Please sign in to comment.