Improved login: now you can login using name or email

Close #873
This commit is contained in:
Dmitry Khomutov 2015-03-17 15:11:05 +06:00 committed by Tobias van Beek
commit f0439782ba
13 changed files with 69 additions and 3 deletions

View file

@ -59,7 +59,7 @@ class UserStoreBase extends Store
/**
* Returns a User model by Email.
* @param mixed $value
* @param string $value
* @param string $useConnection
* @throws HttpException
* @return \@appNamespace\Model\User|null
@ -82,4 +82,30 @@ class UserStoreBase extends Store
return null;
}
/**
* Returns a User model by Email.
* @param string $value
* @param string $useConnection
* @throws HttpException
* @return \@appNamespace\Model\User|null
*/
public function getByLoginOrEmail($value, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM `user` WHERE `name` = :value OR `email` = :value LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepare($query);
$stmt->bindValue(':value', $value);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new User($data);
}
}
return null;
}
}