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

Commit 53e4787

Browse files
committed
Do not allow getUserRoles() to be nullable
Should always return an array.
1 parent ef23c49 commit 53e4787

File tree

7 files changed

+47
-25
lines changed

7 files changed

+47
-25
lines changed

src/UserInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function getUsername() : string;
1616
/**
1717
* Get all user roles
1818
*
19-
* @return string[]|null
19+
* @return string[]
2020
*/
21-
public function getUserRoles() : ?array;
21+
public function getUserRoles() : array;
2222
}

src/UserRepository/Htpasswd.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public function authenticate(string $credential, string $password = null) : ?Use
6666
/**
6767
* {@inheritDoc}
6868
*/
69-
public function getRolesFromUser(string $username): ?array
69+
public function getRolesFromUser(string $username) : array
7070
{
71-
return null;
71+
return [];
7272
}
7373

7474
/**

src/UserRepository/HtpasswdFactory.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ class HtpasswdFactory
1717
*/
1818
public function __invoke(ContainerInterface $container) : Htpasswd
1919
{
20-
$htpasswd = $container->get('config')['authentication']['htpasswd'] ?? null;
20+
$config = $container->has('config') ? $container->get('config') : [];
21+
$htpasswd = $config['authentication']['htpasswd'] ?? null;
22+
2123
if (null === $htpasswd) {
22-
throw new Exception\InvalidConfigException(
23-
'Htpasswd file name is not present in user_register config'
24-
);
24+
throw new Exception\InvalidConfigException(sprintf(
25+
'Config key authentication.htpasswd is not present; cannot create %s user repository adapter',
26+
Htpasswd::class
27+
));
2528
}
29+
2630
return new Htpasswd($htpasswd);
2731
}
2832
}

src/UserRepository/PdoDatabase.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,43 @@ public function authenticate(string $credential, string $password = null) : ?Use
4646
$this->config['table'],
4747
$this->config['field']['username']
4848
);
49+
4950
$stmt = $this->pdo->prepare($sql);
5051
$stmt->bindParam(':username', $credential);
52+
5153
if (! $stmt->execute()) {
5254
return null;
5355
}
56+
5457
$result = $stmt->fetchObject();
5558

56-
return password_verify($password, $result->{$this->config['field']['password']}) ?
57-
$this->generateUser($credential, $this->getRolesFromUser($credential)) :
58-
null;
59+
return password_verify($password, $result->{$this->config['field']['password']})
60+
? $this->generateUser($credential, $this->getRolesFromUser($credential))
61+
: null;
5962
}
6063

6164
/**
6265
* {@inheritDoc}
6366
*/
64-
public function getRolesFromUser(string $username): ?array
67+
public function getRolesFromUser(string $username) : array
6568
{
6669
if (! isset($this->config['sql_get_roles'])) {
67-
return null;
70+
return [];
6871
}
72+
6973
if (false === strpos($this->config['sql_get_roles'], ':username')) {
7074
throw new Exception\InvalidConfigException(
71-
'The sql_get_roles must include a :username parameter'
75+
'The sql_get_roles configuration setting must include a :username parameter'
7276
);
7377
}
78+
7479
$stmt = $this->pdo->prepare($this->config['sql_get_roles']);
7580
$stmt->bindParam(':username', $username);
81+
7682
if (! $stmt->execute()) {
77-
return null;
83+
return [];
7884
}
85+
7986
$roles = [];
8087
foreach ($stmt->fetchAll(PDO::FETCH_NUM) as $role) {
8188
$roles[] = $role[0];

src/UserRepository/UserTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ protected function generateUser(string $username, ?array $roles = null) : UserIn
2323
public function __construct(string $username, $roles)
2424
{
2525
$this->username = $username;
26-
$this->roles = $roles;
26+
$this->roles = $roles ?: [];
2727
}
2828

2929
public function getUsername() : string
3030
{
3131
return $this->username;
3232
}
3333

34-
public function getUserRoles() : ?array
34+
public function getUserRoles() : array
3535
{
3636
return $this->roles;
3737
}

src/UserRepositoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function authenticate(string $credential, string $password = null) : ?Use
2121
* Get the user roles if present.
2222
*
2323
* @param string $username
24-
* @return string[]|null
24+
* @return string[]
2525
*/
26-
public function getRolesFromUser(string $username) : ?array;
26+
public function getRolesFromUser(string $username) : array;
2727
}

test/UserRepository/HtpasswdFactoryTest.php

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

99
use PHPUnit\Framework\TestCase;
1010
use Psr\Container\ContainerInterface;
11+
use Zend\Expressive\Authentication\Exception\InvalidConfigException;
1112
use Zend\Expressive\Authentication\UserRepository\Htpasswd;
1213
use Zend\Expressive\Authentication\UserRepository\HtpasswdFactory;
1314

@@ -19,30 +20,40 @@ protected function setUp()
1920
$this->factory = new HtpasswdFactory();
2021
}
2122

22-
/**
23-
* @expectedException Zend\Expressive\Authentication\Exception\InvalidConfigException
24-
*/
23+
public function testInvokeWithMissingConfig()
24+
{
25+
$this->container->has('config')->willReturn(false);
26+
$this->container->get('config')->shouldNotBeCalled();
27+
28+
$this->expectException(InvalidConfigException::class);
29+
$htpasswd = ($this->factory)($this->container->reveal());
30+
}
31+
2532
public function testInvokeWithEmptyConfig()
2633
{
34+
$this->container->has('config')->willReturn(true);
2735
$this->container->get('config')->willReturn([]);
36+
37+
$this->expectException(InvalidConfigException::class);
2838
$htpasswd = ($this->factory)($this->container->reveal());
2939
}
3040

31-
/**
32-
* @expectedException Zend\Expressive\Authentication\Exception\InvalidConfigException
33-
*/
3441
public function testInvokeWithInvalidConfig()
3542
{
43+
$this->container->has('config')->willReturn(true);
3644
$this->container->get('config')->willReturn([
3745
'authentication' => [
3846
'htpasswd' => 'foo'
3947
]
4048
]);
49+
50+
$this->expectException(InvalidConfigException::class);
4151
$htpasswd = ($this->factory)($this->container->reveal());
4252
}
4353

4454
public function testInvokeWithValidConfig()
4555
{
56+
$this->container->has('config')->willReturn(true);
4657
$this->container->get('config')->willReturn([
4758
'authentication' => [
4859
'htpasswd' => __DIR__ . '/../TestAssets/htpasswd'

0 commit comments

Comments
 (0)