Middleware parameters - comma separated [5.2]

Posted 3 years ago by [email protected]

Hello all,

My apologies if this has been answered before. I did look around but can't find a similar issue.

I am trying to implemented a roles middleware on routing groups. I've got the basics working quite happily. If a user has 1 role, that's no problem. However, my users are likely to have multiple roles.

I see from the documentation that: "Middleware parameters may be specified when defining the route by separating the middleware name and parameters with a :. Multiple parameters should be delimited by commas"

So I attempted the following

Route::group(['middleware' => ['web','auth.role:agent,ops']], function () {

However, in my middleware if I dd($role) I only get the first parameter (i.e 'agent'). It may be that it is my poor understanding of middleware and laravel is calling the middleware of each of those params in turn. However, the problem I am facing is that if a user doesn't have the 'agent' role, but does have the 'ops' role they will already be returned a 403.

This is my middleware

    public function handle($request, Closure $next, $roles)
    {

        dd($role);


        if( auth()->check() ){
            if ( (auth()->user()->hasRole($role)) || auth()->user()->hasRole('manager') ){

                return $next($request);

            }
        }

        abort(403, 'Sorry! You do not have permission for this');
    }

Now, I did come up work a workaround of doing this

Route::group(['middleware' => ['web','auth.role:agent|ops']], function () {

Here I delimit the params I want to use with a pipe and then inside the middleware explode using this. I then foreach the array but I was wondering if the community had a better approach?

Appreciate any thoughts you may have!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.