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

  • Hi @fschmengler updated question – Ankit Shah Dec 20 '16 at 7:57
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 ?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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