Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserPanel: Better design of "No identity." #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,29 @@ jobs:


- name: Nette Code Checker
php: 7.4
install:
- travis_retry composer create-project nette/code-checker temp/code-checker ^3 --no-progress
script:
- php temp/code-checker/code-checker --strict-types


- name: Nette Coding Standard
php: 7.4
install:
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress
script:
- php temp/coding-standard/ecs check src tests --config temp/coding-standard/coding-standard-php71.yml


- stage: Static Analysis (informative)
php: 7.4
script:
- composer run-script phpstan


- stage: Code Coverage
php: 7.4
script:
- vendor/bin/tester -p phpdbg tests -s --coverage ./coverage.xml --coverage-src ./src
after_script:
Expand Down
11 changes: 7 additions & 4 deletions src/Bridges/SecurityDI/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public function getConfigSchema(): Nette\Schema\Schema
'users' => Expect::arrayOf(
Expect::anyOf(
Expect::string(), // user => password
Expect::structure([ // user => password + roles
Expect::structure([ // user => password + roles + data
'password' => Expect::string(),
'roles' => Expect::anyOf(Expect::string(), Expect::listOf('string')),
'data' => Expect::array(),
])->castTo('array')
)
),
Expand All @@ -49,6 +50,7 @@ public function getConfigSchema(): Nette\Schema\Schema

public function loadConfiguration()
{
/** @var object{debugger: bool, users: array, roles: array, resources: array} $config */
$config = $this->config;
$builder = $this->getContainerBuilder();

Expand All @@ -69,17 +71,18 @@ public function loadConfiguration()
}

if ($config->users) {
$usersList = $usersRoles = [];
$usersList = $usersRoles = $usersData = [];
foreach ($config->users as $username => $data) {
$data = is_array($data) ? $data : ['password' => $data];
$this->validateConfig(['password' => null, 'roles' => null], $data, $this->prefix("security.users.$username"));
$this->validateConfig(['password' => null, 'roles' => null, 'data' => []], $data, $this->prefix("security.users.$username"));
$usersList[$username] = $data['password'];
$usersRoles[$username] = $data['roles'] ?? null;
$usersData[$username] = $data['data'] ?? [];
}

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

if ($this->name === 'security') {
$builder->addAlias('nette.authenticator', $this->prefix('authenticator'));
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/SecurityTracy/templates/UserPanel.panel.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ use Tracy\Dumper;
<div class="nette-UserPanel">
<h1><?php if ($user->isLoggedIn()): ?>Logged in<?php else: ?>Unlogged<?php endif ?></h1>

<?php if ($user->getIdentity()): echo Dumper::toHtml($user->getIdentity(), [Dumper::LIVE => true]); else: ?><p>no identity</p><?php endif ?>
<?php if ($user->getIdentity()): echo Dumper::toHtml($user->getIdentity(), [Dumper::LIVE => true]); else: ?><p style="text-align:center;padding:1em 0;color:#888">No&nbsp;identity.</p><?php endif ?>
</div>
9 changes: 7 additions & 2 deletions src/Security/SimpleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ class SimpleAuthenticator implements IAuthenticator
/** @var array */
private $usersRoles;

/** @var array */
private $usersData;


/**
* @param array $userlist list of pairs username => password
* @param array $usersRoles list of pairs username => role[]
* @param array $usersData list of pairs username => mixed[]
*/
public function __construct(array $userlist, array $usersRoles = [])
public function __construct(array $userlist, array $usersRoles = [], array $usersData = [])
{
$this->userlist = $userlist;
$this->usersRoles = $usersRoles;
$this->usersData = $usersData;
}


Expand All @@ -48,7 +53,7 @@ public function authenticate(array $credentials): IIdentity
foreach ($this->userlist as $name => $pass) {
if (strcasecmp($name, $username) === 0) {
if ((string) $pass === (string) $password) {
return new Identity($name, $this->usersRoles[$name] ?? null);
return new Identity($name, $this->usersRoles[$name] ?? null, $this->usersData[$name] ?? []);
} else {
throw new AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Security/SimpleAuthenticator.Data.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Test: Nette\Security\SimpleAuthenticator and data
*/

declare(strict_types=1);

use Nette\Security\SimpleAuthenticator;
use Tester\Assert;


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


$users = [
'john' => 'john123',
'admin' => 'admin123',
'user' => 'user123',
];
$usersData = [
'admin' => ['nick' => 'admin', 'email' => '[email protected]'],
'user' => ['nick' => 'user', 'email' => '[email protected]'],
];
$expectedData = [
'admin' => ['nick' => 'admin', 'email' => '[email protected]'],
'user' => ['nick' => 'user', 'email' => '[email protected]'],
'john' => [],
];

$authenticator = new SimpleAuthenticator($users, [], $usersData);

foreach ($users as $username => $password) {
$identity = $authenticator->authenticate([$username, $password]);
Assert::equal($username, $identity->getId());
Assert::equal($expectedData[$username], $identity->getData());
}