Laravel ACL Route Resource Not Working

After completing the tutorial on how the built-in acl larvel works, I tried it and it works well, defining each route on its own.

Now I'm trying to use the resource, but it does not work as intended. I added the following code to the routes file:

Route::group(['middleware' => 'acl:create_client'], function()
{
    Route::resource('clients', 'ClientController');
});

Now I understand what the problem is:

all methods in the Clientcontroller will be checked against my db if this user has acl: create_client, as a result, all methods are available for the registered user who has this acl.

How do I split each method to use its own acl without writing it like this:

Route::get('/client/create', [
    'middleware' => 'acl:create_client',
    'as' => 'clients.create',
    'uses' => 'ClientController@create'
]);

Result:

create needs create_client

index needs index_client

update update_client

etc.

+4
3

: - "" (ACL). , ; . , 'as', . :

Route::get('/', ['as'=>'clients.create', 'uses'=>'ClientsController@create']);

'clients.create' ACL. : ACL - 'as', ( ).

, , . , . , ACL .

1) App\Http\Middleware\Acl\CheckPermission $permission = null 'as', routes.php. :

<?php namespace App\Http\Middleware;

use Closure;

class CheckPermission
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next/*, $permission = null REMOVE THIS*/)
    {
        // Add the next two lines:
        $action = $request->route()->getAction();
        $permission = isset($action['as']) ? $action['as'] : '';

        if (!app('Illuminate\Contracts\Auth\Guard')->guest()) {
            if ($request->user()->can($permission)) {
                return $next($request);
            }
        }

        return $request->ajax ? response('Unauthorized.', 401) : redirect('/login');
    }
}

2) . , 'as', . : a) b) . 2a 2b, ACL .

2a) . : 'as'=>'clients.*' 'middleware' => 'acl'. , , (, 'middleware' => 'acl:manage_user'). , handle() . URI .

Route::group(['middleware' => 'acl'], function()
{
    Route::get('/clients', ['as'=>'clients.view', 'uses'=>'ClientsController@index']);
    Route::get('/clients/new', ['as'=>'clients.create', 'uses'=>'ClientsController@create']);
    // Add more routes ...
}

2b) , . /app/Http/Kernel.php $routeMiddleware. 2 , . : '\App\Http\Middleware\CheckPermission' $middleware, . $routeMiddleware , .

3) 'as' permissions permission_slug. SQL, id 123 clients.create. , 'client.create'.

INSERT INTO permissions ('permission_title', 'permission_slug', 'permission_description')
    VALUES ('Create a Client', 'clients.create', 'Allow the user to create a client');

INSERT INTO roles ('role_title', 'role_slug')
    VALUES ('Client Admin', 'clients.admin');

id . , , id=1. : id=1 id=1.

INSERT INTO permission_role ('permission_id', 'role_id') VALUES (1, 1);

, id=1, - 123. id=1 id=123.

INSERT INTO role_user ('role_id', 'user_id') VALUES (1, 123);

id=123, Client Admin. Client Admin 'clients.create'. id=123, , 'clients.create', (example.com/clients/new ). , ( , , , ).

+2

acl , , entrust

laravel acl, laracast laracast laravel acl tutorial

0

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


All Articles