2017 © Pedro Peláez
 

project authorization

Native Laravel Authorization.

image

larapacks/authorization

Native Laravel Authorization.

  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 11 Forks
  • 0 Open issues
  • 11 Versions
  • 12 % Grown

The README.md

Authorization

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version License, (*1)

An easy, native role / permission management system for Laravel., (*2)

🚨 Repository Has Moved 🚨

This repository has been moved to: DirectoryTree/Authorization., (*3)

Index

Installation

Note: Laravel 5.5 or greater is required., (*4)

To get started, install Authorization via the Composer package manager:, (*5)

composer require larapacks/authorization

The Authorization service provider registers its own database migration directory with the framework, so you should migrate your database after installing the package. The Authorization migrations will create the tables your application needs to store roles and permissions:, (*6)

php artisan migrate

Now insert the Larapacks\Authorization\Traits\Authorizable onto your App\Models\User model:, (*7)

<?php

namespace App\Models;

use Larapacks\Authorization\Traits\Authorizable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Authorizable;

    // ...
}

You can now perform user authorization., (*8)

Migration Customization

If you would not like to use Authorization's default migrations, you should call the Authorization::ignoreMigrations method in the register method of your AppServiceProvider. You may export the default migrations using php artisan vendor:publish --tag=authorization-migrations., (*9)

use Larapacks\Authorization\Authorization;

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    Authorization::ignoreMigrations();
}

Model Customization

By default, the App\Models\User class is registered as the authorizable user model., (*10)

You're free to extend the models used internally by Authorization, or create your own., (*11)

Instruct Authorization to use your own models via the Authorization class in your AuthServiceProvider:, (*12)

use App\Models\User;
use App\Models\Role;
use App\Models\Permission;
use Larapacks\Authorization\Authorization;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Authorization::useUserModel(User::class);
    Authorization::useRoleModel(Role::class);
    Authorization::usePermissionModel(Permission::class);
}

Be sure to add the relevant traits for each of your custom models:, (*13)

Role Model:, (*14)

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Larapacks\Authorization\Traits\ManagesPermissions;

class Role extends Model
{
    use ManagesPermissions;
}

Permission Model:, (*15)

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Larapacks\Authorization\Traits\HasUsers;
use Larapacks\Authorization\Traits\HasRoles;
use Larapacks\Authorization\Traits\ClearsCachedPermissions;

class Permission extends Model
{
    use HasUsers, HasRoles, ClearsCachedPermissions;
}

Usage

Authorization utilizes native Laravel relationships, so there's no need to learn a new API., (*16)

Create a permission:, (*17)

$createUsers = new Permission();

$createUsers->name = 'users.create';
$createUsers->label = 'Create Users';

$createUsers->save();

Grant the permission to a role:, (*18)

$administrator = new Role();

$administrator->name = 'administrator';
$administrator->label = 'Admin';

$administrator->save();

$administrator->permissions()->save($createUsers);

Now assign the role to the user:, (*19)

$user->roles()->save($administrator);

You can also create user specific permissions:, (*20)

$createUsers = new Permission();

$createUsers->name = 'users.create';
$createUsers->label = 'Create Users';

$createUsers->save();

$user->permissions()->save($createUsers);

Checking Permissions & Roles

Using Laravel's native $user->can() method:, (*21)

if ($user->can('users.create')) {
    // This user can create other users.
}

Using Laravel's native authorize() method in your controllers:, (*22)

public function create()
{
    $this->authorize('users.create');

    User::create(['...']);
}

Using Laravel's native Gate facade:, (*23)

if (Gate::allows('users.create')) {
    //
}

Using Laravel's native @can directive in your views:, (*24)

@can('users.create')
    <!-- This user can create other users. -->
@endcan

Checking Permissions & Roles (Using Authorization Package Methods)

Checking for permission:, (*25)

// Using the permissions name.
if ($user->hasPermission('users.create')) {
    //
}

// Using the permissions model.
if ($user->hasPermission($createUsers)) {
    //
}

Checking for multiple permissions:, (*26)

if (auth()->user()->hasPermissions(['users.create', 'users.edit'])) {
    // This user has both creation and edit rights.
} else {
    // It looks like the user doesn't have one of the specified permissions.
}

Checking if the user has any permissions:, (*27)

if (auth()->user()->hasAnyPermissions(['users.create', 'users.edit', 'users.destroy'])) {
    // This user either has create, edit or destroy permissions.
} else {
    // It looks like the user doesn't have any of the specified permissions.
}

Checking if the user has a role:, (*28)

