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.
-
When your this custom controller called?Dhiren Vasoya– Dhiren Vasoya2023年12月01日 12:20:08 +00:00Commented Dec 1, 2023 at 12:20
1 Answer 1
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.
Explore related questions
See similar questions with these tags.