Skip to content

Commit

Permalink
Issue #1654 fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-webkul committed Nov 7, 2024
1 parent 108e423 commit d71cae7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,26 @@ public function store()
'password' => 'required',
]);

if (! auth()->guard('user')->attempt(request(['email', 'password']), request('remember'))) {
$user = auth()->guard('user');

if (! $user->attempt(request(['email', 'password']), request('remember'))) {
session()->flash('error', trans('admin::app.users.login-error'));

return redirect()->back();
}

if (auth()->guard('user')->user()->status == 0) {
if ($user->user()->status == 0) {
session()->flash('warning', trans('admin::app.users.activate-warning'));

auth()->guard('user')->logout();
$user->logout();

return redirect()->route('admin.session.create');
}

if ($user->user()->role->permission_type !== 'all') {
return redirect()->route(menu()->getItems('admin')->first()->getRoute());
}

return redirect()->intended(route('admin.dashboard.index'));
}

Expand Down
31 changes: 28 additions & 3 deletions packages/Webkul/Core/src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ public function getItems(?string $area = null, string $key = ''): Collection
$this->prepareMenuItems();
}

$items = collect($this->items)->sortBy(fn ($item) => $item->getPosition());

return $items;
return collect($this->removeUnauthorizedMenuItem())
->sortBy(fn ($item) => $item->getPosition());
}

/**
Expand Down Expand Up @@ -185,4 +184,30 @@ private function findMatchingItem($items, $currentKey): ?MenuItem

return null;
}

/**
* Remove unauthorized menu item.
*/
private function removeUnauthorizedMenuItem(): array
{
return collect($this->items)->map(function ($item) {
$this->removeChildrenUnauthorizedMenuItem($item);

return $item;
})->toArray();
}

/**
* Remove unauthorized menuItem's children. This will handle all levels.
*/
private function removeChildrenUnauthorizedMenuItem(MenuItem &$menuItem): void
{
if ($menuItem->haveChildren()) {
$firstChildrenItem = $menuItem->getChildren()->first();

$menuItem->setRoute($firstChildrenItem->getRoute());

$this->removeChildrenUnauthorizedMenuItem($firstChildrenItem);
}
}
}

0 comments on commit d71cae7

Please sign in to comment.