PHPackages                             trunow/rpac - 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. trunow/rpac

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

trunow/rpac
===========

Roles/Permissions Access Control (Rpac) Laravel package

v1.0(6y ago)0241MITVuePHP &gt;=7.1.3

Since Feb 12Pushed 6y ago1 watchersCompare

[ Source](https://github.com/trunow/rpac)[ Packagist](https://packagist.org/packages/trunow/rpac)[ RSS](/packages/trunow-rpac/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Roles/Permissions Access Control \[RPAC\] Laravel Package
=========================================================

[](#rolespermissions-access-control-rpac-laravel-package)

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

[](#installation)

### Composer

[](#composer)

Pull this package in through Composer (file `composer.json`)...

```
{
    "require": {
        "php": ">=7.2.0",
        "laravel/framework": "^6.*",
        "trunow/rpac": "dev-master"
    }
}
```

...and run this command inside your terminal.

```
composer update

```

OR require this package

```
composer require trunow/rpac:dev-master

```

### Service Provider

[](#service-provider)

Add the package to your application service providers in `config/app.php` file.

```
'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
    Illuminate\Auth\AuthServiceProvider::class,
    ...

    /**
     * Third Party Service Providers...
     */
    Trunow\Rpac\RpacServiceProvider::class,

],
```

### Config File And Migrations

[](#config-file-and-migrations)

Publish the package config file and migrations to your application. Run these commands inside your terminal.

```
php artisan vendor:publish --provider="Trunow\Rpac\RpacServiceProvider"

```

And also run migrations.

```
php artisan migrate

```

> This uses the default users table which is in Laravel. You should already have the migration file for the users table available and migrated.

### Rpacable Trait and $with = \[roles\]

[](#rpacable-trait-and-with--roles)

Include `Rpacable` trait inside your `User` model.

```
use Trunow\Rpac\Traits\Rpacable;

class User extends Model implements AuthenticatableContract
{
    use Authenticatable, Rpacable;
```

And set protected property $with = \['roles'\] (for autoloading roles with User's model).

```
protected $with = ['roles'];

```

### Create Su/Admin User

[](#create-suadmin-user)

Run command, example `rpac su:1` or `rpac admin:email@example.com` or `role:user@example.com:pa$$w0r5` .

```
php artisan rpac su:slava@trunov.me

```

> (:
>
> And go to `your-domain.com/admin-rpac`
>
> :)

Usage
-----

[](#usage)

### Creating Policy

[](#creating-policy)

Create empty policy class extends `RpPolicy` for your model.

```
namespace App\Policies;

use Trunow\Rpac\Policies\RpPolicy;

class PostPolicy extends RpPolicy
{
    //...
}
```

TODO

### Creating Roles

[](#creating-roles)

```
use Trunow\Rpac\Role;

$adminRole = Role::create([
    'name' => 'Admin',
    'slug' => 'admin',
    'description' => '', // optional
]);
```

### Attaching And Detaching Roles

[](#attaching-and-detaching-roles)

It's standart. There is `BelongsToMany` relationship between `User` and `Role` model.

```
use App\User;

$user = User::find($id);
$user->roles()->attach($adminRole); // you can pass whole object, or just an id
```

```
$user->roles()->detach($adminRole); // in case you want to detach role
$user->roles()->detach(); // in case you want to detach all roles
```

### Checking For Roles

[](#checking-for-roles)

You can now check if the user has required role.

```
if ($user->is('admin')) { // pass role slug here
    // ...
}
```

You can also do this:

```
if ($user->isAdmin()) {
    //
}
```

And of course, there is a way to check for multiple roles:

```
if ($user->is(['admin', 'moderator'])) {
    /*
    | It is same as:
    | $user->isOr(['admin', 'moderator'])
    */

    // if user has at least one role
}

if ($user->is(['admin', 'moderator'], true)) {
    /*
    | Or alternatively:
    | $user->isAnd(['admin', 'moderator'])
    */

    // if user has all roles
}
```

### Creating Permissions

[](#creating-permissions)

It's very simple thanks to `Permission` model.

```
use Trunow\Rpac\Permission;

$createPostPermission = Permission::create([
    'name' => 'Create posts',
    'entity' => 'App\Post',
    'action' => 'create',
]);
```

### Attaching And Detaching Permissions

[](#attaching-and-detaching-permissions)

You can attach permissions to a role (and of course detach them as well).

```
use Trunow\Rpac\Role;

$role = Role::find($roleId);
$role->permissions()->attach($createPostPermission); // permission attached to a role
```

```
$role->permissions()->detach($createPostPermission); // in case you want to detach permission
$role->permissions()->detach(); // in case you want to detach all permissions
```

### Checking For Permissions

[](#checking-for-permissions)

TODO

### Entity Check

[](#entity-check)

Let's say you have an article and you want to edit it.

```
use App\Article;
use Trunow\Rpac\Permission;

$editArticlesPermission = Permission::create([
    'name' => 'Edit articles',
    'entity' => 'App\Article',
    'action' => 'edit',
]);

$user->roles()->first()->permissions()->attach($editArticlesPermission);

$article = Article::find(1);

if ($user->can('edit', $article)) {
    //
}
```

### Blade Extensions

[](#blade-extensions)

There are four Blade extensions. Basically, it is replacement for classic if statements.

```
@role('admin') // @if(Auth::check() && Auth::user()->is('admin'))
    // user is admin
@endrole
```

### Middleware

[](#middleware)

This package comes with `VerifyRole` middleware. You can easily protect your routes.

```
$router->get('/example', [
    'as' => 'example',
    'middleware' => 'role:admin,manager',
    'uses' => 'ExampleController@index',
]);
```

Config File
-----------

[](#config-file)

You can change connection for models, models path and there is also a handy pretend feature. Have a look at config file for more information.

License
-------

[](#license)

This package is free software distributed under the terms of the MIT license.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2277d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eee572e3e962b0100046aa05f861eddc2b6796b4622453b40d340cde53d3726c?d=identicon)[trunow](/maintainers/trunow)

---

Top Contributors

[![Cellard](https://avatars.githubusercontent.com/u/1220316?v=4)](https://github.com/Cellard "Cellard (2 commits)")

---

Tags

laravelauthaclrolespermissionsrbacilluminaterpac

### Embed Badge

![Health badge](/badges/trunow-rpac/health.svg)

```
[![Health](https://phpackages.com/badges/trunow-rpac/health.svg)](https://phpackages.com/packages/trunow-rpac)
```

###  Alternatives

[phpzen/laravel-rbac

Role based access control for Laravel 5

383.2k](/packages/phpzen-laravel-rbac)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
