0

I created a custom module in magento 1.9 which is working fine from the admin part and when I give permission to another admin user to access my module it is visible on top menu but it is showing "Access denied" when I click the menu. I search through many sites but I did not get the right answer.

Points to be noted.

  1. Menu is coming when I give permission.
  2. I don't have _isAllowed() in my module controller. I already tried by creating _isAllowed() function and I feel it is useless.
  3. here I am mentioning another admin is nothing but a custom role and it is not a default admin role.

Please help me to fix this problem.

My Controller file

<?php
class Divum_Pricing_Adminhtml_PricingController extends Mage_Adminhtml_Controller_action
{
    protected function _initAction()
    {
        $this->loadLayout()->_setActiveMenu('pricing/pricing');
        return $this;
    }

    public function indexAction()
    {
        $this->_initAction()->renderLayout();
    }
    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('pricing/pricing');
    }

    public function updatemetalAction(){
        if ($data = $this->getRequest()->getPost()) {
            $key=1;
            foreach($this->GetAttributeValue('jew_metal') as $metals){
                if(trim($data[$metals['label']])==null || (float)$data[$metals['label']]==0){
                    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricing')->__('Form data must not null or "0"'));
                    $key=0;
                }else{
                    $resource = Mage::getSingleton('core/resource');
                    $writeConnection = $resource->getConnection('core_write');
                    $query = "UPDATE metal_price SET price = '".(float)$data[$metals['label']]."' WHERE metal_name ='".$metals['label']."'";
                    $writeConnection->query($query);
                }
            }

            if($key==1){
                $this->UpdateProductPrice();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('pricing')->__('All the Product Price are updated'.$metal));
            }else{
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricing')->__('Product Price is not updated : Please check the form for any error'));
            }

        }else{
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricing')->__('Form Data Missing'));
        }
        $this->_redirect('*/*/');
    }

    function GetAttributeValue($name) {
        $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
        $attributeId = $attributeInfo->getAttributeId();
        $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
        $attributeOptions = $attribute ->getSource()->getAllOptions(false);
        return $attributeOptions;
    }

    public function UpdateProductPrice(){
        $collection = Mage::getModel('catalog/product')->getCollection();
        foreach($collection as $product){
            $product = Mage::getModel('catalog/product')->load($product->getId());
            $attributes = $product->getAttributes();
            $value='0';
            $base_price=0;
            $weight=0;
            foreach ($attributes as $attribute) {
                $attributeCode = $attribute->getAttributeCode();
                if($attributeCode=='jew_metal'){
                    $value = $attribute->getFrontend()->getValue($product);
                }
                if($attributeCode=='jew_base_price'){
                    $base_price = (float)$attribute->getFrontend()->getValue($product);
                }
                if($attributeCode=='jew_metal_weight'){
                    $weight = (float)$attribute->getFrontend()->getValue($product);
                }
            }
            foreach($this->GetAttributeValue('jew_metal') as $metals){
                if($value==$metals['label']){
                    $metalPrice=$this->getMetalPrice($metals['label']);
                    $price=($weight*$metalPrice)+$base_price;
                    $product->setPrice($price)->save();
                }
            }
        }
    }

    public function getMetalPrice($metalname){
        $resource = Mage::getSingleton('core/resource');
        $readConnection = $resource->getConnection('core_read');
        $query = "SELECT price FROM metal_price WHERE metal_name = '".$metalname."'";
        return $readConnection->fetchOne($query);
    }
}

Above code is working fine... :) The mistake is in the _isAllowed() function... Thanks Friends.

  • show you controller file – MeenakshiSundaram R Sep 15 '15 at 9:38
  • please show adminhtml.xml and config.xml – Amit Bera Sep 15 '15 at 9:42
  • With the latest security update from Magento, it is essential for custom modules to have _isAllowed() method in controller files. – Prateek Sep 15 '15 at 9:44
  • Please add the code you have tried when adding the _isAllowed() method. This is probably the issue, although you are doubting that. Maybe there's something wrong in the way you have added it, so please add that code fragment to your question. – 7ochem Sep 15 '15 at 9:52
  • Thanks bro.. You are right :) i made a mistake in my _isAllowed() function.. Now it is working :) ,..... Thank You all :) – Arul James Sep 15 '15 at 9:59