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

Commit d602ab4

Browse files
committed
Merging develop to master in preparation for 1.1.0 release.
2 parents 57b5c34 + 8efc16d commit d602ab4

File tree

6 files changed

+151
-28
lines changed

6 files changed

+151
-28
lines changed

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,45 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 1.1.0 - 2019-03-05
6+
7+
### Added
8+
9+
- [#41](https://github.com/zendframework/zend-expressive-authentication/pull/46) allows users to provide an application-specific PDO service name to use
10+
with the `PdoDatabase` user repository implementation, instead of connection
11+
parameters. This allows re-use of an existing PDO connection. To configure it:
12+
13+
```php
14+
return [
15+
'authentication' => [
16+
'pdo' => [
17+
'service' => 'name-of-existing-PDO-service',
18+
'table' => 'name-of-table-to-use',
19+
'field' => [
20+
'identity' => 'name-of-field-containing-identity',
21+
'password' => 'name-of-field-containing-password-hash',
22+
],
23+
],
24+
],
25+
];
26+
```
27+
28+
### Changed
29+
30+
- Nothing.
31+
32+
### Deprecated
33+
34+
- Nothing.
35+
36+
### Removed
37+
38+
- Nothing.
39+
40+
### Fixed
41+
42+
- Nothing.
43+
544
## 1.0.2 - 2019-03-05
645

746
### Added

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
},
5656
"extra": {
5757
"branch-alias": {
58-
"dev-master": "0.4.x-dev"
58+
"dev-master": "1.1.x-dev",
59+
"dev-develop": "1.2.x-dev"
5960
},
6061
"zf": {
6162
"config-provider": "Zend\\Expressive\\Authentication\\ConfigProvider"

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/v1/user-repository.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ return [
6767

6868
When using the PDO user repository adapter, you will need to provide PDO
6969
connection parameters, as well as information on the table, field names, and a
70-
SQL statement for retrieiving user roles:
70+
SQL statement for retrieving user roles:
7171

7272
```php
7373
return [
@@ -120,3 +120,34 @@ detail using the following query:
120120
```sql
121121
SELECT email FROM user WHERE username = :identity
122122
```
123+
124+
### PDO service name
125+
126+
> Since 1.1.0
127+
128+
As an alternative, you can provide a service name instead of PDO connection
129+
parameters. In such a case, you can substitute the key `service` for the `dsn`
130+
configuration key:
131+
132+
```php
133+
return [
134+
'authentication' => [
135+
'pdo' => [
136+
'service' => \PDO::class, // "service" instead of "dsn"
137+
'table' => 'user table name',
138+
'field' => [
139+
'identity' => 'identity field name',
140+
'password' => 'password field name',
141+
],
142+
'sql_get_roles' => 'SQL to retrieve roles with :identity parameter',
143+
'sql_get_details' => 'SQL to retrieve user details by :identity',
144+
],
145+
],
146+
];
147+
```
148+
149+
The parameters `table` and `field` still remain required, and one or the other
150+
of `dsn` or `service` **MUST** be present.
151+
152+
When specifying the `service` key, the value **MUST** evaluate to an existing
153+
service that resolves to a `PDO` instance.

src/UserRepository/PdoDatabaseFactory.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4-
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2017-2019 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

@@ -27,11 +27,7 @@ public function __invoke(ContainerInterface $container) : PdoDatabase
2727
'PDO values are missing in authentication config'
2828
);
2929
}
30-
if (! isset($pdo['dsn'])) {
31-
throw new Exception\InvalidConfigException(
32-
'The PDO DSN value is missing in the configuration'
33-
);
34-
}
30+
3531
if (! isset($pdo['table'])) {
3632
throw new Exception\InvalidConfigException(
3733
'The PDO table name is missing in the configuration'
@@ -47,6 +43,21 @@ public function __invoke(ContainerInterface $container) : PdoDatabase
4743
'The PDO password field is missing in the configuration'
4844
);
4945
}
46+
47+
if (isset($pdo['service']) && $container->has($pdo['service'])) {
48+
return new PdoDatabase(
49+
$container->get($pdo['service']),
50+
$pdo,
51+
$container->get(UserInterface::class)
52+
);
53+
}
54+
55+
if (! isset($pdo['dsn'])) {
56+
throw new Exception\InvalidConfigException(
57+
'The PDO DSN value is missing in the configuration'
58+
);
59+
}
60+
5061
return new PdoDatabase(
5162
new PDO(
5263
$pdo['dsn'],

test/UserRepository/PdoDatabaseFactoryTest.php

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4-
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2017-2019 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 ZendTest\Expressive\Authentication\UserRepository;
1111

12+
use PDO;
1213
use PHPUnit\Framework\TestCase;
1314
use Psr\Container\ContainerInterface;
1415
use Zend\Expressive\Authentication\Exception\InvalidConfigException;
@@ -22,6 +23,7 @@ protected function setUp()
2223
{
2324
$this->container = $this->prophesize(ContainerInterface::class);
2425
$this->user = $this->prophesize(UserInterface::class);
26+
$this->pdo = $this->prophesize(PDO::class);
2527
$this->factory = new PdoDatabaseFactory();
2628
}
2729

@@ -45,24 +47,43 @@ public function testInvokeWithEmptyConfig()
4547
$pdoDatabase = ($this->factory)($this->container->reveal());
4648
}
4749

48-
public function getPdoConfig()
50+
public function getPdoInvalidConfig()
4951
{
5052
return [
5153
[[]],
54+
[[
55+
'service' => PDO::class,
56+
]],
57+
[[
58+
'service' => PDO::class,
59+
'table' => 'test'
60+
]],
61+
[[
62+
'service' => PDO::class,
63+
'table' => 'test',
64+
'field' => []
65+
]],
66+
[[
67+
'service' => PDO::class,
68+
'table' => 'test',
69+
'field' => [
70+
'identity' => 'email',
71+
]
72+
]],
5273
[[
5374
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1'
5475
]],
5576
[[
56-
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
77+
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
5778
'table' => 'test'
5879
]],
5980
[[
60-
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
81+
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
6182
'table' => 'test',
6283
'field' => []
6384
]],
6485
[[
65-
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
86+
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
6687
'table' => 'test',
6788
'field' => [
6889
'identity' => 'email'
@@ -72,14 +93,16 @@ public function getPdoConfig()
7293
}
7394

7495
/**
75-
* @dataProvider getPdoConfig
96+
* @dataProvider getPdoInvalidConfig
7697
* @expectedException Zend\Expressive\Authentication\Exception\InvalidConfigException
7798
*/
7899
public function testInvokeWithInvalidConfig($pdoConfig)
79100
{
80101
$this->container->get('config')->willReturn([
81-
'authentication' => [ 'pdo' => $pdoConfig ]
102+
'authentication' => ['pdo' => $pdoConfig]
82103
]);
104+
$this->container->has(PDO::class)->willReturn(true);
105+
$this->container->get(PDO::class)->willReturn($this->pdo->reveal());
83106
$this->container->get(UserInterface::class)->willReturn(
84107
function () {
85108
return $this->user->reveal();
@@ -88,20 +111,38 @@ function () {
88111
$pdoDatabase = ($this->factory)($this->container->reveal());
89112
}
90113

91-
public function testInvokeWithValidConfig()
114+
public function getPdoValidConfig()
92115
{
93-
$this->container->get('config')->willReturn([
94-
'authentication' => [
95-
'pdo' => [
96-
'dsn' => 'sqlite:'. __DIR__ . '/../TestAssets/pdo.sqlite',
97-
'table' => 'user',
98-
'field' => [
99-
'identity' => 'username',
100-
'password' => 'password'
101-
]
116+
return [
117+
[[
118+
'service' => PDO::class,
119+
'table' => 'user',
120+
'field' => [
121+
'identity' => 'username',
122+
'password' => 'password'
102123
]
103-
]
124+
]],
125+
[[
126+
'dsn' => 'sqlite:' . __DIR__ . '/../TestAssets/pdo.sqlite',
127+
'table' => 'user',
128+
'field' => [
129+
'identity' => 'username',
130+
'password' => 'password'
131+
]
132+
]],
133+
];
134+
}
135+
136+
/**
137+
* @dataProvider getPdoValidConfig
138+
*/
139+
public function testInvokeWithValidConfig($pdoConfig)
140+
{
141+
$this->container->get('config')->willReturn([
142+
'authentication' => ['pdo' => $pdoConfig]
104143
]);
144+
$this->container->has(PDO::class)->willReturn(true);
145+
$this->container->get(PDO::class)->willReturn($this->pdo->reveal());
105146
$this->container->get(UserInterface::class)->willReturn(
106147
function () {
107148
return $this->user->reveal();

0 commit comments

Comments
 (0)