PHPackages                             lbausch/laravel-fortress - 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. lbausch/laravel-fortress

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

lbausch/laravel-fortress
========================

Roles and Permissions for Laravel's Authorization

v0.4.1(7y ago)7745MITPHP

Since Sep 18Pushed 6y ago1 watchersCompare

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

READMEChangelog (6)Dependencies (2)Versions (18)Used By (0)

Laravel Fortress
================

[](#laravel-fortress)

Roles and Permissions for Laravel's Authorization
-------------------------------------------------

[](#roles-and-permissions-for-laravels-authorization)

[Laravel](http://laravel.com/) 5.1.11 already ships with a great [Authorization](http://laravel.com/docs/5.1/authorization) system. Fortress aims to add the funcionality of Roles and Permissions in an easy to use, non-invasive manner.

Installation in 3 easy steps
----------------------------

[](#installation-in-3-easy-steps)

**Require the package with composer**:

```
composer require lbausch/laravel-fortress

```

**Add the Service Provider in `config/app.php`**:

```
Bausch\LaravelFortress\ServiceProvider::class,

```

**Publish database migration and config file**:

```
php artisan vendor:publish --provider="Bausch\LaravelFortress\ServiceProvider"

```

Run `php artisan migrate` to finish installation (the Facade `Fortress` is added automagically).

Using Laravel Fortress
----------------------

[](#using-laravel-fortress)

### Guard your Models

[](#guard-your-models)

Every Model (User, Group, ...) that should be protected by Fortress needs to implement the Contract `FortressGuardContract` and may use the Trait `FortressGuardTrait` (in addition to `AuthorizableContract` and `Authorizable`).

```
...
use Bausch\LaravelFortress\Contracts\FortressGuardContract;
use Bausch\LaravelFortress\Traits\FortressGuardTrait as FortressGuard;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract,
                                    FortressGuardContract
{
    use Authenticatable, Authorizable, CanResetPassword, FortressGuard;
...
```

After that the following methods are available on the Model:

- `assignRole($role_name, $resource = null)`: Assign a Role (for a Resource) to the Model
- `revokeRole($role_name, $resource = null)`: Revoke a Role (for a Resource) from the Model
- `hasRole($role_name, $resource = null)`: Check for a Role (for a Resource)
- `hasPermission($permission_name, $resource = null)`: Check for a Permission (for a Resource)
- `destroyRoles()`: Destroy all Roles a Model has

You also can define a method called `fortress_relations()` on your model and return a `\Illuminate\Support\Collection` of additional Models which should be checked (e.g. Groups):

```
...
    /**
     * Fortress Relations.
     *
     * @return \Illuminate\Support\Collection
     */
    public function fortress_relations()
    {
        return $this->groups;
    }

    /**
     * Groups.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function groups()
    {
        return $this->belongsToMany(Group::class, 'group_members');
    }
...
```

### Global Roles

[](#global-roles)

As you may have noticed the `$resource` parameter is optional. You can assign Roles which only apply to a specific Resource or - if you omit the `$resource` parameter - assign so called "global Roles". Global Roles do not apply to a specific Resource (Post, Blog, ...) and are defined in the config file `config/laravel-fortress.php`:

```
