0

Is there a quick block of a code where given a user ID and a menu item ID, I could quickly check it the user is authorized to open that page?

So far I have this, but it feels like I'm jumping through too many hoops and might be missing something.

$access = JFactory::getUser(*id*)->getAuthorisedViewLevels();
$link = JFactory::getApplication()->getMenu()->getItem(*id*);
if (in_array($link->access, $access)) {
    // Has access
}

I check both the user and menu helpers, and I didn't see anything that looked like it did what I was looking for.

1 Answer 1

1

Yes, check both the menu exists and if the user has access:

use \Joomla\CMS\Factory;

if ($menuItem = Factory::getApplication()->getMenu()->getItem($id))
{
    if (in_array($menuItem->access, Factory::getUser()->getAuthorisedViewLevels()))
    {
        echo "User has access";
    }
    else
    {
        echo "User can NOT access";
    }
}
else
{
    echo "Menu item not found"
}
1
  • Kind of comes down to a more inline version of what I had. I was hoping there'd be something that didn't call as many resources.
    – Chaosxmk
    Commented Aug 20, 2020 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.