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
}
}