| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Http; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\Security\IIdentity; |
| 12 |
/** |
| 13 |
* @deprecated by \Packetery\Nette\Bridges\SecurityHttp\SessionStorage |
| 14 |
*/ |
| 15 |
class UserStorage implements \Packetery\Nette\Security\IUserStorage |
| 16 |
{ |
| 17 |
use \Packetery\Nette\SmartObject; |
| 18 |
/** @var string */ |
| 19 |
private $namespace = ''; |
| 20 |
/** @var Session */ |
| 21 |
private $sessionHandler; |
| 22 |
/** @var SessionSection */ |
| 23 |
private $sessionSection; |
| 24 |
public function __construct(Session $sessionHandler) |
| 25 |
{ |
| 26 |
$this->sessionHandler = $sessionHandler; |
| 27 |
} |
| 28 |
/** |
| 29 |
* Sets the authenticated status of this user. |
| 30 |
* @return static |
| 31 |
*/ |
| 32 |
public function setAuthenticated(bool $state) |
| 33 |
{ |
| 34 |
$section = $this->getSessionSection(\true); |
| 35 |
$section->authenticated = $state; |
| 36 |
// Session Fixation defence |
| 37 |
$this->sessionHandler->regenerateId(); |
| 38 |
if ($state) { |
| 39 |
$section->reason = null; |
| 40 |
$section->authTime = \time(); |
| 41 |
// informative value |
| 42 |
} else { |
| 43 |
$section->reason = self::MANUAL; |
| 44 |
$section->authTime = null; |
| 45 |
} |
| 46 |
return $this; |
| 47 |
} |
| 48 |
/** |
| 49 |
* Is this user authenticated? |
| 50 |
*/ |
| 51 |
public function isAuthenticated() : bool |
| 52 |
{ |
| 53 |
$session = $this->getSessionSection(\false); |
| 54 |
return $session && $session->authenticated; |
| 55 |
} |
| 56 |
/** |
| 57 |
* Sets the user identity. |
| 58 |
* @return static |
| 59 |
*/ |
| 60 |
public function setIdentity(?IIdentity $identity) |
| 61 |
{ |
| 62 |
$this->getSessionSection(\true)->identity = $identity; |
| 63 |
return $this; |
| 64 |
} |
| 65 |
/** |
| 66 |
* Returns current user identity, if any. |
| 67 |
*/ |
| 68 |
public function getIdentity() : ?Nette\Security\IIdentity |
| 69 |
{ |
| 70 |
$session = $this->getSessionSection(\false); |
| 71 |
return $session ? $session->identity : null; |
| 72 |
} |
| 73 |
/** |
| 74 |
* Changes namespace; allows more users to share a session. |
| 75 |
* @return static |
| 76 |
*/ |
| 77 |
public function setNamespace(string $namespace) |
| 78 |
{ |
| 79 |
if ($this->namespace !== $namespace) { |
| 80 |
$this->namespace = $namespace; |
| 81 |
$this->sessionSection = null; |
| 82 |
} |
| 83 |
return $this; |
| 84 |
} |
| 85 |
/** |
| 86 |
* Returns current namespace. |
| 87 |
*/ |
| 88 |
public function getNamespace() : string |
| 89 |
{ |
| 90 |
return $this->namespace; |
| 91 |
} |
| 92 |
/** |
| 93 |
* Enables log out after inactivity. Accepts flag IUserStorage::CLEAR_IDENTITY. |
| 94 |
* @return static |
| 95 |
*/ |
| 96 |
public function setExpiration(?string $time, int $flags = 0) |
| 97 |
{ |
| 98 |
$section = $this->getSessionSection(\true); |
| 99 |
if ($time) { |
| 100 |
$time = \Packetery\Nette\Utils\DateTime::from($time)->format('U'); |
| 101 |
$section->expireTime = $time; |
| 102 |
$section->expireDelta = $time - \time(); |
| 103 |
} else { |
| 104 |
unset($section->expireTime, $section->expireDelta); |
| 105 |
} |
| 106 |
$section->expireIdentity = (bool) ($flags & self::CLEAR_IDENTITY); |
| 107 |
$section->setExpiration($time, 'foo'); |
| 108 |
// time check |
| 109 |
return $this; |
| 110 |
} |
| 111 |
/** |
| 112 |
* Why was user logged out? |
| 113 |
*/ |
| 114 |
public function getLogoutReason() : ?int |
| 115 |
{ |
| 116 |
$session = $this->getSessionSection(\false); |
| 117 |
return $session ? $session->reason : null; |
| 118 |
} |
| 119 |
/** |
| 120 |
* Returns and initializes $this->sessionSection. |
| 121 |
*/ |
| 122 |
protected function getSessionSection(bool $need) : ?SessionSection |
| 123 |
{ |
| 124 |
if ($this->sessionSection !== null) { |
| 125 |
return $this->sessionSection; |
| 126 |
} |
| 127 |
if (!$need && !$this->sessionHandler->exists()) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
$this->sessionSection = $section = $this->sessionHandler->getSection('Nette.Http.UserStorage/' . $this->namespace); |
| 131 |
if (!$section->identity instanceof IIdentity || !\is_bool($section->authenticated)) { |
| 132 |
$section->remove(); |
| 133 |
} |
| 134 |
if ($section->authenticated && $section->expireDelta > 0) { |
| 135 |
// check time expiration |
| 136 |
if ($section->expireTime < \time()) { |
| 137 |
$section->reason = self::INACTIVITY; |
| 138 |
$section->authenticated = \false; |
| 139 |
if ($section->expireIdentity) { |
| 140 |
unset($section->identity); |
| 141 |
} |
| 142 |
} |
| 143 |
$section->expireTime = \time() + $section->expireDelta; |
| 144 |
// sliding expiration |
| 145 |
} |
| 146 |
if (!$section->authenticated) { |
| 147 |
unset($section->expireTime, $section->expireDelta, $section->expireIdentity, $section->authTime); |
| 148 |
} |
| 149 |
return $this->sessionSection; |
| 150 |
} |
| 151 |
} |
| 152 |
|