pgrzesiecki / laravel-acl

Advanced ACL for Laravel
50 stars 22 forks source link

Route middleware and Controller middleware #11

Open amilgaul opened 8 years ago

amilgaul commented 8 years ago

Any possibilities for route and controller middleware check?

For Controller checking: class SampleController extends Controller { public function __construct(){ $this->middleware('role:admin'); } }

For Route checking: Route::group(['middleware' => ['role:admin', 'permission:create_post'], function(){ ... }])

Thanks.

pgrzesiecki commented 8 years ago

Hello, Currently you can register own middleware to support this functionality.

Example code:

use Closure;
use Signes\Acl\Exception\AclAccessException;

class AclMiddleware
{
    public function handle($request, Closure $next, $access)
    {
        if (!app('acl')->isAllow($access)) {
            throw new \Exception("You do not have access to this resource.");
        }

        return $next($request);
    }
}

and use it like

// Routes
Route::get('/', ['middleware' => ['acl:zone.permission|action1.action2'], function () {

// Controller
$this->middleware('acl:zone.permission|action1.action2');

This feature is also added to current master branch and will be available by default in v2.2.

Thank you for your feedback.