Skip to content

Commit 799cc65

Browse files
committed
SimpleIdentity & Identity are not aliases due to an unserialization problem
1 parent a2d351f commit 799cc65

File tree

3 files changed

+160
-9
lines changed

3 files changed

+160
-9
lines changed

src/Security/Identity.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
* @deprecated use Nette\Security\SimpleIdentity
17+
*
18+
* @property mixed $id
19+
* @property array $roles
20+
* @property array $data
21+
*/
22+
class Identity implements IIdentity
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 = is_numeric($id) && !is_float($tmp = $id * 1) ? $tmp : $id;
61+
return $this;
62+
}
63+
64+
65+
/**
66+
* Returns the ID of user.
67+
* @return mixed
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+
}

src/Security/SimpleIdentity.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @property array $roles
2020
* @property array $data
2121
*/
22-
class SimpleIdentity implements IIdentity
22+
class SimpleIdentity implements IIdentity, \Serializable
2323
{
2424
use Nette\SmartObject {
2525
__get as private parentGet;
@@ -57,14 +57,14 @@ public function setId($id)
5757
if (!is_string($id) && !is_int($id)) {
5858
throw new Nette\InvalidArgumentException('Identity identifier must be string|int, but type "' . gettype($id) . '" given.');
5959
}
60-
$this->id = is_numeric($id) && !is_float($tmp = $id * 1) ? $tmp : $id;
60+
$this->id = $id;
6161
return $this;
6262
}
6363

6464

6565
/**
6666
* Returns the ID of user.
67-
* @return mixed
67+
* @return string|int
6868
*/
6969
public function getId()
7070
{
@@ -134,4 +134,24 @@ public function __isset(string $key): bool
134134
{
135135
return isset($this->data[$key]) || $this->parentIsSet($key);
136136
}
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(string $serialized): void
150+
{
151+
[
152+
'id' => $this->id,
153+
'roles' => $this->roles,
154+
'data' => $this->data,
155+
] = unserialize($serialized);
156+
}
137157
}

src/compatibility.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,10 @@ interface IRole
3434
interface IUserStorage
3535
{
3636
}
37-
38-
/** @deprecated use Nette\Security\SimpleIdentity */
39-
class Identity extends SimpleIdentity
40-
{
41-
}
4237
} elseif (!interface_exists(IAuthenticator::class)) {
4338
class_alias(Authenticator::class, IAuthenticator::class);
4439
class_alias(Authorizator::class, IAuthorizator::class);
4540
class_alias(Resource::class, IResource::class);
4641
class_alias(Role::class, IRole::class);
4742
class_alias(UserStorage::class, IUserStorage::class);
48-
class_alias(SimpleIdentity::class, Identity::class);
4943
}

0 commit comments

Comments
 (0)