Skip to content

Commit b76f313

Browse files
committed
added SimpleIdentity, successor for Identity
1 parent ae75fb5 commit b76f313

File tree

6 files changed

+202
-9
lines changed

6 files changed

+202
-9
lines changed

src/Security/Identity.php

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

1414

1515
/**
16-
* Default implementation of IIdentity.
16+
* @deprecated use Nette\Security\SimpleIdentity
1717
*
1818
* @property mixed $id
1919
* @property array $roles

src/Security/SimpleAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function authenticate(array $credentials): IIdentity
5353
foreach ($this->userlist as $name => $pass) {
5454
if (strcasecmp($name, $username) === 0) {
5555
if ((string) $pass === (string) $password) {
56-
return new Identity($name, $this->usersRoles[$name] ?? null, $this->usersData[$name] ?? []);
56+
return new SimpleIdentity($name, $this->usersRoles[$name] ?? null, $this->usersData[$name] ?? []);
5757
} else {
5858
throw new AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
5959
}

src/Security/SimpleIdentity.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
use Nette;
13+
14+
15+
/**
16+
* Default implementation of IIdentity.
17+
*
18+
* @property mixed $id
19+
* @property array $roles
20+
* @property array $data
21+
*/
22+
class SimpleIdentity implements IIdentity, \Serializable
23+
{
24+
use Nette\SmartObject {
25+
__get as private parentGet;
26+
__set as private parentSet;
27+
__isset as private parentIsSet;
28+
}
29+
30+
/** @var mixed */
31+
private $id;
32+
33+
/** @var array */
34+
private $roles;
35+
36+
/** @var array */
37+
private $data;
38+
39+
40+
public function __construct($id, $roles = null, iterable $data = null)
41+
{
42+
$this->setId($id);
43+
$this->setRoles((array) $roles);
44+
$this->data = $data instanceof \Traversable
45+
? iterator_to_array($data)
46+
: (array) $data;
47+
}
48+
49+
50+
/**
51+
* Sets the ID of user.
52+
* @param string|int $id
53+
* @return static
54+
*/
55+
public function setId($id)
56+
{
57+
if (!is_string($id) && !is_int($id)) {
58+
throw new Nette\InvalidArgumentException('Identity identifier must be string|int, but type "' . gettype($id) . '" given.');
59+
}
60+
$this->id = $id;
61+
return $this;
62+
}
63+
64+
65+
/**
66+
* Returns the ID of user.
67+
* @return string|int
68+
*/
69+
public function getId()
70+
{
71+
return $this->id;
72+
}
73+
74+
75+
/**
76+
* Sets a list of roles that the user is a member of.
77+
* @return static
78+
*/
79+
public function setRoles(array $roles)
80+
{
81+
$this->roles = $roles;
82+
return $this;
83+
}
84+
85+
86+
/**
87+
* Returns a list of roles that the user is a member of.
88+
*/
89+
public function getRoles(): array
90+
{
91+
return $this->roles;
92+
}
93+
94+
95+
/**
96+
* Returns a user data.
97+
*/
98+
public function getData(): array
99+
{
100+
return $this->data;
101+
}
102+
103+
104+
/**
105+
* Sets user data value.
106+
*/
107+
public function __set(string $key, $value): void
108+
{
109+
if ($this->parentIsSet($key)) {
110+
$this->parentSet($key, $value);
111+
112+
} else {
113+
$this->data[$key] = $value;
114+
}
115+
}
116+
117+
118+
/**
119+
* Returns user data value.
120+
* @return mixed
121+
*/
122+
public function &__get(string $key)
123+
{
124+
if ($this->parentIsSet($key)) {
125+
return $this->parentGet($key);
126+
127+
} else {
128+
return $this->data[$key];
129+
}
130+
}
131+
132+
133+
public function __isset(string $key): bool
134+
{
135+
return isset($this->data[$key]) || $this->parentIsSet($key);
136+
}
137+
138+
139+
public function serialize(): string
140+
{
141+
return serialize([
142+
'id' => $this->id,
143+
'roles' => $this->roles,
144+
'data' => $this->data,
145+
]);
146+
}
147+
148+
149+
public function unserialize($serialized)
150+
{
151+
[
152+
'id' => $this->id,
153+
'roles' => $this->roles,
154+
'data' => $this->data,
155+
] = unserialize($serialized);
156+
}
157+
}

tests/Security/Identity.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test('', function () {
2626
});
2727

2828

29-
test('', function () {
29+
test('int to string conversion', function () {
3030
$id = new Identity('12');
3131
Assert::same(12, $id->getId());
3232

tests/Security/SimpleIdentity.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Security\SimpleIdentity.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Security\SimpleIdentity;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
test('', function () {
17+
$id = new SimpleIdentity(12, 'admin', ['name' => 'John']);
18+
19+
Assert::same(12, $id->getId());
20+
Assert::same(12, $id->id);
21+
Assert::same(['admin'], $id->getRoles());
22+
Assert::same(['admin'], $id->roles);
23+
Assert::same(['name' => 'John'], $id->getData());
24+
Assert::same(['name' => 'John'], $id->data);
25+
Assert::same('John', $id->name);
26+
});
27+
28+
29+
test('no type conversion', function () {
30+
$id = new SimpleIdentity('12');
31+
Assert::same('12', $id->getId());
32+
33+
34+
$id = new SimpleIdentity('12345678901234567890');
35+
Assert::same('12345678901234567890', $id->getId());
36+
});

tests/Security/User.authentication.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
declare(strict_types=1);
88

99
use Nette\Security\IAuthenticator;
10-
use Nette\Security\Identity;
10+
use Nette\Security\SimpleIdentity;
1111
use Tester\Assert;
1212

1313

@@ -31,7 +31,7 @@ class Authenticator implements IAuthenticator
3131
throw new Nette\Security\AuthenticationException('Password not match', self::INVALID_CREDENTIAL);
3232

3333
} else {
34-
return new Identity('John Doe', 'admin');
34+
return new SimpleIdentity('John Doe', 'admin');
3535
}
3636
}
3737
}
@@ -81,16 +81,16 @@ Assert::exception(function () use ($user) {
8181
$user->login('john', 'xxx');
8282
Assert::same(1, $counter->login);
8383
Assert::true($user->isLoggedIn());
84-
Assert::equal(new Identity('John Doe', 'admin'), $user->getIdentity());
84+
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());
8585
Assert::same('John Doe', $user->getId());
8686

8787
// login as john#3
8888
$user->logout(true);
8989
Assert::same(1, $counter->logout);
90-
$user->login(new Identity('John Doe', 'admin'));
90+
$user->login(new SimpleIdentity('John Doe', 'admin'));
9191
Assert::same(2, $counter->login);
9292
Assert::true($user->isLoggedIn());
93-
Assert::equal(new Identity('John Doe', 'admin'), $user->getIdentity());
93+
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());
9494

9595

9696
// log out
@@ -99,7 +99,7 @@ $user->logout(false);
9999
Assert::same(2, $counter->logout);
100100

101101
Assert::false($user->isLoggedIn());
102-
Assert::equal(new Identity('John Doe', 'admin'), $user->getIdentity());
102+
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());
103103

104104

105105
// logging out and clearing identity...

0 commit comments

Comments
 (0)