Skip to content

Commit dea80c6

Browse files
committed
removed usage of IUserStorage (BC break)
1 parent 263426e commit dea80c6

8 files changed

Lines changed: 16 additions & 244 deletions

File tree

src/Bridges/SecurityDI/SecurityExtension.php

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

81-
$builder->addDefinition($this->prefix('legacyUserStorage')) // deprecated
82-
->setType(Nette\Security\IUserStorage::class)
83-
->setFactory(Nette\Http\UserStorage::class);
84-
8581
$user = $builder->addDefinition($this->prefix('user'))
8682
->setFactory(Nette\Security\User::class);
8783

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 & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class User
5151
public array $onLoggedOut = [];
5252

5353
/** Session storage for current user */
54-
private UserStorage|IUserStorage $storage;
54+
private UserStorage $storage;
5555

5656
private ?IAuthenticator $authenticator;
5757

@@ -65,21 +65,17 @@ class User
6565

6666

6767
public function __construct(
68-
IUserStorage $legacyStorage = null,
68+
UserStorage $storage,
6969
IAuthenticator $authenticator = null,
7070
Authorizator $authorizator = null,
71-
UserStorage $storage = null,
7271
) {
73-
$this->storage = $storage ?? $legacyStorage; // back compatibility
74-
if (!$this->storage) {
75-
throw new Nette\InvalidStateException('UserStorage has not been set.');
76-
}
72+
$this->storage = $storage;
7773
$this->authenticator = $authenticator;
7874
$this->authorizator = $authorizator;
7975
}
8076

8177

82-
final public function getStorage(): UserStorage|IUserStorage
78+
final public function getStorage(): UserStorage
8379
{
8480
return $this->storage;
8581
}
@@ -108,13 +104,8 @@ public function login(string|IIdentity $user, string $password = null): void
108104
$id = $this->authenticator instanceof IdentityHandler
109105
? $this->authenticator->sleepIdentity($this->identity)
110106
: $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-
}
117107

108+
$this->storage->saveAuthentication($id);
118109
$this->authenticated = true;
119110
$this->logoutReason = null;
120111
Arrays::invoke($this->onLoggedIn, $this);
@@ -127,16 +118,7 @@ public function login(string|IIdentity $user, string $password = null): void
127118
final public function logout(bool $clearIdentity = false): void
128119
{
129120
$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-
121+
$this->storage->clearAuthentication($clearIdentity);
140122
$this->authenticated = false;
141123
$this->logoutReason = self::MANUAL;
142124
if ($logged) {
@@ -172,17 +154,11 @@ final public function getIdentity(): ?IIdentity
172154

173155
private function getStoredData(): void
174156
{
175-
if ($this->storage instanceof UserStorage) {
176-
(function (bool $state, ?IIdentity $id, ?int $reason) use (&$identity) {
177-
$identity = $id;
178-
$this->authenticated = $state;
179-
$this->logoutReason = $reason;
180-
})(...$this->storage->getState());
181-
} else {
182-
$identity = $this->storage->getIdentity();
183-
$this->authenticated = $this->storage->isAuthenticated();
184-
$this->logoutReason = $this->storage->getLogoutReason();
185-
}
157+
(function (bool $state, ?IIdentity $id, ?int $reason) use (&$identity) {
158+
$identity = $id;
159+
$this->authenticated = $state;
160+
$this->logoutReason = $reason;
161+
})(...$this->storage->getState());
186162

187163
$this->identity = $identity && $this->authenticator instanceof IdentityHandler
188164
? $this->authenticator->wakeupIdentity($identity)
@@ -248,12 +224,9 @@ final public function hasAuthenticator(): bool
248224
/**
249225
* Enables log out after inactivity (like '20 minutes').
250226
*/
251-
public function setExpiration(?string $expire, bool|int $clearIdentity = null)
227+
public function setExpiration(?string $expire, bool $clearIdentity = false)
252228
{
253-
$arg = $this->storage instanceof UserStorage
254-
? (bool) $clearIdentity
255-
: ($clearIdentity ? IUserStorage::CLEAR_IDENTITY : 0);
256-
$this->storage->setExpiration($expire, $arg);
229+
$this->storage->setExpiration($expire, $clearIdentity);
257230
return $this;
258231
}
259232

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)