Skip to content

Commit 3184a5d

Browse files
committed
added UserStorage, successor for IUserStorage
1 parent 70fe8aa commit 3184a5d

File tree

6 files changed

+145
-38
lines changed

6 files changed

+145
-38
lines changed

src/Security/IUserStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
/**
14-
* Interface for persistent storage for user object data.
14+
* @deprecated use Nette\Security\UserStorage
1515
*/
1616
interface IUserStorage
1717
{

src/Security/User.php

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class User
4444
/** @var callable[] function (User $sender): void; Occurs when the user is logged out */
4545
public $onLoggedOut;
4646

47-
/** @var IUserStorage Session storage for current user */
47+
/** @var UserStorage|IUserStorage Session storage for current user */
4848
private $storage;
4949

5050
/** @var IAuthenticator|null */
@@ -64,17 +64,24 @@ class User
6464

6565

6666
public function __construct(
67-
IUserStorage $storage,
67+
UserStorage $storage = null,
6868
IAuthenticator $authenticator = null,
69-
Authorizator $authorizator = null
69+
Authorizator $authorizator = null,
70+
IUserStorage $legacyStorage = null
7071
) {
71-
$this->storage = $storage;
72+
$this->storage = $storage ?? $legacyStorage; // back compatibility
73+
if (!$this->storage) {
74+
throw new Nette\InvalidStateException('UserStorage has not been set.');
75+
}
7276
$this->authenticator = $authenticator;
7377
$this->authorizator = $authorizator;
7478
}
7579

7680

77-
final public function getStorage(): IUserStorage
81+
/**
82+
* @return UserStorage|IUserStorage
83+
*/
84+
final public function getStorage()
7885
{
7986
return $this->storage;
8087
}
@@ -97,8 +104,12 @@ public function login($user, string $password = null): void
97104
? $authenticator->authenticate($user, $password)
98105
: $authenticator->authenticate(func_get_args());
99106
}
100-
$this->storage->setIdentity($user);
101-
$this->storage->setAuthenticated(true);
107+
if ($this->storage instanceof UserStorage) {
108+
$this->storage->saveAuthentication($user);
109+
} else {
110+
$this->storage->setIdentity($user);
111+
$this->storage->setAuthenticated(true);
112+
}
102113
$this->identity = $user;
103114
$this->authenticated = true;
104115
$this->logoutReason = null;
@@ -113,14 +124,20 @@ final public function logout(bool $clearIdentity = false): void
113124
{
114125
if ($this->isLoggedIn()) {
115126
$this->onLoggedOut($this);
127+
}
128+
129+
if ($this->storage instanceof UserStorage) {
130+
$this->storage->clearAuthentication($clearIdentity);
131+
} else {
116132
$this->storage->setAuthenticated(false);
117-
$this->authenticated = false;
133+
if ($clearIdentity) {
134+
$this->storage->setIdentity(null);
135+
}
118136
$this->logoutReason = self::MANUAL;
119137
}
120-
if ($clearIdentity) {
121-
$this->storage->setIdentity(null);
122-
$this->identity = null;
123-
}
138+
139+
$this->authenticated = false;
140+
$this->identity = $clearIdentity ? null : $this->identity;
124141
}
125142

126143

@@ -150,9 +167,17 @@ final public function getIdentity(): ?IIdentity
150167

151168
private function getStoredData(): void
152169
{
153-
$this->authenticated = $this->storage->isAuthenticated();
154-
$this->identity = $this->storage->getIdentity();
155-
$this->logoutReason = $this->storage->getLogoutReason();
170+
if ($this->storage instanceof UserStorage) {
171+
(function (bool $state, ?IIdentity $identity, ?int $reason) {
172+
$this->authenticated = $state;
173+
$this->identity = $identity;
174+
$this->logoutReason = $reason;
175+
})(...$this->storage->getState());
176+
} else {
177+
$this->authenticated = $this->storage->isAuthenticated();
178+
$this->identity = $this->storage->getIdentity();
179+
$this->logoutReason = $this->storage->getLogoutReason();
180+
}
156181
}
157182

158183

