Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit a732243

Browse files
committed
Changed DefaultUser to immutable and added UserInterfaceFactory
1 parent 0426703 commit a732243

11 files changed

+107
-89
lines changed

src/ConfigProvider.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ public function getDependencies() : array
5959
// AuthenticationInterface::class => Basic\BasicAccess::class,
6060
// Provide an alias for the UserRepository adapter based on your application needs.
6161
// UserRepositoryInterface::class => UserRepository\Htpasswd::class
62-
UserInterface::class => DefaultUser::class
6362
],
6463
'factories' => [
6564
AuthenticationMiddleware::class => AuthenticationMiddlewareFactory::class,
6665
UserRepository\Htpasswd::class => UserRepository\HtpasswdFactory::class,
67-
UserRepository\PdoDatabase::class => UserRepository\PdoDatabaseFactory::class
68-
],
69-
'invokables' => [
70-
DefaultUser::class => DefaultUser::class
66+
UserRepository\PdoDatabase::class => UserRepository\PdoDatabaseFactory::class,
67+
UserInterface::class => UserInterfaceFactory::class
7168
]
7269
];
7370
}

src/DefaultUser.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication/blob/master/LICENSE.md New BSD License
66
*/
77

88
declare(strict_types=1);
99

1010
namespace Zend\Expressive\Authentication;
1111

