Permalink
Browse files

UserManager: Passwords is service

  • Loading branch information...
dg committed Jul 15, 2018
1 parent 3bdfe2d commit 67c6150a6ff0d1c5cb51390780ec3f6eb16941d8
Showing with 9 additions and 5 deletions.
  1. +9 −5 app/model/UserManager.php
@@ -26,10 +26,14 @@ class UserManager implements Nette\Security\IAuthenticator
/** @var Nette\Database\Context */
private $database;
/** @var Passwords */
private $passwords;
public function __construct(Nette\Database\Context $database)
public function __construct(Nette\Database\Context $database, Passwords $passwords)
{
$this->database = $database;
$this->passwords = $passwords;
}
@@ -48,12 +52,12 @@ public function authenticate(array $credentials): Nette\Security\IIdentity
if (!$row) {
throw new Nette\Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
} elseif (!Passwords::verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
} elseif (!$this->passwords->verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
} elseif (Passwords::needsRehash($row[self::COLUMN_PASSWORD_HASH])) {
} elseif ($this->passwords->needsRehash($row[self::COLUMN_PASSWORD_HASH])) {
$row->update([
self::COLUMN_PASSWORD_HASH => Passwords::hash($password),
self::COLUMN_PASSWORD_HASH => $this->passwords->hash($password),
]);
}
@@ -72,7 +76,7 @@ public function add(string $username, string $email, string $password): void
try {
$this->database->table(self::TABLE_NAME)->insert([
self::COLUMN_NAME => $username,
self::COLUMN_PASSWORD_HASH => Passwords::hash($password),
self::COLUMN_PASSWORD_HASH => $this->passwords->hash($password),
self::COLUMN_EMAIL => $email,
]);
} catch (Nette\Database\UniqueConstraintViolationException $e) {

0 comments on commit 67c6150

Please sign in to comment.