2017 © Pedro Peláez
 

library laravel-wrappr

Bind permissions to Laravel routes.

image

foothing/laravel-wrappr

Bind permissions to Laravel routes.

  • Monday, June 26, 2017
  • by brazorf
  • Repository
  • 1 Watchers
  • 8 Stars
  • 124 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 21 Versions
  • 8 % Grown

The README.md

Laravel Wrappr

Build Status Code Climate Test Coverage, (*1)

For Laravel 5.1 use 0.4.10 version, (*2)

For Laravel 5.2 and up use the latest 0.x version, (*3)

This is a Laravel 5 package that aims to simplify the process of binding routes to permissions and it is indepentent from a specific permissions handler, allowing to add route checks even if your permissions handler doesn't support this feature natively., (*4)

Also, it tries to put some effort in decoupling your app from the permission handler for what concerns basic operations., (*5)

There is a Lock integration ready to use., (*6)

I've plans to implement a Gate integration as well, feel free to drop a line if you're interested in this., (*7)

Usage example

A basic use case where you want to restrict route access to the read.users permission, (*8)

Route::get('api/users/{id?}', ['middleware:wrappr.check:read.users,user,{id}', function() {
    // Access is allowed to users with the 'read.users' permission on
    // the 'user' resource with the {id} identifier
}]);

Or, you can define custom route patterns that can help with dynamic urls, assuming you have defined a controller and want to split the access logic on its methods., (*9)

Route::controller('api/v1/{args?}', 'FooController');

Assuming your controller provides the following routes, (*10)

GET /api/v1/resources/users
GET /api/v1/resources/posts
POST /api/v1/services/publish/post

you can define a rule like the following to restrict access:, (*11)

[
    'verb' => 'put',
    'path' => 'api/v1/resources/posts/{id}',
    'permissions' => ['posts.create', 'posts.update'],
    'resource' => 'post',
],

Contents

Concept

As it happens you may need to switch to another acl library at some time, so i've tried to put some effort into adding an abstract layer that would make your app more maintenaible. This package tries to abstract your app from the acl layer in 2 different ways:, (*12)

  • standard approach to route-based checks
  • standard api to basic acl manipulation

In order to access permissions checks, a permissions provider that acts as a bridge with the acl library must be set. Also, a users provider is required in order to retrieve the authenticated user., (*13)

While the route checks are the main focus of this project, the acl manipulation feature tries to stay out of the way so you'll just use it at will., (*14)

Install and Setup

Composer install, (*15)

"require": [
    "foothing/laravel-wrappr": "0.*"
]

Add the service provider in yourconfig/app.php providers array., (*16)

'providers' => [
    // ...
    Foothing\Wrappr\WrapprServiceProvider::class
]

Then publish package configuration and migration files, (*17)

php artisan vendor:publish --provider="Foothing\Wrappr\WrapprServiceProvider"

Configure the providers

Once you've published the config file you can configure an users provider and a permissions provider accordingly to your project setup. This sample configuration will enable the integration with Lock and Illuminate\Auth\Guard., (*18)

In your config/wrappr.php, (*19)

'permissionsProvider' => 'Foothing\Wrappr\Lock\LockProvider',
'usersProvider' => 'Foothing\Wrappr\Providers\Users\DefaultProvider',

Use within Laravel Router

There are two use cases for this package, each implemented in its own Middleware. Let's take a look to the default case. First of all you need to setup the Middleware in your App\Http\Kernel., (*20)

Add the following line:, (*21)

protected $routeMiddleware = [
    'wrappr.check' => 'Foothing\Wrappr\Middleware\CheckRoute',
];

Use the CheckRoute Middleware to control access to your routes like the following routes.php:, (*22)

Route::get('api/users', ['middleware:wrappr.check:admin.users', function() {
    // Access is allowed for the users with the 'admin.users' permission
}]);

The CheckRoute Middleware accepts 3 arguments: - the required permission - an optional resource name, i.e. 'user' - an optional resource identifier (integer), (*23)

Example:, (*24)

Route::get('api/users/{id?}', ['middleware:wrappr.check:read.users,user,1', function() {
    // Access is allowed for the users with the 'read.users' permission on
    // the 'user' resource with the {id} identifier
}]);

Also, the Middleware can handle your route arguments. Consider the following, (*25)

Route::get('api/users/{id?}', ['middleware:wrappr.check:read.users,user,{id}', function() {
    // Access is allowed for the users with the 'read.users' permission on
    // the 'user' resource with the {id} identifier
}]);

When you pass a resource identifier within the brackets, the middleware will try to retrieve the value from the http request automatically., (*26)

Use with custom routes

When you're not able to fine-control at routes definition level, there's an alternative way of handling permissions. Think about a global RESTful controller like the following:, (*27)

Route::controller('api/v1/{args?}', 'FooController');

Assume that your controller applies a variable pattern to handle the routes, like for example, (*28)

GET /api/v1/resources/users
GET /api/v1/resources/posts
POST /api/v1/services/publish/post

In this case you won't be able to bind permissions with the previous method, so the CheckPath middleware comes to help. In order to enable this behaviour you need some additional setup step., (*29)

First step is to run the migration you previously published., (*30)

php artisan migrate

then you have the following two choices., (*31)

Install routes with config file

You can now configure the routes you would like to put under authorization control In your config/wrappr.php edit your routes section:, (*32)

