PHPackages                             damiantw/laravel-roles - 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. damiantw/laravel-roles

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

damiantw/laravel-roles
======================

Role Based Authorization For Laravel 6

v2.2.0(1mo ago)122672MITPHPPHP &gt;=8.2.0CI failing

Since Sep 28Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/damiantw/laravel-roles)[ Packagist](https://packagist.org/packages/damiantw/laravel-roles)[ RSS](/packages/damiantw-laravel-roles/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (10)Used By (0)

Role Based Authorization For Laravel 6
======================================

[](#role-based-authorization-for-laravel-6)

1. [Purpose](#purpose)
2. [Concept](#concept)
3. [Installation](#installation)
4. [Usage](#usage)
    - [Middleware](#middleware)
        - [Route Middleware](#route-middleware)
        - [Controller Middleware](#controller-middleware)
    - [API](#api)
    - [Seeding RoleGroups](#seeding-role-groups)
        - [Controller Based RoleGroup Seeding](#controller-based-role-group-seeding)
    - [Blade Directives](#blade-directives)

Purpose

----------

[](#purpose-)

This package aims to provide a granular, clearly defined and easily accessible authority set for association with the Laravel User model. Out of the box Laravel offers some very [powerful tools](https://laravel.com/docs/6.x/authorization "Laravel 6.x Authorization Docs") for handling checks on whether a user can complete a certain action. Often the determining factor is the result of a simple boolean calculation (ex. does the id of a user match the user\_id of the Post being edited?). A User's defined authority set can easily be factored into these calculations to provide a protection front using the provided API. We can then only allow users with the correct authorities access to actions using the Laravel Authorization tools or the provided Middleware.

Concept

----------

[](#concept-)

Each user in the application has a defined authority set. An authority is nothing more then a unique string that hints at a permitted action or a level of privilege. Authorities live inside Roles, with each Role holding exactly one authority value. Roles can be associated with specific Users or with any number of RoleGroups. RoleGroups provide a way to define a common set of authorities that can be shared by many users. A User can be associated with many RoleGroups.

A user's final defined authorities consists of the set of authorities from the Roles directly associated with the user merged with all of the authorities associated with a User's RoleGroups. This approach allows for the flexibility to handle special case scenarios (such as needing to offer a specific lower privileged User access to single administrative action.) while providing the convenience of common assignable authority sets.

Installation

---------------

[](#installation-)

Install via **Composer**

`composer require DamianTW/laravel-roles`

Next add the ServiceProvider to the Package Service Providers in `config/app.php`

```
        /*
         * Package Service Providers...
         */
        DamianTW\LaravelRoles\Providers\RoleServiceProvider::class,
```

Add the HoldsAuthorities traits to the User model

```
