0

I am using below function to send 403 status and "access denied" content and redirect user to some other website or custom page if user's IP is not in list of allowed IP's (whitelist)

protected function denyAccess($observer)
{

    /** @var \Magento\Framework\App\Action\Action $action */
    $action = $observer->getControllerAction();
    /** @var \Magento\Framework\App\Response\Http $response */

    $response = $action->getResponse();
    $response->clearBody()->setStatusCode(\Magento\Framework\App\Response\Http::STATUS_CODE_403);
    $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);

    $redirectUrl = 'https://someotherwebsitestore.com';
    header("Location:".$redirectUrl);
    exit;
}
  • How do I redirect user from observer to some other page or website and keep on same website if IP is whitelisted?
  • Using header when IP is not allowed, the user still remains on website. Only when cache is cleared, user gets redirected?
2
  • Is the above code from some class? I mean is it internal of magento or outside?
    – Avesh Naik
    Aug 23, 2018 at 18:32
  • Is the Magento 2 code only. Aug 23, 2018 at 18:33

1 Answer 1

0
protected $resultRedirect;

 public function __construct(\Magento\Framework\Controller\ResultFactory $result){
     $this->resultRedirect = $result;
 }


protected function denyAccess($observer)
 {
  ...............
  ..............
   $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
    $resultRedirect->setUrl("www.google.com");

   return $resultRedirect;
}

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.