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

Commit 0801e06

Browse files
committed
New UserInterface
1 parent 2fea552 commit 0801e06

13 files changed

+241
-97
lines changed

docs/book/v1/intro.md

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,30 @@ interface UserInterface
2323
{
2424
/**
2525
* Get the unique user identity (id, username, email address or ...)
26-
*
27-
* @return string
2826
*/
29-
public function getIdentity(): string;
27+
public function getIdentity() : string;
28+
29+
/**
30+
* Set the user identity
31+
*/
32+
public function setIdentity(string $identity) : void
33+
{
34+
$this->identity = $identity;
35+
}
3036

3137
/**
3238
* Get all user roles
3339
*
3440
* @return string[]
3541
*/
36-
public function getUserRoles() : array;
42+
public function getRoles() : array;
43+
44+
/**
45+
* Set the user's roles
46+
*
47+
* @param string[] $roles
48+
*/
49+
public function setRoles(array $roles) : void;
3750
}
3851
```
3952

@@ -42,6 +55,33 @@ if a user has been authenticated or not, e.g. it can be used to verify the
4255
authorization level of a user (for this scope, it is consumed by
4356
[zend-expressive-authorization](https://github.com/zendframework/zend-expressive-authorization)).
4457

58+
## Default User class
59+
60+
We provided a default user class, implemented by `Zend\Authentication\DefaultUser`.
61+
This class is a basic implementation of `UserInterface`. This default class
62+
can be changed by configuration, using the service alias `Zend\Authentication\UserInterface`.
63+
By default, the alias points to `DefaultUser` in the ConfigProvider class.
64+
65+
```php
66+
// src/ConfigProvider.php
67+
// ...
68+
public function getDependencies() : array
69+
{
70+
return [
71+
// ...
72+
'aliases' => [
73+
// ...
74+
UserInterface::class => DefaultUser::class
75+
]
76+
];
77+
}
78+
// ...
79+
```
80+
81+
You can change it using a custom `UserInterface` implementation or extending
82+
the `DefaultUser` class if you will.
83+
84+
4585
## Usage in the route
4686

4787
The `AuthenticationMiddleware` can be used to authenticate a route. You just
@@ -75,26 +115,18 @@ Basic Access Authentication* adapter and the *htpasswd* file as the user
75115
repository.
76116

77117
```php
78-
// ConfigProvider.php
79-
80-
use Zend\Expressive\Authentication\AuthenticationInterface;
81-
use Zend\Expressive\Authentication\UserRepositoryInterface;
82-
83-
class ConfigProvider
84-
{
118+
// src/ConfigProvider.php
85119
// ...
86-
87120
public function getDependencies() : array
88121
{
89122
return [
123+
// ...
90124
'aliases' => [
125+
// ...
91126
AuthenticationInterface::class => Basic\BasicAccess::class,
92127
UserRepositoryInterface::class => UserRepository\Htpasswd::class
93-
],
94-
// ...
128+
]
95129
];
96130
}
97-
98131
// ...
99-
}
100132
```

src/ConfigProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public function __invoke() : array
2525
public function getAuthenticationConfig() : array
2626
{
2727
return [
28-
/* Values will depend on user repository and/or adapter.
28+
/*
29+
* Values will depend on user repository and/or adapter.
2930
*
3031
* Example: using htpasswd UserRepositoryInterface implementation:
3132
*
@@ -58,11 +59,15 @@ public function getDependencies() : array
5859
// AuthenticationInterface::class => Basic\BasicAccess::class,
5960
// Provide an alias for the UserRepository adapter based on your application needs.
6061
// UserRepositoryInterface::class => UserRepository\Htpasswd::class
62+
UserInterface::class => DefaultUser::class
6163
],
6264
'factories' => [
6365
AuthenticationMiddleware::class => AuthenticationMiddlewareFactory::class,
6466
UserRepository\Htpasswd::class => UserRepository\HtpasswdFactory::class,
6567
UserRepository\PdoDatabase::class => UserRepository\PdoDatabaseFactory::class
68+
],
69+
'invokables' => [
70+
DefaultUser::class => DefaultUser::class
6671
]
6772
];
6873
}

src/DefaultUser.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
class DefaultUser implements UserInterface
13+
{
14+
protected $identity;
15+
16+
protected $roles = [];
17+
18+
public function setIdentity(string $identity) : void
19+
{
20+
$this->identity = $identity;
21+
}
22+
23+
public function getIdentity() : string
24+
{
25+
return $this->identity;
26+
}
27+
28+
public function setRoles(array $roles) : void
29+
{
30+
$this->roles = $roles;
31+
}
32+
33+
public function getRoles() : array
34+
{
35+
return $this->roles;
36+
}
37+
}

src/UserInterface.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@
1111

1212
interface UserInterface
1313
{
14+
/**
15+
* Set the user's identity
16+
*/
17+
public function setIdentity(string $identity) : void;
18+
1419
/**
1520
* Get the unique user identity (id, username, email address or ...)
1621
*/
1722
public function getIdentity() : string;
1823

24+
/**
25+
* Set the user's roles
26+
*/
27+
public function setRoles(array $roles) : void;
28+
1929
/**
2030
* Get all user roles
2131
*
2232
* @return string[]
2333
*/
24-
public function getUserRoles() : array;
34+
public function getRoles() : array;
2535
}

src/UserRepository/Htpasswd.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@
2020
*/
2121
class Htpasswd implements UserRepositoryInterface
2222
{
23-
use UserTrait;
24-
2523
/**
2624
* @var string
2725
*/
2826
private $filename;
2927

28+
/**
29+
* @var UserInterface
30+
*/
31+
private $user;
32+
3033
/**
3134
* @throws Exception\InvalidConfigException
3235
*/
33-
public function __construct(string $filename)
36+
public function __construct(string $filename, UserInterface $user)
3437
{
3538
if (! file_exists($filename)) {
3639
throw new Exception\InvalidConfigException(sprintf(
@@ -39,6 +42,7 @@ public function __construct(string $filename)
3942
));
4043
}
4144
$this->filename = $filename;
45+
$this->user = $user;
4246
}
4347

4448
/**
@@ -61,10 +65,12 @@ public function authenticate(string $credential, string $password = null) : ?Use
6165
}
6266
fclose($handle);
6367

64-
return $found
65-
&& password_verify($password === null ? '' : $password, $hash)
66-
? $this->generateUser($credential)
67-
: null;
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;
72+
}
73+
return null;
6874
}
6975

7076
/**

src/UserRepository/HtpasswdFactory.php

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

1212
use Psr\Container\ContainerInterface;
1313
use Zend\Expressive\Authentication\Exception;
14+
use Zend\Expressive\Authentication\UserInterface;
1415

1516
class HtpasswdFactory
1617
{
@@ -26,7 +27,9 @@ public function __invoke(ContainerInterface $container) : Htpasswd
2627
Htpasswd::class
2728
));
2829
}
29-
30-
return new Htpasswd($htpasswd);
30+
return new Htpasswd(
31+
$htpasswd,
32+
$container->get(UserInterface::class)
33+
);
3134
}
3235
}

src/UserRepository/PdoDatabase.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
*/
2222
class PdoDatabase implements UserRepositoryInterface
2323
{
24-
use UserTrait;
25-
2624
/**
2725
* @var PDO
2826
*/
@@ -33,10 +31,16 @@ class PdoDatabase implements UserRepositoryInterface
3331
*/
3432
private $config;
3533

36-
public function __construct(PDO $pdo, array $config)
34+
/**
35+
* @var UserInterface
36+
*/
37+
private $user;
38+
39+
public function __construct(PDO $pdo, array $config, UserInterface $user)
3740
{
3841
$this->pdo = $pdo;
3942
$this->config = $config;
43+
$this->user = $user;
4044
}
4145

4246
/**
@@ -66,9 +70,12 @@ public function authenticate(string $credential, string $password = null) : ?Use
6670
return null;
6771
}
6872

69-
return password_verify($password, $result->{$this->config['field']['password']})
70-
? $this->generateUser($credential, $this->getRolesFromUser($credential))
71-
: null;
73+
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;
77+
}
78+
return null;
7279
}
7380

7481
/**

src/UserRepository/PdoDatabaseFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PDO;
1313
use Psr\Container\ContainerInterface;
1414
use Zend\Expressive\Authentication\Exception;
15+
use Zend\Expressive\Authentication\UserInterface;
1516

1617
class PdoDatabaseFactory
1718
{
@@ -52,7 +53,8 @@ public function __invoke(ContainerInterface $container) : PdoDatabase
5253
$pdo['username'] ?? null,
5354
$pdo['password'] ?? null
5455
),
55-
$pdo
56+
$pdo,
57+
$container->get(UserInterface::class)
5658
);
5759
}
5860
}

src/UserRepository/UserTrait.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)