Question

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

Was it helpful?

Solution

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 ?

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top