Contao\User::authenticate PHP Method

authenticate() public method

Authenticate a user
public authenticate ( ) : boolean
return boolean True if the user could be authenticated
    public function authenticate()
    {
        // Check the cookie hash
        if ($this->strHash != $this->getSessionHash($this->strCookie)) {
            return false;
        }
        $objSession = $this->Database->prepare("SELECT * FROM tl_session WHERE hash=?")->execute($this->strHash);
        // Try to find the session in the database
        if ($objSession->numRows < 1) {
            return false;
        }
        $time = time();
        $container = \System::getContainer();
        $session = $container->get('session');
        // Validate the session
        if ($objSession->sessionID != $session->getId() || !$container->getParameter('contao.security.disable_ip_check') && $objSession->ip != $this->strIp || $objSession->hash != $this->strHash || $objSession->tstamp + \Config::get('sessionTimeout') < $time) {
            return false;
        }
        $this->intId = $objSession->pid;
        // Load the user object
        if ($this->findBy('id', $this->intId) == false) {
            return false;
        }
        $this->setUserFromDb();
        // Update session
        $this->Database->prepare("UPDATE tl_session SET tstamp={$time} WHERE hash=?")->execute($this->strHash);
        $this->setCookie($this->strCookie, $this->strHash, $time + \Config::get('sessionTimeout'), null, null, \Environment::get('ssl'), true);
        // HOOK: post authenticate callback
        if (isset($GLOBALS['TL_HOOKS']['postAuthenticate']) && is_array($GLOBALS['TL_HOOKS']['postAuthenticate'])) {
            foreach ($GLOBALS['TL_HOOKS']['postAuthenticate'] as $callback) {
                $this->import($callback[0], 'objAuth', true);
                $this->objAuth->{$callback[1]}($this);
            }
        }
        return true;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Constructor.
  *
  * @param User $user
  *
  * @throws UsernameNotFoundException
  */
 public function __construct(User $user)
 {
     if (!$user->authenticate()) {
         throw new UsernameNotFoundException('Invalid Contao user given.');
     }
     $this->setUser($user);
     $this->setAuthenticated(true);
     parent::__construct($this->getRolesFromUser($user));
 }
All Usage Examples Of Contao\User::authenticate