lib: Add PermittedMenuItemFilter ...
... for iterating over menu items the user is allowed to display refs #8720
This commit is contained in:
parent
7f010102f6
commit
e6a60e214c
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Web\Menu;
|
||||
|
||||
use RecursiveFilterIterator;
|
||||
use Icinga\Authentication\Manager;
|
||||
use Icinga\Web\Menu;
|
||||
|
||||
class PermittedMenuItemFilter extends RecursiveFilterIterator
|
||||
{
|
||||
/**
|
||||
* Accept menu items that are permitted to the user
|
||||
*
|
||||
* @return bool Whether the user has the required permission granted to display the menu item
|
||||
*/
|
||||
public function accept()
|
||||
{
|
||||
$item = $this->current();
|
||||
/** @var Menu $item */
|
||||
if (($permission = $item->getPermission()) !== null) {
|
||||
$auth = Manager::getInstance();
|
||||
if (! $auth->isAuthenticated()) {
|
||||
// Don't accept menu item because user is not authenticated and the menu item requires a permission
|
||||
return false;
|
||||
}
|
||||
if (! $auth->getUser()->can($permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Accept menu item if it does not require a permission
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue