Skip to content

Commit 00a5336

Browse files
committed
removed usage of IUserStorage (BC break)
1 parent 2b5e2a5 commit 00a5336

File tree

8 files changed

+16
-245
lines changed

8 files changed

+16
-245
lines changed

src/Bridges/SecurityDI/SecurityExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ public function loadConfiguration()
8080
$storage->addSetup('setCookieParameters', [$auth->cookieName, $auth->cookieDomain, $auth->cookieSamesite]);
8181
}
8282

83-
$builder->addDefinition($this->prefix('legacyUserStorage')) // deprecated
84-
->setType(Nette\Security\IUserStorage::class)
85-
->setFactory(Nette\Http\UserStorage::class);
86-
8783
$user = $builder->addDefinition($this->prefix('user'))
8884
->setFactory(Nette\Security\User::class);
8985

src/Security/IUserStorage.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,5 @@ interface IUserStorage
2121
INACTIVITY = 0b0010;
2222

2323
/** Log-out behavior */
24-
public const CLEAR_IDENTITY = 0b1000;
25-
26-
/**
27-
* Sets the authenticated status of this user.
28-
* @return static
29-
*/
30-
function setAuthenticated(bool $state);
31-
32-
/**
33-
* Is this user authenticated?
34-
*/
35-
function isAuthenticated(): bool;
36-
37-
/**
38-
* Sets the user identity.
39-
* @return static
40-
*/
41-
function setIdentity(?IIdentity $identity);
42-
43-
/**
44-
* Returns current user identity, if any.
45-
*/
46-
function getIdentity(): ?IIdentity;
47-
48-
/**
49-
* Enables log out from the persistent storage after inactivity (like '20 minutes'). Accepts flag IUserStorage::CLEAR_IDENTITY.
50-
* @return static
51-
*/
52-
function setExpiration(?string $expire, int $flags = 0);
53-
54-
/**
55-
* Why was user logged out?
56-
*/
57-
function getLogoutReason(): ?int;
24+
public const CLEAR_IDENTITY = true;
5825
}

src/Security/User.php

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class User
5555
public iterable $onLoggedOut = [];
5656

5757
/** Session storage for current user */
58-
private UserStorage|IUserStorage $storage;
58+
private UserStorage $storage;
5959
private ?IAuthenticator $authenticator;
6060
private ?Authorizator $authorizator;
6161
private ?IIdentity $identity = null;
@@ -64,22 +64,17 @@ class User
6464

6565

6666
public function __construct(
67-
?IUserStorage $legacyStorage = null,
67+
UserStorage $storage,
6868
?IAuthenticator $authenticator = null,
6969
?Authorizator $authorizator = null,
70-
?UserStorage $storage = null,
7170
) {
72-
$this->storage = $storage ?? $legacyStorage; // back compatibility
73-
if (!$this->storage) {
74-
throw new Nette\InvalidStateException('UserStorage has not been set.');
75-
}
76-
71+
$this->storage = $storage;
7772
$this->authenticator = $authenticator;
7873
$this->authorizator = $authorizator;
7974
}
8075

8176

82-
final public function getStorage(): UserStorage|IUserStorage
77+
final public function getStorage(): UserStorage
8378
{
8479
return $this->storage;
8580
}
@@ -108,13 +103,8 @@ public function login(string|IIdentity $user, ?string $password = null): void
108103
$id = $this->authenticator instanceof IdentityHandler
109104
? $this->authenticator->sleepIdentity($this->identity)
110105
: $this->identity;
111-
if ($this->storage instanceof UserStorage) {
112-
$this->storage->saveAuthentication($id);
113-
} else {
114-
$this->storage->setIdentity($id);
115-
$this->storage->setAuthenticated(true);
116-
}
117106

107+
$this->storage->saveAuthentication($id);
118108
$this->authenticated = true;
119109
$this->logoutReason = null;
120110
Arrays::invoke($this->onLoggedIn, $this);
@@ -127,16 +117,7 @@ public function login(string|IIdentity $user, ?string $password = null): void
127117
final public function logout(bool $clearIdentity = false): void
128118
{
129119
$logged = $this->isLoggedIn();
130-
131-
if ($this->storage instanceof UserStorage) {
132-
$this->storage->clearAuthentication($clearIdentity);
133-
} else {
134-
$this->storage->setAuthenticated(false);
135-
if ($clearIdentity) {
136-
$this->storage->setIdentity(null);
137-
}
138-
}
139-
120+
$this->storage->clearAuthentication($clearIdentity);
140121
$this->authenticated = false;
141122
$this->logoutReason = self::MANUAL;
142123
if ($logged) {
@@ -175,17 +156,11 @@ final public function getIdentity(): ?IIdentity
175156

176157
private function getStoredData(): void
177158
{
178-
if ($this->storage instanceof UserStorage) {
179-
(function (bool $state, ?IIdentity $id, ?int $reason) use (&$identity) {
180-
$identity = $id;
181-
$this->authenticated = $state;
182-
$this->logoutReason = $reason;
183-
})(...$this->storage->getState());
184-
} else {
185-
$identity = $this->storage->getIdentity();
186-
$this->authenticated = $this->storage->isAuthenticated();
187-
$this->logoutReason = $this->storage->getLogoutReason();
188-
}
159+
(function (bool $state, ?IIdentity $id, ?int $reason) use (&$identity) {
160+
$identity = $id;
161+
$this->authenticated = $state;
162+
$this->logoutReason = $reason;
163+
})(...$this->storage->getState());
189164

190165
$this->identity = $identity && $this->authenticator instanceof IdentityHandler
191166
? $this->authenticator->wakeupIdentity($identity)
@@ -252,12 +227,9 @@ final public function hasAuthenticator(): bool
252227
/**
253228
* Enables log out after inactivity (like '20 minutes').
254229
*/
255-
public function setExpiration(?string $expire, bool|int|null $clearIdentity = null)
230+
public function setExpiration(?string $expire, bool $clearIdentity = false)
256231
{
257-
$arg = $this->storage instanceof UserStorage
258-
? (bool) $clearIdentity
259-
: ($clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
260-
$this->storage->setExpiration($expire, $arg);
232+
$this->storage->setExpiration($expire, $clearIdentity);
261233
return $this;
262234
}
263235

tests/Security.DI/SecurityExtension.user.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ eval($compiler->compile());
2525
$container = new Container;
2626

2727
Assert::type(Nette\Bridges\SecurityHttp\SessionStorage::class, $container->getService('security.userStorage'));
28-
Assert::type(Nette\Http\UserStorage::class, $container->getService('security.legacyUserStorage'));
2928
Assert::type(Nette\Security\User::class, $container->getService('security.user'));
3029

3130
// aliases

tests/Security/MockUserStorage.legacy.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/Security/User.authentication.legacy.phpt

Lines changed: 0 additions & 117 deletions
This file was deleted.

tests/Security/User.authentication.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Authenticator implements Nette\Security\Authenticator
3636
}
3737

3838

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

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

tests/Security/User.authorization.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TesterRole implements Role
5454
}
5555
}
5656

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

5959
// guest
6060
Assert::false($user->isLoggedIn());

0 commit comments

Comments
 (0)