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

Commit 2145176

Browse files
committed
Merge branch 'feature/16-rename-username-to-identity' into release-1.0.0
Forward port #16 Conflicts: CHANGELOG.md
2 parents c108792 + b635bb5 commit 2145176

15 files changed

+234
-83
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ All notable changes to this project will be documented in this file, in reverse
2626

2727
- Nothing.
2828

29-
## 0.2.1 - TBD
29+
## 0.3.0 - 2018-01-24
3030

3131
### Added
3232

3333
- Nothing.
3434

3535
### Changed
3636

37-
- Nothing
37+
- [#14](https://github.com/zendframework/zend-expressive-authentication/issues/14)
38+
renames the method `UserInterface::getUsername()` to
39+
`UserInterface::getIdentity()`.
3840

3941
### Deprecated
4042

docs/book/v1/intro.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ namespace Zend\Expressive\Authentication;
2222
interface UserInterface
2323
{
2424
/**
25-
* Get the username
25+
* Get the unique user identity (id, username, email address or ...)
2626
*
2727
* @return string
2828
*/
29-
public function getUsername(): string;
29+
public function getIdentity(): string;
3030

3131
/**
3232
* Get all user roles
@@ -40,7 +40,7 @@ interface UserInterface
4040
The `UserInterface` attribute in the PSR-7 request can be used for checking
4141
if a user has been authenticated or not, e.g. it can be used to verify the
4242
authorization level of a user (for this scope, it is consumed by
43-
[zend-expressive-authotization](https://github.com/zendframework/zend-expressive-authorization)).
43+
[zend-expressive-authorization](https://github.com/zendframework/zend-expressive-authorization)).
4444

4545
## Usage in the route
4646

docs/book/v1/user-repository.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,82 @@ result will be a `UserInterface` instance, otherwise a null value is returned.
4242
The second function is `getRolesFromUser()` and it specifies how to retrieve
4343
the roles for a user. If a user does not have roles, this function will return
4444
an empty array.
45+
46+
47+
## Configure the user repository
48+
49+
In order to use a user repository adapter, we need to configure it. For instance,
50+
to consume an `htpasswd` file, we need to configure the path to the file.
51+
Such configuration is provided in the `authentication` hierarchy provided to
52+
your [PSR-11](http://www.php-fig.org/psr/psr-11/) container. We demonstrate
53+
examples of such configuration below.
54+
55+
Using [Expressive](https://docs.zendframework.com/zend-expressive/), this
56+
configuration can be stored in a file under the `/config/autoload/` folder. We
57+
suggest to use a `.local.php` suffix — e.g.
58+
`/config/autoload/auth.local.php` — as local configuration is not stored
59+
in the version control system.
60+
61+
You can also provide this configuration using a [ConfigProvider.php](https://github.com/zendframework/zend-expressive-authentication/blob/master/src/ConfigProvider.php)
62+
class. [Read this blog post](https://framework.zend.com/blog/2017-04-20-config-aggregator.html)
63+
for more information on config providers.
64+
65+
## htpasswd configuration
66+
67+
When using the htpasswd user repository implementation, you need only configure
68+
the path to the `htpasswd` file:
69+
70+
```php
71+
return [
72+
'authentication' => [
73+
'htpasswd' => 'insert the path to htpasswd file',
74+
],
75+
];
76+
```
77+
78+
## PDO configuration
79+
80+
When using the PDO user repository adapter, you will need to provide PDO
81+
connection parameters, as well as information on the table, field names, and a
82+
SQL statement for retrieiving user roles:
83+
84+
```php
85+
return [
86+
'authentication' => [
87+
'pdo' => [
88+
'dsn' => '',
89+
'username' => '',
90+
'password' => '',
91+
'table' => 'user table name',
92+
'field' => [
93+
'identity' => 'identity field name',
94+
'password' => 'password field name',
95+
],
96+
'sql_get_roles' => 'SQL to retrieve roles with :identity parameter',
97+
],
98+
],
99+
];
100+
```
101+
102+
The required parameters are `dsn`, `table`, and `field`.
103+
104+
The `dsn` value is the DSN connection string to be used to connect to the database.
105+
For instance, using a SQLite database, a typical value is `sqlite:/path/to/file`.
106+
107+
The `username` and `password` parameters are optional parameters used to connect
108+
to the database. Depending on the database, these parameters may not be required;
109+
e.g. [SQLite](https://sqlite.org/) does not require them.
110+
111+
The `table` value is the name of the table containing the user credentials.
112+
113+
The `field` parameter contains the field name of the `identity` of the user and
114+
the user `password.` The `identity` of the user can be a username, an email, etc.
115+
116+
The `sql_get_roles` setting is an optional parameter that contains the SQL query
117+
for retrieving the user roles. The identity value must be specified using the
118+
placeholder `:identity`. For instance, if a role is stored in a user table, a
119+
typical query might look like the following:
120+
121+
```sql
122+
SELECT role FROM user WHERE username = :identity
123+
```

src/ConfigProvider.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ public function getAuthenticationConfig() : array
2929
*
3030
* Example: using htpasswd UserRepositoryInterface implementation:
3131
*
32-
* 'user_register' => [
33-
* 'htpasswd' => 'insert the path to htpasswd file'
32+
* [
33+
* 'htpasswd' => 'insert the path to htpasswd file',
34+
* 'pdo' => [
35+
* 'dsn' => 'DSN for connection',
36+
* 'username' => 'username for database connection, if needed',
37+
* 'password' => 'password for database connection, if needed',
38+
* 'table' => 'user table name',
39+
* 'field' => [
40+
* 'identity' => 'identity field name',
41+
* 'password' => 'password field name',
42+
* ],
43+
* 'sql_get_roles' => 'SQL to retrieve roles by :identity',
44+
* ],
3445
* ]
3546
*/
3647
];

src/UserInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
interface UserInterface
1313
{
1414
/**
15-
* Get the username
15+
* Get the unique user identity (id, username, email address or ...)
1616
*/
17-
public function getUsername() : string;
17+
public function getIdentity() : string;
1818

1919
/**
2020
* Get all user roles

src/UserRepository/Htpasswd.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function authenticate(string $credential, string $password = null) : ?Use
7070
/**
7171
* {@inheritDoc}
7272
*/
73-
public function getRolesFromUser(string $username) : array
73+
public function getRolesFromUser(string $identity) : array
7474
{
7575
return [];
7676
}

src/UserRepository/HtpasswdFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class HtpasswdFactory
1919
*/
2020
public function __invoke(ContainerInterface $container) : Htpasswd
2121
{
22-
$config = $container->has('config') ? $container->get('config') : [];
23-
$htpasswd = $config['authentication']['htpasswd'] ?? null;
24-
22+
$htpasswd = $container->get('config')['authentication']['htpasswd'] ?? null;
2523
if (null === $htpasswd) {
2624
throw new Exception\InvalidConfigException(sprintf(
2725
'Config key authentication.htpasswd is not present; cannot create %s user repository adapter',

src/UserRepository/PdoDatabase.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Zend\Expressive\Authentication\UserRepository;
1111

1212
use PDO;
13+
use PDOException;
1314
use Zend\Expressive\Authentication\Exception;
1415
use Zend\Expressive\Authentication\UserInterface;
1516
use Zend\Expressive\Authentication\UserRepositoryInterface;
@@ -44,14 +45,20 @@ public function __construct(PDO $pdo, array $config)
4445
public function authenticate(string $credential, string $password = null) : ?UserInterface
4546
{
4647
$sql = sprintf(
47-
"SELECT %s FROM %s WHERE %s = :username",
48+
"SELECT %s FROM %s WHERE %s = :identity",
4849
$this->config['field']['password'],
4950
$this->config['table'],
50-
$this->config['field']['username']
51+
$this->config['field']['identity']
5152
);
5253

5354
$stmt = $this->pdo->prepare($sql);
54-
$stmt->bindParam(':username', $credential);
55+
if (false === $stmt) {
56+
throw new Exception\RuntimeException(
57+
'An error occurred when preparing to fetch user details from ' .
58+
'the repository; please verify your configuration'
59+
);
60+
}
61+
$stmt->bindParam(':identity', $credential);
5562
$stmt->execute();
5663

5764
$result = $stmt->fetchObject();
@@ -67,20 +74,32 @@ public function authenticate(string $credential, string $password = null) : ?Use
6774
/**
6875
* {@inheritDoc}
6976
*/
70-
public function getRolesFromUser(string $username) : array
77+
public function getRolesFromUser(string $identity) : array
7178
{
7279
if (! isset($this->config['sql_get_roles'])) {
7380
return [];
7481
}
7582

76-
if (false === strpos($this->config['sql_get_roles'], ':username')) {
83+
if (false === strpos($this->config['sql_get_roles'], ':identity')) {
7784
throw new Exception\InvalidConfigException(
78-
'The sql_get_roles configuration setting must include a :username parameter'
85+
'The sql_get_roles configuration setting must include a :identity parameter'
7986
);
8087
}
8188

82-
$stmt = $this->pdo->prepare($this->config['sql_get_roles']);
83-
$stmt->bindParam(':username', $username);
89+
try {
90+
$stmt = $this->pdo->prepare($this->config['sql_get_roles']);
91+
} catch (PDOException $e) {
92+
throw new Exception\RuntimeException(sprintf(
93+
'Error preparing retrieval of user roles: %s',
94+
$e->getMessage()
95+
));
96+
}
97+
if (false === $stmt) {
98+
throw new Exception\RuntimeException(sprintf(
99+
'Error preparing retrieval of user roles: unknown error'
100+
));
101+
}
102+
$stmt->bindParam(':identity', $identity);
84103

85104
if (! $stmt->execute()) {
86105
return [];

src/UserRepository/PdoDatabaseFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public function __invoke(ContainerInterface $container) : PdoDatabase
3636
'The PDO table name is missing in the configuration'
3737
);
3838
}
39-
if (! isset($pdo['field']['username'])) {
39+
if (! isset($pdo['field']['identity'])) {
4040
throw new Exception\InvalidConfigException(
41-
'The PDO username field is missing in the configuration'
41+
'The PDO identity field is missing in the configuration'
4242
);
4343
}
4444
if (! isset($pdo['field']['password'])) {

src/UserRepository/UserTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
trait UserTrait
1515
{
1616
/**
17-
* Generate a user from username and list of roles
17+
* Generate a user from identity and list of roles
1818
*/
19-
protected function generateUser(string $username, ?array $roles = null) : UserInterface
19+
protected function generateUser(string $identity, ?array $roles = null) : UserInterface
2020
{
21-
return new class($username, $roles) implements UserInterface {
22-
private $username;
21+
return new class($identity, $roles) implements UserInterface {
22+
private $identity;
2323
private $roles;
2424

25-
public function __construct(string $username, $roles)
25+
public function __construct(string $identity, $roles)
2626
{
27-
$this->username = $username;
27+
$this->identity = $identity;
2828
$this->roles = $roles ?: [];
2929
}
3030

31-
public function getUsername() : string
31+
public function getIdentity() : string
3232
{
33-
return $this->username;
33+
return $this->identity;
3434
}
3535

3636
public function getUserRoles() : array

0 commit comments

Comments
 (0)