Skip to content

Commit 3ca7ffc

Browse files
MartkCzdg
authored andcommitted
SecurityExtensions, SimpleAuthenticator: add option for users data (#40)
1 parent 93bf4ac commit 3ca7ffc

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/Bridges/SecurityDI/SecurityExtension.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public function getConfigSchema(): Nette\Schema\Schema
3535
'users' => Expect::arrayOf(
3636
Expect::anyOf(
3737
Expect::string(), // user => password
38-
Expect::structure([ // user => password + roles
38+
Expect::structure([ // user => password + roles + data
3939
'password' => Expect::string(),
4040
'roles' => Expect::anyOf(Expect::string(), Expect::listOf('string')),
41+
'data' => Expect::array(),
4142
])->castTo('array')
4243
)
4344
),
@@ -69,17 +70,18 @@ public function loadConfiguration()
6970
}
7071

7172
if ($config->users) {
72-
$usersList = $usersRoles = [];
73+
$usersList = $usersRoles = $usersData = [];
7374
foreach ($config->users as $username => $data) {
7475
$data = is_array($data) ? $data : ['password' => $data];
75-
$this->validateConfig(['password' => null, 'roles' => null], $data, $this->prefix("security.users.$username"));
76+
$this->validateConfig(['password' => null, 'roles' => null, 'data' => []], $data, $this->prefix("security.users.$username"));
7677
$usersList[$username] = $data['password'];
7778
$usersRoles[$username] = $data['roles'] ?? null;
79+
$usersData[$username] = $data['data'] ?? [];
7880
}
7981

8082
$builder->addDefinition($this->prefix('authenticator'))
8183
->setType(Nette\Security\IAuthenticator::class)
82-
->setFactory(Nette\Security\SimpleAuthenticator::class, [$usersList, $usersRoles]);
84+
->setFactory(Nette\Security\SimpleAuthenticator::class, [$usersList, $usersRoles, $usersData]);
8385

8486
if ($this->name === 'security') {
8587
$builder->addAlias('nette.authenticator', $this->prefix('authenticator'));

src/Security/SimpleAuthenticator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ class SimpleAuthenticator implements IAuthenticator
2525
/** @var array */
2626
private $usersRoles;
2727

28+
/** @var array */
29+
private $usersData;
30+
2831

2932
/**
3033
* @param array $userlist list of pairs username => password
3134
* @param array $usersRoles list of pairs username => role[]
35+
* @param array $usersData list of pairs username => mixed[]
3236
*/
33-
public function __construct(array $userlist, array $usersRoles = [])
37+
public function __construct(array $userlist, array $usersRoles = [], array $usersData = [])
3438
{
3539
$this->userlist = $userlist;
3640
$this->usersRoles = $usersRoles;
41+
$this->usersData = $usersData;
3742
}
3843

3944

@@ -48,7 +53,7 @@ public function authenticate(array $credentials): IIdentity
4853
foreach ($this->userlist as $name => $pass) {
4954
if (strcasecmp($name, $username) === 0) {
5055
if ((string) $pass === (string) $password) {
51-
return new Identity($name, $this->usersRoles[$name] ?? null);
56+
return new Identity($name, $this->usersRoles[$name] ?? null, $this->usersData[$name] ?? []);
5257
} else {
5358
throw new AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
5459
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Security\SimpleAuthenticator and data
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Security\SimpleAuthenticator;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
$users = [
17+
'john' => 'john123',
18+
'admin' => 'admin123',
19+
'user' => 'user123',
20+
];
21+
$usersData = [
22+
'admin' => ['nick' => 'admin', 'email' => '[email protected]'],
23+
'user' => ['nick' => 'user', 'email' => '[email protected]'],
24+
];
25+
$expectedData = [
26+
'admin' => ['nick' => 'admin', 'email' => '[email protected]'],
27+
'user' => ['nick' => 'user', 'email' => '[email protected]'],
28+
'john' => [],
29+
];
30+
31+
$authenticator = new SimpleAuthenticator($users, [], $usersData);
32+
33+
foreach ($users as $username => $password) {
34+
$identity = $authenticator->authenticate([$username, $password]);
35+
Assert::equal($username, $identity->getId());
36+
Assert::equal($expectedData[$username], $identity->getData());
37+
}

0 commit comments

Comments
 (0)