Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
add: filter menu by roles
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Nov 26, 2019
1 parent 48d0119 commit 6e9cfb4
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,31 @@ public function all()
protected function filterByVisibility(Item $item)
{
$permission = $item->data('permission');
$roles = $item->data('roles');
$permissionCheck = $rolesCheck = true;

// If menu doesn't define permission, we assume this menu visible to everyone
// Otherwise, check if current user has access
if ($permission === null) {
return true;
if ($roles) {
$rolesCheck = auth()->user()->hasRole($roles);
}

// If it was multiple permissions, we check using OR conditions.
// It means, user only need to have one of the permissions
if (is_array($permission)) {
foreach ($permission as $perm) {
if (auth()->user()->can($perm)) {
return true;
// If permission defined, we assume User doesn't allowed to access Menu,
// until she proved that she has the access
if ($permission) {
$permissionCheck = false;
// If it was multiple permissions, we check using OR conditions.
// It means, user only need to have one of the permissions
if (is_array($permission)) {
foreach ($permission as $perm) {
if (auth()->user()->can($perm)) {
$permissionCheck = true;
break;
}
}
} else {
$permissionCheck = auth()->user()->can($item->data('permission'));
}
}

return auth()->user()->can($item->data('permission'));
return $permissionCheck && $rolesCheck;
}
}

0 comments on commit 6e9cfb4

Please sign in to comment.