2

I create some permissions and roles for my laravel project.

in web.php route, I can define a middleware like this with some permissions:

$can = ['can:manage_global,manage_users,create_users'];
$this->get('/create', 'UserController@create')->middleware($can);

or in blade with one permission:

@can('manage_global')
   ...
@endcan

How can I check multi permission values in blade and controller line web.php?

Areza
  • 671
  • 14
  • 26

2 Answers2

2

You can write middleware.

class CanAnyMiddleware
{
    public function handle($request, Closure $next, $permissions)
    {
        foreach ($permissions as $permission) {
            if ( $request->user()->can($permission)) {
                return $next($request); // allow
            }
        }

        return redirect(route('home')); // deny
    }
}

And use it in route string.

Route::get('/create', 'UserController@create')
    ->middleware('canAny:manage_global,manage_users,create_users');

And since Laravel 5.6 you can use @canany in Blade:

@canany(['manage_global', 'manage_users', 'create_users'])
    ...
@endcanany

https://github.com/laravel/framework/pull/24137

Roman Meyer
  • 2,634
  • 2
  • 20
  • 27
1

If you're trying to check if a user can access any of your defined gates, from the controller, you can take a queue from the existing Authorizable trait and add some additional functionality in your own trait.

<?php

namespace App\Traits\MyAuthorizable;

use Illuminate\Contracts\Auth\Access\Gate;

trait MyAuthorizable {
  public function canAny(array $abilities, $arguments = []) {
    return collect($abilities)->reduce(function($canAccess, $ability) use ($arguments) {
      // if this user has access to any of the previously checked abilities, or the current ability, return true
      return $canAccess || app(Gate::class)->forUser($this)->check($ability, $arguments);
    }, false);
  }

  public function canAll(array $abilities, $arguments = []) {
    return collect($abilities)->reduce(function($canAccess, $ability) use ($arguments) {
      // if this user has access to _all_ of the previously checked abilities, _and_ the current ability, return true
      return $canAccess && app(Gate::class)->forUser($this)->check($ability, $arguments);
    }, true);
  }
}

You can than add this trait to your user class with use App\ MyAuthorizable; in your user class definition.

This will expose the canAny and canAll methods for your user, which you can then access from your controller.

<?php

public function get($request) {
    $User = Auth::User();

    if ($User->canAll(['manage_global', 'manage_users', 'create_users'])) {
        // user can do all of the things
    } elseif ($User->canAny(['manage_global', 'manage_users', 'create_users']) {
        // user can only do _some_ of the things
    } else {
        // user can do _none_ of the things
    }
}
Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54