Exemple #1
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();
            }
        }
    }
 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();
         }
     }
 }
 /**
  * Handles 404 errors gracefully after log outs
  *
  * If a user does not have access to the entity after logging out, they will be redirected to the homepage.
  *
  * @param KEventException $event
  * @return bool
  */
 public function onErrorAfterLogout(KEventException $event)
 {
     if ($event->getException()->getCode() === KHttpResponse::NOT_FOUND && JFactory::getApplication()->isSite()) {
         if (version_compare(JVERSION, '3.0', '<')) {
             $hash = JApplication::getHash('plgSystemLogout');
             // Watch out. Starts with lowercase p for 2.5
         } else {
             $hash = JApplicationHelper::getHash('PlgSystemLogout');
         }
         $app = JFactory::getApplication();
         if ($app->input->cookie->getString($hash, null)) {
             $app->enqueueMessage(JText::_('PLG_SYSTEM_LOGOUT_REDIRECT'));
             $app->redirect('index.php');
             return true;
         }
     }
 }