PHPackages                             aneeq/laravelrbac - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. aneeq/laravelrbac

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

aneeq/laravelrbac
=================

Role Based Access Control For Laravel Projects

052PHP

Since May 15Pushed 8y ago1 watchersCompare

[ Source](https://github.com/aneeqtariq143/laravelrbac)[ Packagist](https://packagist.org/packages/aneeq/laravelrbac)[ RSS](/packages/aneeq-laravelrbac/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Role Based Access Control Package#
==========================================

[](#laravel-role-based-access-control-package)

---

Prerequisite
------------

[](#prerequisite)

- This library is built for laravel version 5.2
- Users Table exist in database with primary key "id"
- At least one user must with "id" =&gt; 1 exist in the table (User with Id "1" will be assumed "Admin").

Installation
------------

[](#installation)

1 Install RBAC Package by adding the following lines into ***composer.json*** file

```
"require": {
    "aneeq/laravelrbac": "dev-master"
}

```

or

```
composer require "aneeq/laravelrbac:dev-master"

```

2 Add the below line in autoload ps4 selection of ***composer.json*** file.

```
"Aneeq\\LaravelRbac\\": "vendor/aneeq/laravelrbac/src"

```

3 Add Provider into ***app.php*** config file.

```
Aneeq\LaravelRbac\Providers\RbacServiceProvider::class

```

4 Add Middleware into routeMiddleware of ***App/Http/Kernel.php*** file.

```
'role' => \Aneeq\LaravelRbac\Middleware\RbacRole::class,
'permission' => \Aneeq\LaravelRbac\Middleware\RbacPermission::class

```

5 Publish Package Files.

```
php artisan vendor:publish --provider="Aneeq\LaravelRbac\Providers\RbacServiceProvider"

```

The above command will copy ***Config, Migrations, Seeds and views*** file.

Note: If you want to Publish Specific files then, use the above command with `--tag="config"`

6 Migrate and Seed published tables.

```
php artisan migrate
php artisan db:seed --class=RolesTableSeeder
php artisan db:seed --class=PermissionsTableSeeder
php artisan db:seed --class=RolesPermissionsTableSeeder
php artisan db:seed --class=UsersRolesTableSeeder

```

7 Implement ***RbacUserInterface*** and ***RbacUserTrait*** on Laravel ***User*** Model

```
use Aneeq\LaravelRbac\Interfaces\RbacUserInterface;
use Aneeq\LaravelRbac\Traits\RbacUserTrait;

class User extends Authenticatable implements RbacUserInterface
{
	use RbacUserTrait;

```

Installation Completed

\##Usage##

1 Controller Authorization

```
if(!$request->user()->can('dashboad')){
        return redirect('unauthorized-access');
}

```

2 Middleware Authorization

```
Route::get('/url', 'Controller@function')->middleware('permission:permission-name')

```

3 view File Authorization

```

```

\###Events###

This package raises 4 event during "Assigning Permissions to the role" and "Assign Roles to the user". You may attach listeners to these events in your ***App\\Providers\\EventServiceProvider***

```
/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'Aneeq\LaravelRbac\Events\PreRolesAssignEvent' => [ // This event raise before Roles assigned to the User
        // Here is the list of User Defined Listner Class
        'App\Listeners\PreRolesAssignEventListener',
    ],
    'Aneeq\LaravelRbac\Events\PostRolesAssignEvent' => [ // This event raise after Roles assigned to the User
        // Here is the list of User Defined Listner Class
        'App\Listeners\PostRolesAssignEventListener',
    ],
    'Aneeq\LaravelRbac\Events\PrePermissionAssignEvent' => [ // This event raise before Permissions assigned to the Role
        // Here is the list of User Defined Listner Class
        'App\Listeners\PrePermissionAssignEventListner',
    ],
    'Aneeq\LaravelRbac\Events\PostPermissionAssignEvent' => [ // This event raise After Permissions assigned to the Role
        // Here is the list of User Defined Listner Class//
        'App\Listeners\PostPermissionAssignEventListner',
    ],
];

```

Example of Pre &amp; Post RolesAssignEvent

```
