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

Commit ae6806f

Browse files
committed
Merge pull request #13 from samsonasik/fix-12
Fixes #12 : use false result check at stmt->fetchObject() to handle username not found data ( auth failure )
2 parents ce7620e + bd06cb7 commit ae6806f

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

CHANGELOG.md

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

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

5+
## 0.2.1 - TBD
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- Nothing
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- [#13](https://github.com/zendframework/zend-expressive-authentication/pull/13) fixes "Trying to get property of non-object" when no record found at PdoDatabase user repository.
26+
527
## 0.2.0 - 2017-11-27
628

729
### Added

src/UserRepository/PdoDatabase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public function authenticate(string $credential, string $password = null) : ?Use
4949

5050
$stmt = $this->pdo->prepare($sql);
5151
$stmt->bindParam(':username', $credential);
52+
$stmt->execute();
5253

53-
if (! $stmt->execute()) {
54+
$result = $stmt->fetchObject();
55+
if (! $result) {
5456
return null;
5557
}
5658

57-
$result = $stmt->fetchObject();
58-
5959
return password_verify($password, $result->{$this->config['field']['password']})
6060
? $this->generateUser($credential, $this->getRolesFromUser($credential))
6161
: null;

test/UserRepository/PdoDatabaseTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testAuthenticate()
3636
$this->assertEquals('test', $user->getUsername());
3737
}
3838

39-
public function testAuthenticateInvalidUser()
39+
public function testAuthenticateInvalidUserPassword()
4040
{
4141
$pdo = new PDO('sqlite:'. __DIR__ . '/../TestAssets/pdo.sqlite');
4242
$pdoDatabase = new PdoDatabase($pdo, [
@@ -51,6 +51,21 @@ public function testAuthenticateInvalidUser()
5151
$this->assertNull($user);
5252
}
5353

54+
public function testAuthenticateInvalidUsername()
55+
{
56+
$pdo = new PDO('sqlite:'. __DIR__ . '/../TestAssets/pdo.sqlite');
57+
$pdoDatabase = new PdoDatabase($pdo, [
58+
'table' => 'user',
59+
'field' => [
60+
'username' => 'username',
61+
'password' => 'password'
62+
]
63+
]);
64+
65+
$user = $pdoDatabase->authenticate('invalidusername', 'password');
66+
$this->assertNull($user);
67+
}
68+
5469
public function testAuthenticateWithRole()
5570
{
5671
$pdo = new PDO('sqlite:'. __DIR__ . '/../TestAssets/pdo_role.sqlite');

0 commit comments

Comments
 (0)