distilleries/permission-util

Provide a system to detect if the user connected have access or not. Provide a middleware to restrict access with a 403
8,609 2
Install
composer require distilleries/permission-util
Latest Version:v1.11.0
PHP:>=7.1.3
License:MIT
Last Updated:Sep 10, 2019
Links: GitHub  ·  Packagist
Maintainer: Kezho

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version License

Laravel 5 Permission Util

Provide a system to detect if the user connected have access or not. Provide a middleware to restrict access with a 403

Table of contents

  1. Installation
  2. Basic usage
  3. Middleware

##Installation

Add on your composer.json

    "require": {
        "distilleries/permission-util": "1.*",
    }

run composer update.

Add Service provider to config/app.php:

    'providers' => [
        // ...
       'Distilleries\PermissionUtil\PermissionUtilServiceProvider',
    ]

And Facade (also in config/app.php)

    'aliases' => [
        // ...
        'PermissionUtil'   => 'Distilleries\PermissionUtil\Facades\PermissionUtil',
    ]

Export the configuration:

php artisan vendor:publish --provider="Distilleries\PermissionUtil\PermissionUtilProvider"

###Basic usage To check the permission, I use the auth of your application. On your model use for the Auth implement the interface Distilleries\PermissionUtil\Contracts\PermissionUtilContract add the method hasAccess to define if the user have access or not. The key in param is a string action like UserController@getEdit.

    public function hasAccess($key)
    {
        return true;
    }

If the user is connected and your model haven't this method the class return true. If the user is not connected the permission util return false. To disabled the restriction of connected user just go in config file and put false in auth_restricted.

You can use the facade to detect if the user can access or not:

Method Call Description
hasAccess PermissionUtil::hasAccess('Controller@action') Return if the user can access to this action

##Middleware The package provide a middleware to detect automatically if the user can access to a method of a controller. To active it just add on your app/Http/Kernel:

    protected $routeMiddleware = [
        //...
		'permission' => 'Distilleries\PermissionUtil\Http\Middleware\CheckAccessPermission',
	];

And on your controller or your route add the middleware:

    public function __construct()
    {
        $this->middleware('permission');
    
    }

If the user can access to an action that dispatch a 403

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

631,580,251 9,707
laravel/framework

The Laravel Framework.

580,311,802 34,925
laravel/tinker

Powerful REPL for the Laravel framework.

489,754,436 7,444
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

404,034,618 608
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

384,821,166 4,658