Skip to content

Commit 18352fb

Browse files
committed
UserStorage: implements Nette\Security\UserStorage
1 parent 7f07a26 commit 18352fb

File tree

2 files changed

+103
-67
lines changed

2 files changed

+103
-67
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
"require-dev": {
2222
"nette/di": "^3.0",
2323
"nette/tester": "^2.0",
24-
"nette/security": "^3.0",
24+
"nette/security": "^3.1",
2525
"tracy/tracy": "^2.4",
2626
"phpstan/phpstan": "^0.12"
2727
},
2828
"conflict": {
2929
"nette/di": "<3.0.3",
30+
"nette/security": "<3.1",
3031
"nette/schema": "<1.1"
3132
},
3233
"suggest": {

src/Http/UserStorage.php

Lines changed: 101 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* Session storage for user object.
1818
*/
19-
class UserStorage implements Nette\Security\IUserStorage
19+
class UserStorage implements Nette\Security\UserStorage, Nette\Security\IUserStorage
2020
{
2121
use Nette\SmartObject;
2222

@@ -36,58 +36,57 @@ public function __construct(Session $sessionHandler)
3636
}
3737

3838

39-
/**
40-
* Sets the authenticated status of this user.
41-
* @return static
42-
*/
43-
public function setAuthenticated(bool $state)
39+
public function saveAuthentication(IIdentity $identity): void
4440
{
4541
$section = $this->getSessionSection(true);
46-
$section->authenticated = $state;
42+
$section->authenticated = true;
43+
$section->reason = null;
44+
$section->authTime = time(); // informative value
45+
$section->identity = $identity;
4746

4847
// Session Fixation defence
4948
$this->sessionHandler->regenerateId();
49+
}
5050

51-
if ($state) {
52-
$section->reason = null;
53-
$section->authTime = time(); // informative value
5451

55-
} else {
56-
$section->reason = self::MANUAL;
57-
$section->authTime = null;
58-
}
59-
return $this;
52+
public function clearAuthentication(bool $clearIdentity): void
53+
{
54+
$section = $this->getSessionSection(true);
55+
$section->authenticated = false;
56+
$section->reason = self::LOGOUT_MANUAL;
57+
$section->authTime = null;
58+
59+
// Session Fixation defence
60+
$this->sessionHandler->regenerateId();
6061
}
6162

6263

63-
/**
64-
* Is this user authenticated?
65-
*/
66-
public function isAuthenticated(): bool
64+
public function getState(): array
6765
{
6866
$session = $this->getSessionSection(false);
69-
return $session && $session->authenticated;
67+
return $session
68+
? [(bool) $session->authenticated, $session->identity, $session->reason]
69+
: [false, null, null];
7070
}
7171

7272

7373
/**
74-
* Sets the user identity.
75-
* @return static
74+
* Enables log out after inactivity.
7675
*/
77-
public function setIdentity(?IIdentity $identity)
76+
public function setExpiration(?string $time, /*bool*/ $clearIdentity = false): void
7877
{
79-
$this->getSessionSection(true)->identity = $identity;
80-
return $this;
81-
}
78+
$section = $this->getSessionSection(true);
79+
if ($time) {
80+
$time = Nette\Utils\DateTime::from($time)->format('U');
81+
$section->expireTime = $time;
82+
$section->expireDelta = $time - time();
8283

84+
} else {
85+
unset($section->expireTime, $section->expireDelta);
86+
}
8387

84-
/**
85-
* Returns current user identity, if any.
86-
*/
87-
public function getIdentity(): ?Nette\Security\IIdentity
88-
{
89-
$session = $this->getSessionSection(false);
90-
return $session ? $session->identity : null;
88+
$section->expireIdentity = (bool) $clearIdentity;
89+
$section->setExpiration($time, 'foo'); // time check
9190
}
9291

9392

@@ -114,38 +113,6 @@ public function getNamespace(): string
114113
}
115114

116115

117-
/**
118-
* Enables log out after inactivity. Accepts flag IUserStorage::CLEAR_IDENTITY.
119-
* @return static
120-
*/
121-
public function setExpiration(?string $time, int $flags = 0)
122-
{
123-
$section = $this->getSessionSection(true);
124-
if ($time) {
125-
$time = Nette\Utils\DateTime::from($time)->format('U');
126-
$section->expireTime = $time;
127-
$section->expireDelta = $time - time();
128-
129-
} else {
130-
unset($section->expireTime, $section->expireDelta);
131-
}
132-
133-
$section->expireIdentity = (bool) ($flags & self::CLEAR_IDENTITY);
134-
$section->setExpiration($time, 'foo'); // time check
135-
return $this;
136-
}
137-
138-
139-
/**
140-
* Why was user logged out?
141-
*/
142-
public function getLogoutReason(): ?int
143-
{
144-
$session = $this->getSessionSection(false);
145-
return $session ? $session->reason : null;
146-
}
147-
148-
149116
/**
150117
* Returns and initializes $this->sessionSection.
151118
*/
@@ -167,7 +134,7 @@ protected function getSessionSection(bool $need): ?SessionSection
167134

168135
if ($section->authenticated && $section->expireDelta > 0) { // check time expiration
169136
if ($section->expireTime < time()) {
170-
$section->reason = self::INACTIVITY;
137+
$section->reason = self::LOGOUT_INACTIVITY;
171138
$section->authenticated = false;
172139
if ($section->expireIdentity) {
173140
unset($section->identity);
@@ -182,4 +149,72 @@ protected function getSessionSection(bool $need): ?SessionSection
182149

183150
return $this->sessionSection;
184151
}
152+
153+
154+
/********************* legacy Nette\Security\IUserStorage ****************d*g**/
155+
156+
157+
/**
158+
* Sets the authenticated status of this user.
159+
* @return static
160+
*/
161+
public function setAuthenticated(bool $state)
162+
{
163+
$section = $this->getSessionSection(true);
164+
$section->authenticated = $state;
165+
166+
// Session Fixation defence
167+
$this->sessionHandler->regenerateId();
168+
169+
if ($state) {
170+
$section->reason = null;
171+
$section->authTime = time(); // informative value
172+
173+
} else {
174+
$section->reason = self::MANUAL;
175+
$section->authTime = null;
176+
}
177+
return $this;
178+
}
179+
180+
181+
/**
182+
* Is this user authenticated?
183+
*/
184+
public function isAuthenticated(): bool
185+
{
186+
$session = $this->getSessionSection(false);
187+
return $session && $session->authenticated;
188+
}
189+
190+
191+
/**
192+
* Sets the user identity.
193+
* @return static
194+
*/
195+
public function setIdentity(?IIdentity $identity)
196+
{
197+
$this->getSessionSection(true)->identity = $identity;
198+
return $this;
199+
}
200+
201+
202+
/**
203+
* Returns current user identity, if any.
204+
*/
205+
public function getIdentity(): ?Nette\Security\IIdentity
206+
{
207+
$session = $this->getSessionSection(false);
208+
return $session ? $session->identity : null;
209+
}
210+
211+
212+
/**
213+
* Why was user logged out?
214+
*/
215+
public function getLogoutReason(): ?int
216+
{
217+
$session = $this->getSessionSection(false);
218+
return $session ? $session->reason : null;
219+
}
185220
}

0 commit comments

Comments
 (0)