Admin User Restrictions - Hide Menu items 2

Discussion in 'Customization & add-ons' started by halmaclean, Jul 28, 2016.

  1. halmaclean

    halmaclean aMember Pro Customer

    Joined:
    Aug 30, 2013
    Messages:
    23
    Just as a follow up to the other thread, which is now closed, I thought I'd share an alternative method which may be of use for someone.

    We wanted to hide certain menu blocks from our admin users who were not superusers. We generally provide aMember to clients so they can manage access to online courses through a learning management system. The people we provide are sometimes not so very technically literate, and simplifying things is part of what we do for them.

    The problem was the help and support menu section in the control panel... our client admin users were accessing that to try to contact us, and had no clue they were in a different place. We therefore needed to hide it from those who are not super admins.

    We could hide the whole section using some php... but that hid it from us as well. So we first of all had to detect who was a super user and who was a 'lesser' admin user. We did that from the session information.

    Next we had to select the parts to hide - in fact we went a little further than we needed to, and did it a sub menu item at a time. This gives us more control in future, should we need it.

    We used Javascript, so it's very easy to do:


    <script src="<?php echo ROOT_SURL.'/application/default/views/public/js/jquery/jquery.js';?>"></script>
    <script type="text/javascript">

    $(document).ready(function (){
    <?php
    $superuser=$_SESSION['amember_admin_auth']['user']['super_user'];
    if($superuser=='0')
    {?>
    $('#menu-documentation').hide();
    $('#menu-help').hide();
    $('#menu-support').hide();
    $('#menu-report-bugs').hide();
    $('#menu-report-feature').hide();

    <?php
    }?>
    });
    </script>

    This was put into the site.php file. Note that the reference is to the class ID for the menu item... makes it simple to hide any part of it. It should also be possible to extend this to any other section in the menu, should you need to.

    Hope it helps :)
  2. halmaclean

    halmaclean aMember Pro Customer

    Joined:
    Aug 30, 2013
    Messages:
    23
    I need to update this!

    The code we used caused a small issue elsewhere as we output some content there. A better solution provided by Andrey:

    Am_Di::getInstance()->hook->add(Am_Event::BEFORE_RENDER, function(Am_Event $e){
    static $added = false;
    $v = $e->getView();
    if ($added ||
    !defined('AM_ADMIN') ||
    !AM_ADMIN ||
    !$e->getDi()->authAdmin->getUserId() ||
    $e->getDi()->authAdmin->getUser()->isSuper()) {

    return; //guard
    }
    $v->placeholder('body-finish')->append(<<<'CUT'
    <script type="text/javascript">
    $(document).ready(function (){
    $('#menu-documentation').hide();
    $('#menu-help').hide();
    $('#menu-support').hide();
    $('#menu-report-bugs').hide();
    $('#menu-report-feature').hide();
    });
    </script>
    CUT
    );
    });

    Many thanks for this!

Share This Page