Skip to content

Commit 28acac6

Browse files
committed
Identity -> SimpleIdentity
Identity class must exist as an alias in order to read it from session
1 parent 5b0b48e commit 28acac6

File tree

5 files changed

+152
-152
lines changed

5 files changed

+152
-152
lines changed

src/Security/Identity.php

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

src/Security/SimpleIdentity.php

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,115 @@
99

1010
namespace Nette\Security;
1111

12+
use Nette;
13+
1214

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

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?php
22

33
/**
4-
* Test: Nette\Security\Identity.
4+
* Test: Nette\Security\SimpleIdentity.
55
*/
66

77
declare(strict_types=1);
88

9-
use Nette\Security\Identity;
9+
use Nette\Security\SimpleIdentity;
1010
use Tester\Assert;
1111

1212

1313
require __DIR__ . '/../bootstrap.php';
1414

1515

1616
test('', function () {
17-
$id = new Identity(12, 'admin', ['name' => 'John']);
17+
$id = new SimpleIdentity(12, 'admin', ['name' => 'John']);
1818

1919
Assert::same(12, $id->getId());
2020
Assert::same(12, $id->id);
@@ -27,10 +27,10 @@ test('', function () {
2727

2828

2929
test('', function () {
30-
$id = new Identity('12');
30+
$id = new SimpleIdentity('12');
3131
Assert::same(12, $id->getId());
3232

3333

34-
$id = new Identity('12345678901234567890');
34+
$id = new SimpleIdentity('12345678901234567890');
3535
Assert::same('12345678901234567890', $id->getId());
3636
});

0 commit comments

Comments
 (0)