RSS Feed

Blog

Hiding Admin Menu Items In OpenCart

Hiding Admin Menu Items In OpenCart
Posted on Sep 12, 2010 by Stephen Owens
Tutorial and Code category
   [ Return ]

How to hide the admin menu items in OpenCart from admin users that do not have viewing access privileges.

While doing a project in the open source eCommerce platform OpenCart, I noticed that admin menu items were still visible even if the admin user did not have viewing access privileges. The first stop, on open source issues like these, is to visit the community forums. After searching the OpenCart forums real quickly, I noticed this question about admin user permissions had been asked at least three times. So I decided to go ahead and write the code, and 15 minutes later I have a solution.

How to Hide the Admin Menu Items in OpenCart

The OpenCart eCommerce structure uses MVC (Model-View-Controller), so the menu is contained in the file header.tpl (a view file) as an unordered list. The fix was rather simple. Using the existing OpenCart function hasPermission() a check can be performed on each <li> to determine if the admin user has viewing (access) privileges. If the user has access permission then process the if statement to show the menu item. Otherwise skip it and move on.

To determine if a Parent menu item should be shown, a check for viewing access against all the children menu items is performed. For example, Products is a child of Catalog in the admin menu. Therefore, if Products, the child, should be shown, so should the parent menu item Catalog. If the user has viewing access to at least one of the children then the if statement is executed to show the parent menu item.

Example of the access checking code:

  1. if ($this->user->hasPermission('access', 'catalog/category')) {
    
  2. ...
    
  3. }

Download the Modified Header.tpl File

You can download a completed header.tpl file for the OpenCart open source eCommerce platform with all the modifications talked about above, from my post in the OpenCart Community forum.

http://forum.opencart.com/viewtopic.php?p=97682#p97682

Hopefully I will get around to submitting this to the core.

Share
Like what you read? Get updates for free!
comments powered by Disqus