'routes' => [

    [
        // Allowed values are 'get', 'post', 'put', 'delete'
        // or the '*' wildcard to enable all verbs.
        'verb' => 'post',

        // The url path we want to restrict access to.
        'path' => 'foo',

        // The required permissions for the given path.
        'permissions' => 'bar',
    ],

    // This configuration will control the access to the
    // POST:api/v1/resources/users action, which will be
    // only allowed for users with the 'admin.account' permission
    [
        'verb' => 'post',
        'path' => 'api/v1/resources/users',
        'permissions' => 'admin.account',
    ],


    // This configuration will control the access to the
    // PUT:api/v1/resources/posts/{id} action, which will be
    // only allowed for users with both the 'posts.create' and
    // 'posts.update' permissions on the 'post' resource with
    // the {id} identifier.
    [
        'verb' => 'put',
        'path' => 'api/v1/resources/posts/{id}',
        'permissions' => ['posts.create', 'posts.update'],
        'resource' => 'post',
    ],

    // In this case the 'admin/' nested routes
    // will be granted access only when the 'admin' permission
    // is available to the current auth user.
    [
        'verb' => '*',
        'path' => 'admin/*',
        'permissions' => ['admin'],
    ],

    // You can also use the path wildcard in this way,
    // therefore requiring the 'superadmin' permission
    // for each route starting with 'admin'.
    [
        'verb' => '*',
        'path' => 'admin*',
        'permissions' => ['superadmin'],
    ],
],

Once you're done with your routes setup run the artisan command, (*33)

php artisan wrappr:install

Note that each time you change the routes configuration you should run the artisan command again in order to refresh them., (*34)

Install routes programmatically

Alternatively you can programmatically setup your routes using the RouteInstaller. In this case you won't need the artisan command., (*35)

$installer = \App::make('foothing.wrappr.installer');
$installer
    ->route('get', '/api/v1/*')->requires('api.read')
    ->route('put', '/api/v1/*')->requires('api.write')
    ->route('delete', '/api/v1/users/{id}/*')->requires('api.write,api.read')->on('users')
    ->install();

// Use wildcard
$installer->route('*', '/admin')->requires->('admin.access');

Setup the middleware

Add the global Middleware to your App\Http\Kernel like this, (*36)

protected $middleware = [
    \Foothing\Wrappr\Middleware\CheckPath::class
];

and you're all set., (*37)

A note on routes processing order

The Middleware will parse all incoming http requests to match your installed routes and it will react like the following - if a route pattern is not found access is granted - if a route pattern is found it will trigger the permissions provider that will perform the check, (*38)

Once you've got your routes installed keep in mind that they will be processed in a hierarchical order, from the more specific to the more generic. Look at this example, (*39)

api/v1/users/{id}/*
api/v1/users/{id}
api/v1/*
api/v1
api/*

This will result in the following behaviour - if you request foo/bar route is not found hence access is allowed - if you request api/foo permissions bound to the api/* pattern will be applied - if you request api/v1 permission bound to the api/v1 pattern will be applied, (*40)

and so on., (*41)

Middleware Response

Both the middleware implementation will return HTTP 401 on failure with an additional X-Reason: permission header that will come handy when dealing with responses on the client side (i.e. an angular interceptor)., (*42)

If you want your error responses to be redirected when the Middleware check fails, just set the redirect path in your wrappr.config, (*43)

'redirect' => '/login'

This value will be ignored when the http request is an ajax request., (*44)

How to develop providers

Extend Foothing\Wrappr\Providers\Permissions\AbstractProvider., (*45)

You'll have the mandatory check() method to implement, and other optional methods you can implement or ignore at your choice., (*46)

/**
 * Check the given user has access to the given permission.
 *
 * @param      $user
 * @param      $permissions
 * @param null $resourceName
 * @param null $resourceId
 *
 * @return mixed
 */
public function check($user, $permissions, $resourceName = null, $resourceId = null);

/**
 * Check the given subject has access to the given permission.
 *
 * @param      $permissions
 * @param null $resourceName
 * @param null $resourceId
 *
 * @return mixed
 */
public function can($permissions, $resourceName = null, $resourceId = null);

/**
 * Fluent method to work on users.
 * @param $user
 * @return self
 */
public function user($user);

/**
 * Fluent method to work on roles.
 * @param $role
 * @return self
 */
public function role($role);

/**
 * Return all permissions for the given subject.
 * @return mixed
 */
public function all();

/**
 * Grant the given permissions to the given subject.
 *
 * @param      $permissions
 * @param null $resourceName
 * @param null $resourceId
 *
 * @return mixed
 */
public function grant($permissions, $resourceName = null, $resourceId = null);

/**
 * Revoke the given permissions from the given subject.
 *
 * @param      $permissions
 * @param null $resourceName
 * @param null $resourceId
 *
 * @return mixed
 */
public function revoke($permissions, $resourceName = null, $resourceId = null);

License

MIT, (*47)

The Versions

26/06 2017
26/06 2017
28/07 2016

0.4.9

0.4.9.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

26/07 2016

0.4.7

0.4.7.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

26/07 2016

dev-develop

dev-develop

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

25/07 2016

0.4.6

0.4.6.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

22/06 2016

0.4.5

0.4.5.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

25/03 2016

0.4.4

0.4.4.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

25/03 2016

0.4.3

0.4.3.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

09/11 2015

0.4.2

0.4.2.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

07/11 2015

0.4.1

0.4.1.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

06/11 2015

0.4.0

0.4.0.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

03/11 2015

0.3.2

0.3.2.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

03/11 2015

0.3.1

0.3.1.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

30/10 2015

0.3.0

0.3.0.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

27/10 2015

0.2.2

0.2.2.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

26/10 2015

0.2.1

0.2.1.0

Bind permissions to Laravel routes.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

26/10 2015

0.2.0

0.2.0.0

Bind permissions to Laravel routes.

  Sources   Download

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes

23/10 2015

0.1.0

0.1.0.0

Bind permissions to Laravel routes.

  Sources   Download

The Requires

 

The Development Requires

by Avatar brazorf

laravel permissions routes