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

Commit 27e0485

Browse files
committed
Added the details and changed UserRepositoryInterface
1 parent 941544f commit 27e0485

File tree

12 files changed

+120
-120
lines changed

12 files changed

+120
-120
lines changed

docs/book/v1/intro.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ interface UserInterface
3232
* @return string[]
3333
*/
3434
public function getRoles() : array;
35+
36+
/**
37+
* Get a detail $name if present, $default otherwise
38+
*/
39+
public function getDetail(string $name, $default = null);
40+
41+
/**
42+
* Get all the details, if any
43+
*/
44+
public function getDetails() : array;
3545
}
3646
```
3747

docs/book/v1/user-repository.md

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,12 @@ interface UserRepositoryInterface
2424
* @param string $credential can be also a token
2525
*/
2626
public function authenticate(string $credential, string $password = null) : ?UserInterface;
27-
28-
/**
29-
* Get the user roles if present.
30-
*
31-
* @param string $username
32-
* @return string[]
33-
*/
34-
public function getUserRoles(string $username) : array;
35-
36-
/**
37-
* Get the user details if present.
38-
*
39-
* @param string $identity
40-
* @return string[]
41-
*/
42-
public function getUserDetails(string $identity) : array;
4327
}
4428
```
4529

46-
It contains 3 functions: `authenticate()`, `getUserRoles()`, and `getUserDetails`.
47-
The first function is used to authenticate the user's credential. If
48-
authenticated, the result will be a `UserInterface` instance, otherwise a null
49-
value is returned.
50-
51-
The second function is `getUserRoles()` and it specifies how to retrieve
52-
the roles for a user. If a user does not have roles, this function will return
53-
an empty array.
54-
55-
The last function is `getUserDetails()` and it specifies the additional user
56-
information (details), if any.
57-
30+
It contains only the `authenticate()` function, to authenticate the user's
31+
credential. If authenticated, the result will be a `UserInterface` instance,
32+
otherwise a `null` value is returned.
5833

5934
## Configure the user repository
6035

@@ -137,3 +112,9 @@ SELECT role FROM user WHERE username = :identity
137112

138113
The `sql_get_details` parameter is similar to `sql_get_roles`, it specified the
139114
SQL query for retrieving the user's additional details, if any.
115+
For instance, if a user has an email field this can be returned as additional
116+
detail using the query as follows:
117+
118+
```sql
119+
SELECT email FROM user WHERE username = :identity
120+
```

src/UserRepository/Htpasswd.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,6 @@ public function authenticate(string $credential, string $password = null) : ?Use
7171
return null;
7272
}
7373

74-
/**
75-
* {@inheritDoc}
76-
*/
77-
public function getUserRoles(string $identity) : array
78-
{
79-
return [];
80-
}
81-
82-
/**
83-
* {@inheritDoc}
84-
*/
85-
public function getUserDetails(string $identity) : array
86-
{
87-
return [];
88-
}
89-
9074
/**
9175
* Check bcrypt usage for security reason
9276
*

src/UserRepository/PdoDatabase.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ public function authenticate(string $credential, string $password = null) : ?Use
8484
}
8585

8686
/**
87-
* {@inheritDoc}
87+
* Get the user roles if present.
88+
*
89+
* @param string $identity
90+
* @return string[]
8891
*/
89-
public function getUserRoles(string $identity) : array
92+
protected function getUserRoles(string $identity) : array
9093
{
9194
if (! isset($this->config['sql_get_roles'])) {
9295
return [];
@@ -124,7 +127,13 @@ public function getUserRoles(string $identity) : array
124127
return $roles;
125128
}
126129

127-
public function getUserDetails(string $identity) : array
130+
/**
131+
* Get the user details if present.
132+
*
133+
* @param string $identity
134+
* @return string[]
135+
*/
136+
protected function getUserDetails(string $identity) : array
128137
{
129138
if (! isset($this->config['sql_get_details'])) {
130139
return [];
@@ -154,11 +163,6 @@ public function getUserDetails(string $identity) : array
154163
if (! $stmt->execute()) {
155164
return [];
156165
}
157-
158-
$details = [];
159-
foreach ($stmt->fetchAll(PDO::FETCH_NUM) as $detail) {
160-
$details[] = $detail[0];
161-
}
162-
return $details;
166+
return $stmt->fetch(PDO::FETCH_ASSOC);
163167
}
164168
}

src/UserRepositoryInterface.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,4 @@ interface UserRepositoryInterface
1919
* @param string $credential can be also a token
2020
*/
2121
public function authenticate(string $credential, string $password = null) : ?UserInterface;
22-
23-
/**
24-
* Get the user roles if present.
25-
*
26-
* @param string $identity
27-
* @return string[]
28-
*/
29-
public function getUserRoles(string $identity) : array;
30-
31-
/**
32-
* Get the user details if present.
33-
*
34-
* @param string $identity
35-
* @return string[]
36-
*/
37-
public function getUserDetails(string $identity) : array;
3822
}