if (auth()->user()->hasRole('administrator')) {
    // This user is an administrator.
} else {
    // It looks like the user isn't an administrator.
}

Checking if the user has specified roles:, (*29)

if (auth()->user()->hasRoles(['administrator', 'member'])) {
    // This user is an administrator and a member.
} else {
    // It looks like the user isn't an administrator or member.
}

Checking if the user has any specified roles:, (*30)

if (auth()->user()->hasAnyRoles(['administrator', 'member', 'guest'])) {
    // This user is either an administrator, member or guest.
} else {
    // It looks like the user doesn't have any of these roles.
}

Caching

By default all permissions are cached to prevent them from being retrieved on every request., (*31)

This cache is automatically flushed when permissions are created, updated, or deleted., (*32)

If you would like to disable the cache, call Authorization::disablePermissionCache in your AuthServiceProvider:, (*33)

use Larapacks\Authorization\Authorization;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Authorization::disablePermissionCache();
}

Cache Key

By default, the permission cache key is authorization.permissions., (*34)

To alter the cache key, call Authorization::cacheKey in your AuthServiceProvider:, (*35)

use Larapacks\Authorization\Authorization;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Authorization::cacheKey('my-key');
}

Cache Expiry

By default, the permission cache will expire daily., (*36)

To alter this expiry date, call Authorization::cacheExpiresIn in your AuthServiceProvider:, (*37)

use Larapacks\Authorization\Authorization;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Authorization::cacheExpiresIn(now()->addWeek());
}

Gate Registration

By default all permissions you create are registered in Laravel's Gate., (*38)

If you would like to disable this, call Authorization::disableGateRegistration in your AuthServiceProvider:, (*39)

use Larapacks\Authorization\Authorization;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Authorization::disableGateRegistration();
}

Middleware

Authorization includes two useful middleware classes you can utilize for your routes., (*40)

Insert them into your app/Http/Kernel.php:, (*41)

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

    // The role middleware:
    'role' => \Larapacks\Authorization\Middleware\RoleMiddleware::class,

    // The permission middleware:
    'permission' => \Larapacks\Authorization\Middleware\PermissionMiddleware::class,
];

Once you've added them, you can start using them., (*42)

Note: When a user does not meet the requirements using the middleware, a 403 HTTP exception is thrown., (*43)

To guard a route to only allow specific permissions:, (*44)

Route::get('users', [
    'uses' => 'UsersController@index',
    'middleware' => 'permission:users.index',
]);

// Multiple permissions:
Route::get('users', [
    'uses' => 'UsersController@index',
    // Users must have index **and** create rights to access this route.
    'middleware' => 'permission:users.index,users.create',
]);

To guard a route to allow a specific role:, (*45)

Route::get('users', [
    'uses' => 'UsersController@index',
    'middleware' => 'role:administrator',
]);

// Multiple roles:
Route::get('users', [
    'uses' => 'UsersController@index',
    // Users must be an administrator **and** a member to access this route.
    'middleware' => 'role:administrator,member',
]);

Running Tests

To run your applications tests, you must instantiate the PermissionRegistrar inside your TestCase::setUp() method before running your tests for permissions to register properly:, (*46)

use Larapacks\Authorization\PermissionRegistrar;
protected function setUp() : void
{
    parent::setUp();

    app(PermissionResistrar::class)->register();
}

Upgrading v1 to v2

Configuration

Configuration is now done via static methods on the Authorization class., (*47)

You may delete the published config/authorization.php file., (*48)

Traits

The UserRolesTrait has been renamed to Authorizable., (*49)

The PermissionRolesTrait has been separated into multiple traits. You must apply the HasRoles, HasUsers, and ClearsCachedPermissions traits., (*50)

The RolePermissionsTrait has been renamed to ManagesPermissions., (*51)

Closure Permissions

Permission closures have been removed. If you still require this functionality, continue using v1., (*52)

The Versions

22/05 2017

dev-master

9999999-dev

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

22/05 2017

v1.1.8

1.1.8.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

22/05 2017

v1.1.7

1.1.7.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

12/09 2016

v1.1.6

1.1.6.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

12/09 2016

v1.1.5

1.1.5.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

07/09 2016

v1.1.4

1.1.4.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

21/07 2016

v1.1.3

1.1.3.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

06/05 2016

v1.1.2

1.1.2.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

06/05 2016

v1.1.1

1.1.1.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

05/05 2016

v1.1.0

1.1.0.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions

04/05 2016

v1.0.0

1.0.0.0

Native Laravel Authorization.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel authorization acl roles permissions