Contao\BackendUser::authenticate PHP Method

authenticate() public method

Redirect to the login screen if authentication fails
public authenticate ( ) : boolean
return boolean True if the user could be authenticated
    public function authenticate()
    {
        // Do not redirect if authentication is successful
        if (parent::authenticate()) {
            return true;
        }
        $route = \System::getContainer()->get('request_stack')->getCurrentRequest()->attributes->get('_route');
        if ($route == 'contao_backend_login') {
            return false;
        }
        $parameters = array();
        // Redirect to the last page visited upon login
        if ($route == 'contao_backend' || $route == 'contao_backend_preview') {
            $parameters['referer'] = base64_encode(\Environment::get('request'));
        }
        throw new RedirectResponseException(\System::getContainer()->get('router')->generate('contao_backend_login', $parameters, UrlGeneratorInterface::ABSOLUTE_URL));
    }

Usage Example

Esempio n. 1
0
 /**
  * Initialize the controller.
  *
  * The workflow is:
  * 1. Import user.
  * 2. Call parent constructor
  * 3. Authenticate user
  * 4. Load language files
  * DO NOT CHANGE THIS ORDER!
  */
 public function __construct()
 {
     $this->User = \BackendUser::getInstance();
     parent::__construct();
     $this->User->authenticate();
     $this->loadLanguageFile('default');
     $this->loadLanguageFile('modules');
 }