12-
class DefaultUser implements UserInterface
12+
final class DefaultUser implements UserInterface
1313
{
14-
protected $identity;
14+
private $identity;
1515

16-
protected $roles = [];
16+
private $roles;
1717

18-
public function setIdentity(string $identity) : void
18+
public function __construct(string $identity, array $roles = [])
1919
{
2020
$this->identity = $identity;
21+
$this->roles = $roles;
2122
}
2223

2324
public function getIdentity() : string
2425
{
2526
return $this->identity;
2627
}
2728

28-
public function setRoles(array $roles) : void
29-
{
30-
$this->roles = $roles;
31-
}
32-
3329
public function getRoles() : array
3430
{
3531
return $this->roles;

src/UserInterface.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,11 @@
1111

1212
interface UserInterface
1313
{
14-
/**
15-
* Set the user's identity
16-
*/
17-
public function setIdentity(string $identity) : void;
18-
1914
/**
2015
* Get the unique user identity (id, username, email address or ...)
2116
*/
2217
public function getIdentity() : string;
2318

24-
/**
25-
* Set the user's roles
26-
*/
27-
public function setRoles(array $roles) : void;
28-
2919
/**
3020
* Get all user roles
3121
*

src/UserInterfaceFactory.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-expressive-authentication/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zend\Expressive\Authentication;
11+
12+
use Psr\Container\ContainerInterface;
13+
14+
class UserInterfaceFactory
15+
{
16+
public function generate(string $identity, array $roles = []) : UserInterface
17+
{
18+
return new DefaultUser($identity, $roles);
19+
}
20+
}

src/UserRepository/Htpasswd.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Zend\Expressive\Authentication\Exception;
1313
use Zend\Expressive\Authentication\UserInterface;
14+
use Zend\Expressive\Authentication\UserInterfaceFactory;
1415
use Zend\Expressive\Authentication\UserRepositoryInterface;
1516

1617
/**
@@ -26,14 +27,14 @@ class Htpasswd implements UserRepositoryInterface
2627
private $filename;
2728

2829
/**
29-
* @var UserInterface
30+
* @var UserInterfaceFactory
3031
*/
31-
private $user;
32+
private $userFactory;
3233

3334
/**
3435
* @throws Exception\InvalidConfigException
3536
*/
36-
public function __construct(string $filename, UserInterface $user)
37+
public function __construct(string $filename, UserInterfaceFactory $userFactory)
3738
{
3839
if (! file_exists($filename)) {
3940
throw new Exception\InvalidConfigException(sprintf(
@@ -42,7 +43,7 @@ public function __construct(string $filename, UserInterface $user)
4243
));
4344
}
4445
$this->filename = $filename;
45-
$this->user = $user;
46+
$this->userFactory = $userFactory;
4647
}
4748

4849
/**
@@ -65,10 +66,8 @@ public function authenticate(string $credential, string $password = null) : ?Use
6566
}
6667
fclose($handle);
6768

68-
if ($found && password_verify($password === null ? '' : $password, $hash)) {
69-
$this->user->setIdentity($credential);
70-
$this->user->setRoles($this->getRolesFromUser($credential));
71-
return $this->user;
69+
if ($found && password_verify($password ?? '', $hash)) {
70+
return $this->userFactory->generate($credential, $this->getRolesFromUser($credential));
7271
}
7372
return null;
7473
}

src/UserRepository/PdoDatabase.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PDOException;
1414
use Zend\Expressive\Authentication\Exception;
1515
use Zend\Expressive\Authentication\UserInterface;
16+
use Zend\Expressive\Authentication\UserInterfaceFactory;
1617
use Zend\Expressive\Authentication\UserRepositoryInterface;
1718

1819
/**
@@ -32,15 +33,18 @@ class PdoDatabase implements UserRepositoryInterface
3233
private $config;
3334

3435
/**
35-
* @var UserInterface
36+
* @var UserInterfaceFactory
3637
*/
37-
private $user;
38+
private $userFactory;
3839

39-
public function __construct(PDO $pdo, array $config, UserInterface $user)
40-
{
40+
public function __construct(
41+
PDO $pdo,
42+
array $config,
43+
UserInterfaceFactory $userFactory
44+
) {
4145
$this->pdo = $pdo;
4246
$this->config = $config;
43-
$this->user = $user;
47+
$this->userFactory = $userFactory;
4448
}
4549

4650
/**
@@ -71,9 +75,10 @@ public function authenticate(string $credential, string $password = null) : ?Use
7175
}
7276

7377
if (password_verify($password, $result->{$this->config['field']['password']})) {
74-
$this->user->setIdentity($credential);
75-
$this->user->setRoles($this->getRolesFromUser($credential));
76-
return $this->user;
78+
return $this->userFactory->generate(
79+
$credential,
80+
$this->getRolesFromUser($credential)
81+
);
7782
}
7883
return null;
7984
}

test/DefaultUserTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,19 @@ class DefaultUserTest extends TestCase
1717
{
1818
public function testConstructor()
1919
{
20-
$user = new DefaultUser();
20+
$user = new DefaultUser('foo');
2121
$this->assertInstanceOf(UserInterface::class, $user);
2222
}
2323

24-
public function testSetGetIdentity()
24+
public function testGetIdentity()
2525
{
26-
$user = new DefaultUser();
27-
$user->setIdentity('foo');
26+
$user = new DefaultUser('foo');
2827
$this->assertEquals('foo', $user->getIdentity());
2928
}
3029

31-
public function testSetGetRoles()
30+
public function testGetRoles()
3231
{
33-
$user = new DefaultUser();
34-
$user->setRoles(['foo', 'bar']);
32+
$user = new DefaultUser('foo', ['foo', 'bar']);
3533
$this->assertEquals(['foo', 'bar'], $user->getRoles());
3634
}
3735
}

test/UserRepository/HtpasswdFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Psr\Container\ContainerInterface;
1414
use Zend\Expressive\Authentication\Exception\InvalidConfigException;
1515
use Zend\Expressive\Authentication\UserInterface;
16+
use Zend\Expressive\Authentication\UserInterfaceFactory;
1617
use Zend\Expressive\Authentication\UserRepository\Htpasswd;
1718
use Zend\Expressive\Authentication\UserRepository\HtpasswdFactory;
1819

@@ -21,7 +22,7 @@ class HtpasswdFactoryTest extends TestCase
2122
protected function setUp()
2223
{
2324
$this->container = $this->prophesize(ContainerInterface::class);
24-
$this->user = $this->prophesize(UserInterface::class);
25+
$this->user = $this->prophesize(UserInterfaceFactory::class);
2526
$this->factory = new HtpasswdFactory();
2627
}
2728

test/UserRepository/HtpasswdTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@
1212
use PHPUnit\Framework\TestCase;
1313
use Prophecy\Argument;
1414
use Zend\Expressive\Authentication\UserInterface;
15+
use Zend\Expressive\Authentication\UserInterfaceFactory;
1516
use Zend\Expressive\Authentication\UserRepositoryInterface;
1617
use Zend\Expressive\Authentication\UserRepository\Htpasswd;
1718

1819
class HtpasswdTest extends TestCase
1920
{
21+
const EXAMPLE_IDENTITY = 'test';
22+
2023
protected function setUp()
2124
{
22-
$user = $this->prophesize(UserInterface::class);
23-
$user->setIdentity(Argument::type('string'))->will(function ($args) use ($user) {
24-
$user->getIdentity()->willReturn($args[0]);
25-
});
26-
$user->setRoles(Argument::type('array'))->will(function ($args) use ($user) {
27-
$user->getRoles()->willReturn($args[0]);
28-
});
29-
$this->user = $user;
25+
$this->user = $this->prophesize(UserInterface::class);
26+
$this->user->getIdentity()->willReturn(self::EXAMPLE_IDENTITY);
27+
$this->userFactory = $this->prophesize(UserInterfaceFactory::class);
28+
$this->userFactory->generate(Argument::type('string'), Argument::type('array'))
29+
->willReturn($this->user->reveal());
3030
}
3131
/**
3232
* @expectedException Zend\Expressive\Authentication\Exception\InvalidConfigException
3333
*/
3434
public function testConstructorWithNoFile()
3535
{
36-
$htpasswd = new Htpasswd('foo', $this->user->reveal());
36+
$htpasswd = new Htpasswd('foo', $this->userFactory->reveal());
3737
}
3838

3939
public function testConstructor()
4040
{
4141
$htpasswd = new Htpasswd(
4242
__DIR__ . '/../TestAssets/htpasswd',
43-
$this->user->reveal()
43+
$this->userFactory->reveal()
4444
);
4545
$this->assertInstanceOf(UserRepositoryInterface::class, $htpasswd);
4646
}
@@ -49,30 +49,30 @@ public function testAuthenticate()
4949
{
5050
$htpasswd = new Htpasswd(
5151
__DIR__ . '/../TestAssets/htpasswd',
52-
$this->user->reveal()
52+
$this->userFactory->reveal()
5353
);
5454

55-
$user = $htpasswd->authenticate('test', 'password');
55+
$user = $htpasswd->authenticate(self::EXAMPLE_IDENTITY, 'password');
5656
$this->assertInstanceOf(UserInterface::class, $user);
57-
$this->assertEquals('test', $user->getIdentity());
57+
$this->assertEquals(self::EXAMPLE_IDENTITY, $user->getIdentity());
5858
}
5959

6060
public function testAuthenticateInvalidUser()
6161
{
6262
$htpasswd = new Htpasswd(
6363
__DIR__ . '/../TestAssets/htpasswd',
64-
$this->user->reveal()
64+
$this->userFactory->reveal()
6565
);
66-
$this->assertNull($htpasswd->authenticate('test', 'foo'));
66+
$this->assertNull($htpasswd->authenticate(self::EXAMPLE_IDENTITY, 'foo'));
6767
}
6868

6969
public function testAuthenticateWithoutPassword()
7070
{
7171
$htpasswd = new Htpasswd(
7272
__DIR__ . '/../TestAssets/htpasswd',
73-
$this->user->reveal()
73+
$this->userFactory->reveal()
7474
);
75-
$this->assertNull($htpasswd->authenticate('test', null));
75+
$this->assertNull($htpasswd->authenticate(self::EXAMPLE_IDENTITY, null));
7676
}
7777

7878
/**
@@ -82,8 +82,8 @@ public function testAuthenticateWithInsecureHash()
8282
{
8383
$htpasswd = new Htpasswd(
8484
__DIR__ . '/../TestAssets/htpasswd_insecure',
85-
$this->user->reveal()
85+
$this->userFactory->reveal()
8686
);
87-
$htpasswd->authenticate('test', 'password');
87+
$htpasswd->authenticate(self::EXAMPLE_IDENTITY, 'password');
8888
}
8989
}

test/UserRepository/PdoDatabaseFactoryTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Psr\Container\ContainerInterface;
1414
use Zend\Expressive\Authentication\Exception\InvalidConfigException;
1515
use Zend\Expressive\Authentication\UserInterface;
16+
use Zend\Expressive\Authentication\UserInterfaceFactory;
1617
use Zend\Expressive\Authentication\UserRepository\PdoDatabase;
1718
use Zend\Expressive\Authentication\UserRepository\PdoDatabaseFactory;
1819

@@ -21,7 +22,7 @@ class PdoDatabaseFactoryTest extends TestCase
2122
protected function setUp()
2223
{
2324
$this->container = $this->prophesize(ContainerInterface::class);
24-
$this->user = $this->prophesize(UserInterface::class);
25+
$this->userFactory = $this->prophesize(UserInterfaceFactory::class);
2526
$this->factory = new PdoDatabaseFactory();
2627
}
2728

@@ -79,7 +80,7 @@ public function testInvokeWithInvalidConfig($pdoConfig)
7980
$this->container->get('config')->willReturn([
8081
'authentication' => [ 'pdo' => $pdoConfig ]
8182
]);
82-
$this->container->get(UserInterface::class)->willReturn($this->user->reveal());
83+
$this->container->get(UserInterface::class)->willReturn($this->userFactory->reveal());
8384
$pdoDatabase = ($this->factory)($this->container->reveal());
8485
}
8586

@@ -97,7 +98,7 @@ public function testInvokeWithValidConfig()
9798
]
9899
]
99100
]);
100-
$this->container->get(UserInterface::class)->willReturn($this->user->reveal());
101+
$this->container->get(UserInterface::class)->willReturn($this->userFactory->reveal());
101102
$pdoDatabase = ($this->factory)($this->container->reveal());
102103
$this->assertInstanceOf(PdoDatabase::class, $pdoDatabase);
103104
}

0 commit comments

Comments
 (0)