Skip to content

Commit

Permalink
chore: restore repository
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfensdrfer committed Nov 15, 2021
0 parents commit 567782c
Show file tree
Hide file tree
Showing 13 changed files with 594 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/

composer.lock
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 PHPZen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

236 changes: 236 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
# Laravel RBAC
Super simple RBAC/ACL implementation for Laravel 5. Laravel >=5.4 compatible fork of https://github.com/keepanitreel/laravel-rbac.

## Installation
Require this package with composer ([Packagist](https://packagist.org/packages/visualappeal/laravel-rbac)) using the following command

```
composer require visualappeal/laravel-rbac
```

or modify your `composer.json`

```
"require": {
...
"visualappeal/laravel-rbac": "^0.7"
}
```

then run `composer update`.

After installation register the ServiceProvider to the `providers` array in `config/app.php`

```php
PHPZen\LaravelRbac\RbacServiceProvider::class,
```

Publish migration files

```
$ php artisan vendor:publish --provider="PHPZen\LaravelRbac\RbacServiceProvider" --force
```

Run migrations

```
$ php artisan migrate
```

Add RBAC middleware to your `app/Http/Kernel.php`

```php
protected $routeMiddleware = [
...
'rbac' => '\PHPZen\LaravelRbac\Middleware\Rbac::class'
];
```

Add Rbac trait to your `User` model

```php
use PHPZen\LaravelRbac\Traits\Rbac;

class User extends Authenticatable
{
use Rbac;
...

}
```

## Usage

### Roles

#### Create role

```php
$adminRole = new Role;
$adminRole->name = 'Administrator';
$adminRole->slug = 'administrator';
$adminRole->description = 'System Administrator';
$adminRole->save();

$editorRole = new Role;
$editorRole->name = 'Editor';
$editorRole->slug = 'editor';
$editorRole->description = 'Editor';
$editorRole->save();
```

#### Assign role to user

```php
$user = User::find(1);
$user->roles()->attach($adminRole->id);
```

you can also assign multiple roles at once

```php
$user->roles()->attach([$adminRole->id, $editorRole->id]);
```

#### Revoke role from user

```php
$user->roles()->detach($adminRole->id);
```

you can also revoke multiple roles at once

```php
$user->roles()->detach([$adminRole->id, $editorRole->id]);
```

#### Sync roles

```php
$user->roles()->sync([$editorRole->id]);
```

Any role already assigned to user will be revoked if you don't pass its id to sync method.

### Permissions

#### Create permission

```php
$createUser = new Permission;
$createUser->name = 'Create user';
$createUser->slug = 'user.create';
$createUser->description = 'Permission to create user';
$createUser->save();

$updateUser = new Permission;
$updateUser->name = 'Update user';
$updateUser->slug = 'user.update';
$updateUser->description = 'Permission to update user';
$updateUser->save();
```

#### Assign permission to role

```php
$adminRole = Role::find(1);
$adminRole->permissions()->attach($createUser->id);
```

you can also assign multiple permissions at once

```php
$adminRole->permissions()->attach([$createUser->id, $updateUser->id]);
```

#### Revoke permission from role

```php
$adminRole->permissions()->detach($createUser->id);
```

you can also revoke multiple permissions at once

```php
$adminRole->permissions()->detach([$createUser->id, $updateUser->id]);
```

#### Sync permissions

```php
$adminRole->permissions()->sync([$updateUser->id]);
```

Any permission already assigned to role will be revoked if you don't pass its id to sync method.

### Check user roles/permissions

Roles and permissions can be checked on `User` instance using `hasRole` and `canDo` methods.

```php
$isAdmin = Auth::user()->hasRole('administrator'); // pass role slug as parameter
$isAdminOrEditor = Auth::user()->hasRole('administrator|editor'); // using OR operator
$canUpdateUser = Auth::user()->canDo('update.user'); // pass permission slug as parameter
$canUpdateOrCreateUser = Auth::user()->canDo('update.user|create.user'); // using OR operator
```

### Protect routes

Laravel RBAC provides middleware to protect single route and route groups. Middleware expects 2 comma separated params:
- **is** or **can** as first param - what to check (role/permission)
- role/permission slug as second param

```php
Route::get('/backend', [
'uses' => 'BackendController@index',
'middleware' => ['auth', 'rbac:is,administrator']
]);
Route::get('/backend', [
'uses' => 'BackendController@index',
'middleware' => ['auth', 'rbac:is,administrator|editor']
]);
Route::get('/dashboard', [
'uses' => 'DashboardController@index',
'middleware' => ['auth', 'rbac:can,view.dashboard']
]);
Route::get('/dashboard', [
'uses' => 'DashboardController@index',
'middleware' => ['auth', 'rbac:can,view.dashboard|view.statistics']
]);
```

### Blade directive

Laravel RBAC provides two Blade directives to check if user has role/permission assigned.

Check for role

```
@ifUserIs('administrator')
// show admin content here
@else
// sorry
@endif
@ifUserIs('administrator|editor')
// show editor content here
@else
// sorry
@endif
```

Check for permission

```
@ifUserCan('delete.user')
// show delete button
@endif
@ifUserCan('delete.user|manage.user')
// show delete button
@endif
```

## License

Laravel RBAC is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "visualappeal/laravel-rbac",
"description": "Role based access control for Laravel 5",
"keywords": [
"laravel",
"rbac",
"acl",
"permissions",
"roles",
"auth",
"security"
],
"minimum-stability": "stable",
"license": "MIT",
"authors": [
{
"name": "PHPZen",
"email": "[email protected]",
"homepage": "https://github.com/phpzen",
"role": "Developer"
}
],
"require": {
"php": ">=5.5.9",
"illuminate/support": ">=5.4"
},
"autoload": {
"psr-4": {
"PHPZen\\LaravelRbac\\": "src/"
}
}
}
33 changes: 33 additions & 0 deletions src/Middleware/Rbac.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace PHPZen\LaravelRbac\Middleware;

use Closure;

class Rbac
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $level, $permission)
{
if (!in_array($level, ['is', 'isnt', 'can']))
abort(500, 'Invalid RBAC operator specified.');
if ('is' == $level) {
if ($request->user()->hasRole($permission))
return $next($request);
} else if ('isnt' == $level) {
if (!$request->user()->hasRole($permission))
return $next($request);
} else {
if ($request->user()->canDo($permission))
return $next($request);
}

abort(403);
}
}
15 changes: 15 additions & 0 deletions src/Model/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PHPZen\LaravelRbac\Model;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
protected $fillable = ['name', 'slug', 'description'];

public function roles()
{
return $this->belongsToMany('PHPZen\LaravelRbac\Model\Role');
}
}
20 changes: 20 additions & 0 deletions src/Model/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PHPZen\LaravelRbac\Model;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
protected $fillable = ['name', 'slug', 'description'];

public function users()
{
return $this->belongsToMany(config('auth.providers.users.model'));
}

public function permissions()
{
return $this->belongsToMany('PHPZen\LaravelRbac\Model\Permission')->withTimestamps();
}
}
Loading

0 comments on commit 567782c

Please sign in to comment.