Skip to content

Commit 9f326d4

Browse files
committed
Identity -> SimpleIdentity
Identity class must exist as an alias in order to read it from session
1 parent b3d847c commit 9f326d4

File tree

5 files changed

+140
-175
lines changed

5 files changed

+140
-175
lines changed

src/Security/Identity.php

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

src/Security/SimpleIdentity.php

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,105 @@
1212

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

src/compatibility-intf.php

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

src/compatibility.php

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

1212
if (false) {
13-
/** @deprecated use Nette\Security\Authorizator */
14-
interface IAuthorizator extends Authorizator
13+
/** @deprecated use Nette\Security\SimpleIdentity */
14+
class Identity extends SimpleIdentity
1515
{
1616
}
17-
} elseif (!interface_exists(IAuthorizator::class)) {
18-
class_alias(Authorizator::class, IAuthorizator::class);
19-
}
20-
21-
if (false) {
22-
/** @deprecated use Nette\Security\Resource */
23-
interface IResource extends Resource
24-
{
25-
}
26-
} elseif (!interface_exists(IResource::class)) {
27-
class_alias(Resource::class, IResource::class);
28-
}
29-
30-
if (false) {
31-
/** @deprecated use Nette\Security\Role */
32-
interface IRole extends Role
33-
{
34-
}
35-
} elseif (!interface_exists(IRole::class)) {
36-
class_alias(Role::class, IRole::class);
17+
} elseif (!class_exists(Identity::class)) {
18+
class_alias(SimpleIdentity::class, Identity::class);
3719
}

tests/Security/Identity.phpt

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

0 commit comments

Comments
 (0)