Skip to content

Commit

Permalink
Added Permission check
Browse files Browse the repository at this point in the history
  • Loading branch information
balajidharma committed Jan 20, 2024
1 parent 9fb4668 commit 3d6407c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public static function validateMachineName($machine_name)
return preg_match('/^[a-z0-9_-]+$/', $machine_name);
}

protected static function getMenuTree($machine_name, $includeDisabledItems = false)
protected static function getMenuTree($machine_name, $includeDisabledItems = false, $checkPermission = false)
{
$menu = Menu::where('machine_name', $machine_name)->first();
if (! $menu) {
throw MenuNotExists::create($machine_name);
}

return (new MenuItem)->toTree($menu->id, $includeDisabledItems);
return (new MenuItem)->toTree($menu->id, $includeDisabledItems, $checkPermission);
}
}
2 changes: 1 addition & 1 deletion src/Models/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class MenuItem extends Model
{
use spatiePermission;
use MenuTree {
MenuTree::boot as treeBoot;
}
use spatiePermission;

/**
* The attributes that aren't mass assignable.
Expand Down
76 changes: 54 additions & 22 deletions src/Traits/MenuTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,38 +168,43 @@ public function withQuery(?\Closure $query = null)
/**
* Format data to tree like array.
*
* @return array
* @return \Illuminate\Support\Collection
*/
public function toTree($menuId, $includeDisabledItems = false)
public function toTree($menuId, $includeDisabledItems = false, $checkPermission = false)
{
return $this->buildNestedArray($menuId, $includeDisabledItems);
return $this->buildNestedItems($menuId, $includeDisabledItems, $checkPermission);
}

/**
* Build Nested array.
*
* @param int $parentId
* @return array
* @return \Illuminate\Support\Collection
*/
protected function buildNestedArray($menuId, $includeDisabledItems = false, array $nodes = [], $parentId = 0)
protected function buildNestedItems($menuId, $includeDisabledItems = false, $checkPermission = false, $nodes = null, $parentId = 0)
{
$branch = [];
$branch = collect();

if (empty($nodes)) {
$nodes = $this->allNodes($menuId, null, $includeDisabledItems);
}
$nodes->each(function ($node) use ($menuId, $nodes, $includeDisabledItems, $checkPermission, $parentId, &$branch) {
$hasPermission = true;
$parentColumn = $this->getParentColumn();
$keyName = $this->getKeyName();

foreach ($nodes as $node) {
if ($node[$this->getParentColumn()] == $parentId) {
$children = $this->buildNestedArray($menuId, $includeDisabledItems, $nodes, $node[$this->getKeyName()]);

if ($checkPermission && ! $this->checkHasPermission($node)) {
$hasPermission = false;
}
if ($parentId == $node->$parentColumn && $hasPermission) {
$children = $this->buildNestedItems($menuId, $includeDisabledItems, $checkPermission, $nodes, $node->$keyName);
if ($children) {
$node['children'] = $children;
$node->children = $children;
}

$branch[] = $node;
$branch->push($node);
}
}
});

return $branch;
}
Expand All @@ -225,14 +230,20 @@ public function allNodes($menuId, $ignoreItemId = null, $includeDisabledItems =
->when(! $includeDisabledItems, function ($query) {
$query->where('enabled', true);
})
->orderBy($this->getOrderColumn())->get()->toArray();
->when($this->hasSpatiePermission, function ($query) {
$query->with('roles');
})
->orderBy($this->getOrderColumn())->get();
}

return $self->where($this->getMenuRelationColumn(), $menuId)
->when(! $includeDisabledItems, function ($query) {
$query->where('enabled', true);
})
->orderBy($this->getOrderColumn())->get()->toArray();
->when($this->hasSpatiePermission, function ($query) {
$query->with('roles');
})
->orderBy($this->getOrderColumn())->get();
}

/**
Expand All @@ -256,7 +267,7 @@ public static function selectOptions($menuId, $ignoreItemId = null, $includeDisa
* @param string $space
* @return array
*/
protected function buildSelectOptions($menuId, $ignoreItemId, $includeDisabledItems = false, array $nodes = [], $parentId = 0, $prefix = '', $space = ' ')
protected function buildSelectOptions($menuId, $ignoreItemId, $includeDisabledItems = false, $nodes = null, $parentId = 0, $prefix = '', $space = ' ')
{
$prefix = $prefix ?: ''.$space;

Expand All @@ -266,21 +277,24 @@ protected function buildSelectOptions($menuId, $ignoreItemId, $includeDisabledIt
$nodes = $this->allNodes($menuId, $ignoreItemId, $includeDisabledItems);
}

foreach ($nodes as $index => $node) {
if ($node[$this->getParentColumn()] == $parentId) {
$node[$this->getTitleColumn()] = $prefix.$space.$node[$this->getTitleColumn()];
$nodes->each(function ($node) use ($menuId, $nodes, $includeDisabledItems, $parentId, $prefix, $space, &$options) {
$parentColumn = $this->getParentColumn();
$keyName = $this->getKeyName();
$titleColumn = $this->getTitleColumn();
if ($parentId == $node->$parentColumn) {
$node->$titleColumn = $prefix.$space.$node->$titleColumn;

$childrenPrefix = str_replace('', str_repeat($space, 6), $prefix).''.str_replace(['', $space], '', $prefix);

$children = $this->buildSelectOptions($menuId, null, $includeDisabledItems, $nodes, $node[$this->getKeyName()], $childrenPrefix);
$children = $this->buildSelectOptions($menuId, null, $includeDisabledItems, $nodes, $node->$keyName, $childrenPrefix);

$options[$node[$this->getKeyName()]] = $node[$this->getTitleColumn()];
$options[$node->$keyName] = $node->$titleColumn;

if ($children) {
$options += $children;
}
}
}
});

return $options;
}
Expand Down Expand Up @@ -337,4 +351,22 @@ protected static function boot()
return $branch;
});
}

protected function checkHasPermission($menuItem)
{
if (! $this->hasSpatiePermission) {
return true;
}
$roles = $menuItem->roles;

if ($roles->isEmpty()) {
return true;
}
$user = auth()->user();
if ($user) {
return $user->hasAnyRole($roles);
}

return false;
}
}
5 changes: 3 additions & 2 deletions src/Traits/spatiePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
if (class_exists(\Spatie\Permission\PermissionRegistrar::class)) {
trait spatiePermission
{
use \Spatie\Permission\Traits\HasRoles, \Spatie\Permission\Traits\HasPermissions;
use \Spatie\Permission\Traits\HasPermissions, \Spatie\Permission\Traits\HasRoles;

public $hasSpatiePermission = true;

protected $guard_name = 'web';
Expand All @@ -15,4 +16,4 @@ trait spatiePermission
{
public $hasSpatiePermission = false;
}
}
}

0 comments on commit 3d6407c

Please sign in to comment.