public function onException(KEventException $event)
 {
     $exception = $event->getException();
     if ($exception instanceof KHttpExceptionUnauthorized) {
         $request = $this->getObject('request');
         $response = $this->getObject('response');
         if ($request->getFormat() == 'html' && $request->isSafe()) {
             $message = $this->getObject('translator')->translate('You are not authorized to access this resource. Please login and try again.');
             if (JFactory::getApplication()->isSite()) {
                 $url = JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode((string) $request->getUrl()), false);
             } else {
                 $url = JRoute::_('index.php', false);
             }
             $response->setRedirect($url, $message, 'error');
             $response->send();
             $event->stopPropagation();
         }
     }
 }
Example #2
0
    /**
     * Redirects guest users to login form on 404 errors if the entity can be accessed without the viewlevel filter
     *
     * @param KEventException $event
     */
    public function onExceptionNotFound(KEventException $event)
    {
        if ($event->getException()->getCode() === KHttpResponse::NOT_FOUND && !$this->getObject('user')->isAuthentic()
            && $event->getException()->getMessage() !== 'File not found')
        {
            $model = $this->getController()->getModel();
            $model->setState(array('access' => null));

            if (!$model->fetch()->isNew())
            {
                $message = $this->getObject('translator')->translate('You are not authorized to access this resource. Please login and try again.');
                $url = JRoute::_('index.php?option=com_users&view=login&return='.base64_encode((string) $this->getRequest()->getUrl()), false);

                $this->getResponse()->setRedirect($url, $message, 'error');
                $this->getResponse()->send();

                $event->stopPropagation();
            }
        }
    }