Laravel 5.1 ACL

I read about the new policy features in Laravel 5.1 .

From the documents you can see that the blacklist is selected by default. For example. controller actions are possible until access is verified and denied using the policy.

Can this be whitelisted? Thus, every controller action is rejected, unless explicitly provided.

+4
source share
1 answer

I just found a pretty clean way, I think on your routes you pass middleware and policies that need to be checked.

Code example:

<?php

namespace App\Http\Middleware;

use Closure;

class PolicyMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $policy The policy that will be checked
     * @return mixed
     */
    public function handle($request, Closure $next, $policy)
    {
        if (! $request->user()->can($policy)) {
            // Redirect...
        }

        return $next($request);
    }

}

:

   Route::put('post/{id}', ['middleware' => 'policy:policytobechecked', function ($id) {
    //
}]);
0

Source: https://habr.com/ru/post/1616564/


All Articles