Skip to content

Commit 0dfe6d0

Browse files
committed
Change login to use username instead of email
1 parent 01e03f8 commit 0dfe6d0

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

src/controllers/User/Login.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
use \CarlBennett\MVC\Libraries\View;
1818

1919
class Login extends Controller {
20-
2120
public function &run(Router &$router, View &$view, array &$args) {
22-
2321
$model = new UserLoginModel();
2422

2523
$model->csrf_id = mt_rand();
@@ -44,30 +42,38 @@ protected function tryLogin(Router &$router, UserLoginModel &$model) {
4442
if (!isset(Common::$database)) {
4543
Common::$database = DatabaseDriver::getDatabaseObject();
4644
}
45+
4746
$data = $router->getRequestBodyArray();
4847
$csrf_id = (isset($data["csrf_id" ]) ? $data["csrf_id" ] : null);
4948
$csrf_token = (isset($data["csrf_token"]) ? $data["csrf_token"] : null);
5049
$csrf_valid = CSRF::validate($csrf_id, $csrf_token);
51-
$email = (isset($data["email" ]) ? $data["email" ] : null);
50+
$username = (isset($data["username" ]) ? $data["username" ] : null);
5251
$password = (isset($data["password" ]) ? $data["password" ] : null);
52+
53+
$model->username = $username;
54+
5355
if (!$csrf_valid) {
5456
$model->error = "INVALID_CSRF";
5557
return;
5658
}
5759
CSRF::invalidate($csrf_id);
60+
5861
if ( isset( Authentication::$user )) {
5962
$model->error = "ALREADY_LOGGED_IN";
60-
} else if (empty($email)) {
61-
$model->error = "EMPTY_EMAIL";
63+
} else if (empty($username)) {
64+
$model->error = "EMPTY_USERNAME";
6265
} else if (Common::$config->bnetdocs->user_login_disabled) {
6366
$model->error = "LOGIN_DISABLED";
6467
}
68+
6569
if ($model->error) return;
70+
6671
try {
67-
$user = new User(User::findIdByEmail($email));
72+
$user = new User(User::findIdByUsername($username));
6873
} catch (UserNotFoundException $e) {
6974
$user = null;
7075
}
76+
7177
if (!$user) {
7278
$model->error = "USER_NOT_FOUND";
7379
} else if ($user->getOptionsBitmask() & User::OPTION_DISABLED) {
@@ -77,16 +83,20 @@ protected function tryLogin(Router &$router, UserLoginModel &$model) {
7783
} else if (!$user->checkPassword($password)) {
7884
$model->error = "PASSWORD_INCORRECT";
7985
}
86+
8087
if ($model->error) return;
81-
$model->error = false;
82-
$model->password = '';
88+
$model->error = false;
89+
8390
Authentication::login( $user );
91+
8492
Logger::logEvent(
8593
EventTypes::USER_LOGIN,
8694
($user ? $user->getId() : null),
8795
getenv("REMOTE_ADDR"),
88-
json_encode(["error" => $model->error])
96+
json_encode([
97+
"error" => $model->error,
98+
"username" => $username,
99+
])
89100
);
90101
}
91-
92102
}

src/models/User/Login.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class Login extends Model {
88

9-
public $bad_email;
10-
public $bad_password;
11-
public $email;
12-
public $password;
9+
public $csrf_id;
10+
public $csrf_token;
11+
public $error;
12+
public $username;
1313

1414
}

src/templates/User/Login.phtml

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ switch ($this->getContext()->error) {
1616
case "ALREADY_LOGGED_IN":
1717
$message = "You are already logged in, you must log out first.";
1818
break;
19-
case "EMPTY_EMAIL":
20-
$message = "The email address was left blank.";
19+
case "EMPTY_USERNAME":
20+
$message = "The username was left blank.";
2121
break;
2222
case "USER_NOT_FOUND":
2323
case "PASSWORD_INCORRECT":
24-
$message = "Incorrect email address or password.";
24+
$message = "Incorrect username or password.";
2525
break;
2626
case "USER_DISABLED":
2727
$message = "The account has been administratively disabled.";
@@ -48,23 +48,23 @@ require("./header.inc.phtml");
4848
</section>
4949
<?php } ?>
5050
<form method="POST" action="?">
51-
<input type="hidden" name="csrf_id" value="<?php echo $this->getContext()->csrf_id; ?>"/>
52-
<input type="hidden" name="csrf_token" value="<?php echo $this->getContext()->csrf_token; ?>"/>
51+
<input type="hidden" name="csrf_id" value="<?=filter_var($this->getContext()->csrf_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"/>
52+
<input type="hidden" name="csrf_token" value="<?=filter_var($this->getContext()->csrf_token, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"/>
5353
<section>
5454
<table>
5555
<thead></thead>
5656
<tbody>
5757
<tr>
5858
<td>
59-
<label for="email">Email address:</label><br/>
59+
<label for="username">Username:</label><br/>
6060
<input
61-
type="email"
62-
name="email"
63-
id="email"
64-
value=""
61+
type="text"
62+
name="username"
63+
id="username"
64+
value="<?=filter_var($this->getContext()->username, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"
6565
tabindex="1"
6666
required
67-
autocomplete="email"
67+
autocomplete="username"
6868
autofocus="autofocus"
6969
/>
7070
</td><td>

0 commit comments

Comments
 (0)