Made e-mail and username case insensitive.

This commit is contained in:
flash 2018-03-26 04:24:32 +02:00
parent 13c1c0722e
commit 610a6a8b72
2 changed files with 18 additions and 5 deletions

View file

@ -84,9 +84,9 @@ switch ($mode) {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
try {
$user = User::where('username', $username)->orWhere('email', $username)->firstOrFail();
} catch (ModelNotFoundException $e) {
$user = User::findLogin($username);
if ($user === null) {
LoginAttempt::recordFail($ipAddress, null, $user_agent);
$auth_login_error = 'Invalid username or password!';
break;

View file

@ -40,6 +40,14 @@ class User extends Model
return $user;
}
public static function findLogin(string $usernameOrEmail): ?User
{
$usernameOrEmail = strtolower($usernameOrEmail);
return User::whereRaw("LOWER(`username`) = '{$usernameOrEmail}'")
->orWhere('email', $usernameOrEmail)
->first();
}
public static function validateUsername(string $username, bool $checkInUse = false): string
{
$username_length = strlen($username);
@ -68,7 +76,7 @@ class User extends Model
return 'spacing';
}
if ($checkInUse && static::where('username', $username)->count() > 0) {
if ($checkInUse && static::whereRaw("LOWER(`username`) = LOWER('{$username}')")->count() > 0) {
return 'in-use';
}
@ -85,7 +93,7 @@ class User extends Model
return 'dns';
}
if ($checkInUse && static::where('email', $email)->count() > 0) {
if ($checkInUse && static::whereRaw("LOWER(`email`) = LOWER('{$email}')")->count() > 0) {
return 'in-use';
}
@ -194,6 +202,11 @@ class User extends Model
$this->attributes['password'] = password_hash($password, self::PASSWORD_HASH_ALGO);
}
public function setEmailAttribute(string $email): void
{
$this->attributes['email'] = strtolower($email);
}
public function sessions()
{
return $this->hasMany(Session::class, 'user_id');