@@ -235,7 +260,11 @@ public function setExpiration($expire, /*int*/$flags = 0)
235260
$clearIdentity = $clearIdentity || func_get_arg(2);
236261
trigger_error(__METHOD__ . '() third parameter is deprecated, use flag setExpiration($time, IUserStorage::CLEAR_IDENTITY)', E_USER_DEPRECATED);
237262
}
238-
$this->storage->setExpiration($expire, $clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
263+
264+
$arg = $this->storage instanceof UserStorage
265+
? $clearIdentity
266+
: ($clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
267+
$this->storage->setExpiration($expire, $arg);
239268
return $this;
240269
}
241270

src/Security/UserStorage.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Security;
11+
12+
13+
/**
14+
* Interface for persistent storage for user object data.
15+
*/
16+
interface UserStorage
17+
{
18+
/** Log-out reason */
19+
public const
20+
LOGOUT_MANUAL = 1,
21+
LOGOUT_INACTIVITY = 2;
22+
23+
/**
24+
* Sets the authenticated state of user.
25+
*/
26+
function saveAuthentication(IIdentity $identity): void;
27+
28+
/**
29+
* Removed authenticated state of user.
30+
*/
31+
function clearAuthentication(bool $clearIdentity): void;
32+
33+
/**
34+
* Returns user authenticated state, identity and logout reason.
35+
* @return array{bool, ?IIdentity, ?int}
36+
*/
37+
function getState(): array;
38+
39+
/**
40+
* Enables log out from the persistent storage after inactivity (like '20 minutes').
41+
*/
42+
function setExpiration(?string $expire, bool $clearIdentity): void;
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class MockUserStorage implements Nette\Security\IUserStorage
6+
{
7+
private $auth = false;
8+
9+
private $identity;
10+
11+
12+
public function setAuthenticated(bool $state)
13+
{
14+
$this->auth = $state;
15+
}
16+
17+
18+
public function isAuthenticated(): bool
19+
{
20+
return $this->auth;
21+
}
22+
23+
24+
public function setIdentity(Nette\Security\IIdentity $identity = null)
25+
{
26+
$this->identity = $identity;
27+
}
28+
29+
30+
public function getIdentity(): ?Nette\Security\IIdentity
31+
{
32+
return $this->identity;
33+
}
34+
35+
36+
public function setExpiration(?string $time, int $flags = 0)
37+
{
38+
}
39+
40+
41+
public function getLogoutReason(): ?int
42+
{
43+
return null;
44+
}
45+
}

tests/Security/MockUserStorage.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,34 @@
22

33
declare(strict_types=1);
44

5-
class MockUserStorage implements Nette\Security\IUserStorage
5+
class MockUserStorage implements Nette\Security\UserStorage
66
{
77
private $auth = false;
88

99
private $identity;
1010

1111

12-
public function setAuthenticated(bool $state)
13-
{
14-
$this->auth = $state;
15-
}
16-
17-
18-
public function isAuthenticated(): bool
19-
{
20-
return $this->auth;
21-
}
22-
23-
24-
public function setIdentity(Nette\Security\IIdentity $identity = null)
12+
public function saveAuthentication(Nette\Security\IIdentity $identity): void
2513
{
14+
$this->auth = true;
2615
$this->identity = $identity;
2716
}
2817

2918

30-
public function getIdentity(): ?Nette\Security\IIdentity
19+
public function clearAuthentication(bool $clearIdentity): void
3120
{
32-
return $this->identity;
21+
$this->auth = false;
22+
$this->identity = $clearIdentity ? null : $this->identity;
3323
}
3424

3525

36-
public function setExpiration(?string $time, int $flags = 0)
26+
public function getState(): array
3727
{
28+
return [$this->auth, $this->identity, null];
3829
}
3930

4031

41-
public function getLogoutReason(): ?int
32+
public function setExpiration(?string $expire, bool $clearIdentity): void
4233
{
43-
return null;
4434
}
4535
}

tests/Security/User.authentication.legacy.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use Tester\Assert;
1212

1313

1414
require __DIR__ . '/../bootstrap.php';
15-
require __DIR__ . '/MockUserStorage.php';
15+
require __DIR__ . '/MockUserStorage.legacy.php';
1616

1717
// Setup environment
1818
$_COOKIE = [];
@@ -37,7 +37,7 @@ class Authenticator implements IAuthenticator
3737
}
3838

3939

40-
$user = new Nette\Security\User(new MockUserStorage);
40+
$user = new Nette\Security\User(null, null, null, new MockUserStorage);
4141

4242
$counter = (object) [
4343
'login' => 0,

0 commit comments

Comments
 (0)