0

In an Admin Controller Class, How do I check if the admin user is logged in, then redirect to the login page if not logged in, then return to the previous controller after login?

A cut down version of my script as follows.

use Magento\Framework\View\Result\Page;
use Magento\Framework\Controller\Result\Redirect;

class Index implements HttpGetActionInterface

{
    /* Constructor */
  
    public function execute(): Page|Redirect
    {
        if (!$this->_isAllowed()) {
            ### How and where to redirect here? ###
        }

        /* Do stuff here and load Index page */
    }
}

Not sure if "$this->_isAllowed()" is the correct method to use to check if an admin user is logged in?

Not sure where/how to redirect if user not logged in, or session has ended.

Advice on the correct Magento way to do this would be appreciated.

1
  • When your this custom controller called? Dec 1, 2023 at 12:20

1 Answer 1

0

In Magento 2, admins can only access pages after a successful login. The page access for admins is configured in the acl.xml file.

ACL.XML : https://developer.adobe.com/commerce/php/tutorials/backend/create-access-control-list-rule/

Method - 1 : You can restrict admin user access by using a constant variable

const ADMIN_RESOURCE = 'Vendor_MyModule::create'

Method - 2 : You can restrict admin user access by using the method below in your controller.

protected function _isAllowed()
{
 return $this->_authorization->isAllowed('Vendor_MyModule::create');
} 

For more information, please check the following URLs.

  1. https://www.mageplaza.com/devdocs/magento-2-acl-access-control-lists.html#step-3-check-acl-rule

  2. https://magento.stackexchange.com/a/320580/82670

Your Answer

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

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