Skip to content

Commit d4385af

Browse files
committed
Identity -> SimpleIdentity
Identity class must exist as an alias in order to read it from session
1 parent 4562a18 commit d4385af

5 files changed

Lines changed: 149 additions & 182 deletions

File tree

src/Security/Identity.php

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

src/Security/SimpleIdentity.php

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,119 @@
77

88
namespace Nette\Security;
99

10+
use function in_array, is_float;
11+
1012

1113
/**
1214
* Default implementation of IIdentity.
15+
* @property string|int $id
16+
* @property list<string> $roles
17+
* @property array<string,mixed> $data
1318
*/
14-
class SimpleIdentity extends Identity
19+
class SimpleIdentity implements IIdentity
1520
{
21+
private string|int $id;
22+
23+
/** @var list<string> */
24+
private array $roles;
25+
26+
/** @var array<string, mixed> */
27+
private array $data;
28+
29+
30+
/**
31+
* @param string|list<string>|null $roles
32+
* @param ?iterable<string, mixed> $data
33+
*/
34+
public function __construct(string|int $id, string|array|null $roles = null, ?iterable $data = null)
35+
{
36+
$this->setId($id);
37+
$this->setRoles((array) $roles);
38+
$this->data = iterator_to_array($data ?? []);
39+
}
40+
41+
42+
/**
43+
* Sets the ID of user.
44+
*/
45+
public function setId(string|int $id): static
46+
{
47+
$this->id = is_numeric($id) && !is_float($tmp = $id * 1) ? $tmp : $id;
48+
return $this;
49+
}
50+
51+
52+
/**
53+
* Returns the ID of user.
54+
*/
55+
public function getId(): string|int
56+
{
57+
return $this->id;
58+
}
59+
60+
61+
/**
62+
* Sets a list of roles that the user is a member of.
63+
* @param list<string> $roles
64+
*/
65+
public function setRoles(array $roles): static
66+
{
67+
$this->roles = $roles;
68+
return $this;
69+
}
70+
71+
72+
/**
73+
* Returns a list of roles that the user is a member of.
74+
* @return list<string>
75+
*/
76+
public function getRoles(): array
77+
{
78+
return $this->roles;
79+
}
80+
81+
82+
/**
83+
* Returns user data.
84+
* @return array<string, mixed>
85+
*/
86+
public function getData(): array
87+
{
88+
return $this->data;
89+
}
90+
91+
92+
/**
93+
* Sets user data value.
94+
*/
95+
public function __set(string $key, mixed $value): void
96+
{
97+
match ($key) {
98+
'id' => $this->setId($value),
99+
'roles' => $this->setRoles($value),
100+
'data' => $this->data = $value,
101+
default => $this->data[$key] = $value,
102+
};
103+
}
104+
105+
106+
/**
107+
* Returns user data value.
108+
*/
109+
public function &__get(string $key): mixed
110+
{
111+
if (in_array($key, ['id', 'roles', 'data'], strict: true)) {
112+
$res = $this->{'get' . ucfirst($key)}();
113+
return $res;
114+
115+
} else {
116+
return $this->data[$key];
117+
}
118+
}
119+
120+
121+
public function __isset(string $key): bool
122+
{
123+
return isset($this->data[$key]) || in_array($key, ['id', 'roles', 'data'], strict: true);
124+
}
16125
}

src/compatibility-intf.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
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+
namespace Nette\Security;
9+
10+
if (false) {
11+
/** @deprecated use Nette\Security\Authorizator */
12+
interface IAuthorizator extends Authorizator
13+
{
14+
}
15+
} elseif (!interface_exists(IAuthorizator::class)) {
16+
class_alias(Authorizator::class, IAuthorizator::class);
17+
}
18+
19+
if (false) {
20+
/** @deprecated use Nette\Security\Resource */
21+
interface IResource extends Resource
22+
{
23+
}
24+
} elseif (!interface_exists(IResource::class)) {
25+
class_alias(Resource::class, IResource::class);
26+
}
27+
28+
if (false) {
29+
/** @deprecated use Nette\Security\Role */
30+
interface IRole extends Role
31+
{
32+
}
33+
} elseif (!interface_exists(IRole::class)) {
34+
class_alias(Role::class, IRole::class);
35+
}

src/compatibility.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,10 @@
88
namespace Nette\Security;
99

1010
if (false) {
11-
/** @deprecated use Nette\Security\Authorizator */
12-
interface IAuthorizator extends Authorizator
11+
/** @deprecated use Nette\Security\SimpleIdentity */
12+
class Identity extends SimpleIdentity
1313
{
1414
}
15-
} elseif (!interface_exists(IAuthorizator::class)) {
16-
class_alias(Authorizator::class, IAuthorizator::class);
17-
}
18-
19-
if (false) {
20-
/** @deprecated use Nette\Security\Resource */
21-
interface IResource extends Resource
22-
{
23-
}
24-
} elseif (!interface_exists(IResource::class)) {
25-
class_alias(Resource::class, IResource::class);
26-
}
27-
28-
if (false) {
29-
/** @deprecated use Nette\Security\Role */
30-
interface IRole extends Role
31-
{
32-
}
33-
} elseif (!interface_exists(IRole::class)) {
34-
class_alias(Role::class, IRole::class);
15+
} elseif (!class_exists(Identity::class)) {
16+
class_alias(SimpleIdentity::class, Identity::class);
3517
}

tests/Security/Identity.phpt

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

0 commit comments

Comments
 (0)