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

Commit d997148

Browse files
committed
Merge branch 'hotfix/4'
Close #4 Fixes #1
2 parents a4bbfc7 + 23642a0 commit d997148

13 files changed

+188
-25
lines changed

CHANGELOG.md

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

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

5+
## 0.2.0 - 2017-11-27
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- [#4](https://github.com/zendframework/zend-expressive-authentication/pull/4)
14+
renames the method `UserInterface::getUserRole()` to
15+
`UserInterface::getUserRoles()`. The method MUST return an array of string
16+
role names.
17+
18+
- [#4](https://github.com/zendframework/zend-expressive-authentication/pull/4)
19+
renames the method `UserRepositoryInterface::getRoleFromUser()` to
20+
`UserRepositoryInterface::getRolesFromUser()`. The method MUST return an array
21+
of string role names.
22+
23+
### Deprecated
24+
25+
- Nothing.
26+
27+
### Removed
28+
29+
- Nothing.
30+
31+
### Fixed
32+
33+
- Nothing.
34+
535
## 0.1.0 - 2017-11-08
636

737
Initial release.

src/UserInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ interface UserInterface
1414
public function getUsername() : string;
1515

1616
/**
17-
* Get the user role
17+
* Get all user roles
18+
*
19+
* @return string[]
1820
*/
19-
public function getUserRole() : string;
21+
public function getUserRoles() : array;
2022
}

src/UserRepository/Htpasswd.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ public function authenticate(string $credential, string $password = null) : ?Use
5959
fclose($handle);
6060

6161
return $found && password_verify($password, $hash) ?
62-
$this->generateUser($credential, '') :
62+
$this->generateUser($credential) :
6363
null;
6464
}
6565

66+
/**
67+
* {@inheritDoc}
68+
*/
69+
public function getRolesFromUser(string $username) : array
70+
{
71+
return [];
72+
}
73+
6674
/**
6775
* Check bcrypt usage for security reason
6876
*

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: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,52 @@ public function __construct(PDO $pdo, array $config)
4141
public function authenticate(string $credential, string $password = null) : ?UserInterface
4242
{
4343
$sql = sprintf(
44-
'SELECT * FROM %s WHERE %s = :username',
44+
"SELECT %s FROM %s WHERE %s = :username",
45+
$this->config['field']['password'],
4546
$this->config['table'],
4647
$this->config['field']['username']
4748
);
49+
4850
$stmt = $this->pdo->prepare($sql);
4951
$stmt->bindParam(':username', $credential);
52+
5053
if (! $stmt->execute()) {
5154
return null;
5255
}
56+
5357
$result = $stmt->fetchObject();
5458

55-
return password_verify($password, $result->{$this->config['field']['password']}) ?
56-
$this->generateUser($credential, $this->config['field']['role'] ?? '') :
57-
null;
59+
return password_verify($password, $result->{$this->config['field']['password']})
60+
? $this->generateUser($credential, $this->getRolesFromUser($credential))
61+
: null;
62+
}
63+
64+
/**
65+
* {@inheritDoc}
66+
*/
67+
public function getRolesFromUser(string $username) : array
68+
{
69+
if (! isset($this->config['sql_get_roles'])) {
70+
return [];
71+
}
72+
73+
if (false === strpos($this->config['sql_get_roles'], ':username')) {
74+
throw new Exception\InvalidConfigException(
75+
'The sql_get_roles configuration setting must include a :username parameter'
76+
);
77+
}
78+
79+
$stmt = $this->pdo->prepare($this->config['sql_get_roles']);
80+
$stmt->bindParam(':username', $username);
81+
82+
if (! $stmt->execute()) {
83+
return [];
84+
}
85+
86+
$roles = [];
87+
foreach ($stmt->fetchAll(PDO::FETCH_NUM) as $role) {
88+
$roles[] = $role[0];
89+
}
90+
return $roles;
5891
}
5992
}

src/UserRepository/UserTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212
trait UserTrait
1313
{
1414
/**
15-
* Generate a user from $username and $role
15+
* Generate a user from username and list of roles
1616
*/
17-
protected function generateUser(string $username, string $role) : UserInterface
17+
protected function generateUser(string $username, ?array $roles = null) : UserInterface
1818
{
19-
return new class($username, $role) implements UserInterface {
19+
return new class($username, $roles) implements UserInterface {
2020
private $username;
21-
private $role;
21+
private $roles;
2222

23-
public function __construct($username, $role)
23+
public function __construct(string $username, $roles)
2424
{
2525
$this->username = $username;
26-
$this->role = $role;
26+
$this->roles = $roles ?: [];
2727
}
2828

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

34-
public function getUserRole() : string
34+
public function getUserRoles() : array
3535
{
36-
return $this->role;
36+
return $this->roles;
3737
}
3838
};
3939
}

src/UserRepositoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ interface UserRepositoryInterface
1616
* @param string $credential can be also a token
1717
*/
1818
public function authenticate(string $credential, string $password = null) : ?UserInterface;
19+
20+
/**
21+
* Get the user roles if present.
22+
*
23+
* @param string $username
24+
* @return string[]
25+
*/
26+
public function getRolesFromUser(string $username) : array;
1927
}

test/TestAssets/pdo_role.sqlite

2 KB
Binary file not shown.

test/TestAssets/pdo_roles.sqlite

4 KB
Binary file not shown.

test/TestAssets/sqlite_with_role.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE user(
2+
username TEXT,
3+
password TEXT,
4+
role TEXT
5+
);
6+
7+
INSERT INTO user (username, password, role) VALUES ('test', '$2y$10$C822kPutHb8S/An9pBzJHeaN2/uqytA88O5VtTaY9m9EzWCJPDF7e', 'admin');

0 commit comments

Comments
 (0)