0

Magento 2: ACL Permission for Admin Module

In

magento\app\code\Custom\Module\Controller\Adminhtml\Posts\Index.php

namespace Custom\Module\Controller\Adminhtml\Posts;

use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Framework\Controller\Result\ForwardFactory
     */
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
     */
    public function __construct(
    \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute() {
        $resultPage = $this->resultPageFactory->create();

        $this->_isAllowed();

        $resultPage->getConfig()->getTitle()->prepend(__('Posts'));

        return $resultPage;
    }

    /*
     * Check permission via ACL resource
     */

    public function _isAllowed() {
        return $this->_authorization->->isAllowed('Custom_Module::posts');
    }
}

While calling this it's giving error

Exception #0 (Exception): Notice: Undefined property: Custom\Module\Controller\Adminhtml\Posts\Index\Interceptor::$_authorization in D:\wamp\www\magento\app\code\Custom\Module\Controller\Adminhtml\Posts\Index.php on line 55

Why i'm doing manually, because my Module Permission is still not working. So for Authrization we have module-authorization Module. Would like to use that Manually in Controller/Block file. Ideal Controller.

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of\Magento\Framework\App\ObjectManager
$aclObject  = $_objectManager->get('Magento\Authorization\Model\Acl');

If i use above then it gives Class 'Magento\Authorization\Model\Acl' not found

1
  • Hi @fschmengler updated question
    – Jackson
    Dec 20, 2016 at 7:57

1 Answer 1

2

The controller does not extend Magento\Backend\App\AbstractAction, where _authorization is defined. In a non-admin controller you are not supposed to use the ACL.

About the second part of your question: I am not sure what exactly you want to do with $aclObject, but there really is no class Magento\Authorization\Model\Acl in Magento. Did you mean Magento\Framework\Acl ?

2
  • Yes. So instead of \Magento\Framework\App\Action\Action need to use Magento\Backend\App\AbstractAction then all works fine?
    – Jackson
    Dec 20, 2016 at 8:02
  • Theoretically yes. But I also saw your other question and could not find out what's wrong. Good luck! Dec 20, 2016 at 8:05

Your Answer

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

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