test/DefaultUserFactoryTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,34 @@ public function testConstructor()
2828
$this->assertInstanceOf(DefaultUserFactory::class, $factory);
2929
}
3030

31-
public function testInvoke()
31+
public function testInvokeWithIdentity()
3232
{
3333
$factory = new DefaultUserFactory();
3434
$userFactory = $factory($this->container->reveal());
3535
$defaultUser = $userFactory('foo');
3636
$this->assertInstanceOf(DefaultUser::class, $defaultUser);
37+
$this->assertEquals('foo', $defaultUser->getIdentity());
38+
}
39+
40+
public function testInvokeWithIdentityAndRoles()
41+
{
42+
$factory = new DefaultUserFactory();
43+
$userFactory = $factory($this->container->reveal());
44+
$defaultUser = $userFactory('foo', ['admin', 'user']);
45+
$this->assertInstanceOf(DefaultUser::class, $defaultUser);
46+
$this->assertEquals('foo', $defaultUser->getIdentity());
47+
$this->assertEquals(['admin', 'user'], $defaultUser->getRoles());
48+
}
49+
50+
public function testInvokeWithIdentityAndRolesAndDetails()
51+
{
52+
$factory = new DefaultUserFactory();
53+
$userFactory = $factory($this->container->reveal());
54+
$defaultUser = $userFactory('foo', ['admin', 'user'], ['email' => 'foo@test.com']);
55+
$this->assertInstanceOf(DefaultUser::class, $defaultUser);
56+
$this->assertEquals('foo', $defaultUser->getIdentity());
57+
$this->assertEquals(['admin', 'user'], $defaultUser->getRoles());
58+
$this->assertEquals(['email' => 'foo@test.com'], $defaultUser->getDetails());
59+
$this->assertEquals('foo@test.com', $defaultUser->getDetail('email'));
3760
}
3861
}

test/TestAssets/pdo.sqlite

0 Bytes
Binary file not shown.

test/TestAssets/pdo_role.sqlite

6 KB
Binary file not shown.

test/TestAssets/pdo_roles.sqlite

12 KB
Binary file not shown.

test/TestAssets/sqlite_with_role.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
CREATE TABLE user(
22
username TEXT,
33
password TEXT,
4-
role TEXT
4+
role TEXT,
5+
email TEXT
56
);
67

7-
INSERT INTO user (username, password, role) VALUES ('test', '$2y$10$C822kPutHb8S/An9pBzJHeaN2/uqytA88O5VtTaY9m9EzWCJPDF7e', 'admin');
8+
INSERT INTO user (username, password, role, email) VALUES ('test', '$2y$10$C822kPutHb8S/An9pBzJHeaN2/uqytA88O5VtTaY9m9EzWCJPDF7e', 'admin', 'test@foo.com');

0 commit comments

Comments
 (0)