Skip to content

Commit

Permalink
renamed Identity -> SimpleIdentity
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 25, 2020
1 parent 6cadf83 commit d6dc5d1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Security/SimpleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,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, $this->usersData[$name] ?? []);
return new SimpleIdentity($name, $this->usersRoles[$name] ?? null, $this->usersData[$name] ?? []);
} else {
throw new AuthenticationException('Invalid password.', self::INVALID_CREDENTIAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @property array $roles
* @property array $data
*/
class Identity implements IIdentity
class SimpleIdentity implements IIdentity
{
use Nette\SmartObject {
__get as private parentGet;
Expand Down
6 changes: 6 additions & 0 deletions src/compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ class IRole
class IUserStorage
{
}

/** @deprecated use Nette\Security\SimpleIdentity */
class Identity
{
}
} elseif (!interface_exists(IAuthenticator::class)) {
class_alias(Authenticator::class, IAuthenticator::class);
class_alias(Authorizator::class, IAuthorizator::class);
class_alias(Resource::class, IResource::class);
class_alias(Role::class, IRole::class);
class_alias(UserStorage::class, IUserStorage::class);
class_alias(SimpleIdentity::class, Identity::class);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

/**
* Test: Nette\Security\Identity.
* Test: Nette\Security\SimpleIdentity.
*/

declare(strict_types=1);

use Nette\Security\Identity;
use Nette\Security\SimpleIdentity;
use Tester\Assert;


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


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

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


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


$id = new Identity('12345678901234567890');
$id = new SimpleIdentity('12345678901234567890');
Assert::same('12345678901234567890', $id->getId());
});
12 changes: 6 additions & 6 deletions tests/Security/User.authentication.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
declare(strict_types=1);

use Nette\Security\Authenticator as IAuthenticator;
use Nette\Security\Identity;
use Nette\Security\SimpleIdentity;
use Tester\Assert;


Expand All @@ -31,7 +31,7 @@ class Authenticator implements IAuthenticator
throw new Nette\Security\AuthenticationException('Password not match', self::INVALID_CREDENTIAL);

} else {
return new Identity('John Doe', 'admin');
return new SimpleIdentity('John Doe', 'admin');
}
}
}
Expand Down Expand Up @@ -81,16 +81,16 @@ Assert::exception(function () use ($user) {
$user->login('john', 'xxx');
Assert::same(1, $counter->login);
Assert::true($user->isLoggedIn());
Assert::equal(new Identity('John Doe', 'admin'), $user->getIdentity());
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());
Assert::same('John Doe', $user->getId());

// login as john#3
$user->logout(true);
Assert::same(1, $counter->logout);
$user->login(new Identity('John Doe', 'admin'));
$user->login(new SimpleIdentity('John Doe', 'admin'));
Assert::same(2, $counter->login);
Assert::true($user->isLoggedIn());
Assert::equal(new Identity('John Doe', 'admin'), $user->getIdentity());
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());


// log out
Expand All @@ -99,7 +99,7 @@ $user->logout(false);
Assert::same(2, $counter->logout);

Assert::false($user->isLoggedIn());
Assert::equal(new Identity('John Doe', 'admin'), $user->getIdentity());
Assert::equal(new SimpleIdentity('John Doe', 'admin'), $user->getIdentity());


// logging out and clearing identity...
Expand Down

0 comments on commit d6dc5d1

Please sign in to comment.