Skip to content

Instantly share code, notes, and snippets.

@enumag
Last active October 14, 2016 20:02
Show Gist options
  • Save enumag/4709159 to your computer and use it in GitHub Desktop.
Save enumag/4709159 to your computer and use it in GitHub Desktop.
<?php
namespace App;
class UserStorage extends \Nette\Http\UserStorage
{
/** Log-out reason */
const IDENTITY_CHANGED = 16;
/** @var \Model\UserRepository */
private $userRepository;
/**
* @param \Nette\Http\Session $session
* @param \Model\UserRepository $userRepository
*/
public function __construct(\Nette\Http\Session $session, \Model\UserRepository $userRepository)
{
parent::__construct($session);
$this->userRepository = $userRepository;
}
/**
* Checks if the identity is still valid.
* @param \Nette\Security\IIdentity $identity
* @return bool
*/
protected function isIdentityValid(\Nette\Security\IIdentity $identity)
{
$entity = $this->userRepository->get($identity->getId());
return $entity && $identity->identityHash === $entity->identityHash;
}
/**
* Returns and initializes $this->sessionSection.
* @param bool $need
* @return SessionSection
*/
protected function getSessionSection($need)
{
$section = parent::getSessionSection($need);
if ($section->authenticated && $section->identity instanceof \Nette\Security\IIdentity) {
if (!$this->isIdentityValid($section->identity)) {
$section->authenticated = FALSE;
$section->reason = self::IDENTITY_CHANGED;
if ($section->expireIdentity) {
unset($section->identity);
}
unset($section->expireTime, $section->expireDelta, $section->expireIdentity,
$section->expireBrowser, $section->browserCheck, $section->authTime);
}
}
return $section;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment