mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-18 18:26:42 +00:00
Users can change their password
This commit is contained in:
parent
3492e133e1
commit
dc034d1437
@ -99,7 +99,7 @@ public class Main {
|
||||
|
||||
if (username != null && password != null) {
|
||||
if (GameServer.INSTANCE.getUserManager().validateUser(username, password)) {
|
||||
AlertMessage[] messages = {new AlertMessage("Logged in as " + username, AlertType.INFO)};
|
||||
AlertMessage[] messages = {new AlertMessage("Logged in as " + username, AlertType.SUCCESS)};
|
||||
request.session().attribute("messages", messages);
|
||||
request.session().attribute("username", username);
|
||||
|
||||
@ -124,6 +124,40 @@ public class Main {
|
||||
return null;
|
||||
});
|
||||
|
||||
Spark.post("change_password", (request, response) -> {
|
||||
|
||||
String username = request.session().attribute("username");
|
||||
String currentPassword = request.queryParams("password");
|
||||
String newPassword = request.queryParams("new_password");
|
||||
String newPasswordRepeat = request.queryParams("new_password_repeat");
|
||||
|
||||
if (newPassword.equals(newPasswordRepeat)) {
|
||||
|
||||
if (username != null && GameServer.INSTANCE.getUserManager().validateUser(username, currentPassword)) {
|
||||
|
||||
try {
|
||||
GameServer.INSTANCE.getUserManager().changePassword(username, newPassword);
|
||||
AlertMessage[] messages = {new AlertMessage("Changed password", AlertType.SUCCESS)};
|
||||
request.session().attribute("messages", messages);
|
||||
} catch (RegistrationException e) {
|
||||
AlertMessage[] messages = {new AlertMessage(e.getMessage(), AlertType.DANGER)};
|
||||
request.session().attribute("messages", messages);
|
||||
}
|
||||
|
||||
} else {
|
||||
AlertMessage[] messages = {new AlertMessage("Invalid password", AlertType.DANGER)};
|
||||
request.session().attribute("messages", messages);
|
||||
}
|
||||
} else {
|
||||
AlertMessage[] messages = {new AlertMessage("Passwords did not match", AlertType.DANGER)};
|
||||
request.session().attribute("messages", messages);
|
||||
}
|
||||
|
||||
|
||||
response.redirect("/account");
|
||||
return null;
|
||||
});
|
||||
|
||||
Spark.after((request, response) -> response.header("Content-Encoding", "gzip"));
|
||||
}
|
||||
|
||||
|
@ -76,4 +76,19 @@ public class UserManager {
|
||||
DBObject user = userCollection.findOne(where);
|
||||
return user != null && BCrypt.checkpw(password, (String) user.get("password"));
|
||||
}
|
||||
|
||||
public void changePassword(String username, String newPassword) throws RegistrationException {
|
||||
|
||||
if (newPassword.length() < 8 || newPassword.length() > 96) {
|
||||
throw new RegistrationException("Password must be 8-96 characters");
|
||||
}
|
||||
|
||||
User user = GameServer.INSTANCE.getGameUniverse().getUser(username);
|
||||
|
||||
String salt = BCrypt.gensalt();
|
||||
String hashedPassword = BCrypt.hashpw(newPassword, salt);
|
||||
user.setPassword(hashedPassword);
|
||||
|
||||
userCollection.save(user.mongoSerialise()); //Save new password immediately
|
||||
}
|
||||
}
|
||||
|
@ -80,3 +80,11 @@
|
||||
flex: 0 0 16.66667%;
|
||||
max-width: 16.66667%;
|
||||
}
|
||||
|
||||
.col-sm-3 {
|
||||
-webkit-box-flex: 0;
|
||||
-webkit-flex: 0 0 25%;
|
||||
-ms-flex: 0 0 25%;
|
||||
flex: 0 0 25%;
|
||||
max-width: 25%;
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
#if($session.attribute("username"))
|
||||
## ALREADY LOGGED IN
|
||||
<p>Logged in as <strong>$session.attribute("username")</strong></p>
|
||||
<a href="/logout" class="btn btn-warning text-mono"><i class="mi">eject</i> Logout</a>
|
||||
|
||||
<hr>
|
||||
|
||||
@ -38,12 +39,12 @@
|
||||
<div class="col">
|
||||
<div class="form-group">
|
||||
<input title="Repeat new password" type="password" placeholder="Repeat new password"
|
||||
name="new_password" class="form-control">
|
||||
name="new_password_repeat" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-outline-primary text-mono">Register</button>
|
||||
<button type="submit" class="btn btn-outline-primary text-mono">Change password</button>
|
||||
</form>
|
||||
|
||||
<hr>
|
||||
@ -53,9 +54,6 @@
|
||||
<h5>CPU</h5>
|
||||
<pre>$user.getCpu()</pre>
|
||||
|
||||
<h5>Code</h5>
|
||||
<pre>$user.getUserCode()</pre>
|
||||
|
||||
<h5>Controlled unit</h5>
|
||||
<p>id: $user.getControlledUnit().getObjectId()</p>
|
||||
<p>energy: $user.getControlledUnit().getEnergy()</p>
|
||||
@ -63,8 +61,12 @@
|
||||
<p>y: $user.getControlledUnit().getY()</p>
|
||||
<p>console mode: $user.getControlledUnit().getConsoleMode()</p>
|
||||
<p>kbBuffer size: $user.getControlledUnit().getKeyboardBuffer().size()</p>
|
||||
|
||||
<h5>World</h5>
|
||||
<pre>$user.getControlledUnit().getWorld()</pre>
|
||||
|
||||
<h5>User code</h5>
|
||||
<pre>$user.getUserCode()</pre>
|
||||
</div>
|
||||
#else
|
||||
## NOT LOGGED IN
|
||||
|
@ -43,8 +43,8 @@
|
||||
<img src="images/github-logo.png" class="feature-image">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Free and open source</h5>
|
||||
<p class="card-text">The project's source code and the game's documentation are available on
|
||||
GitHub.</p>
|
||||
<p class="card-text">The project's source code and the game's documentation are
|
||||
available <a href="https://github.com/simon987/Much-Assembly-Required">on GitHub</a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -114,24 +114,24 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-2">
|
||||
<div class="col-sm-3">
|
||||
<button id="colorButton" class="btn btn-outline-info text-mono btn-shadow regular-screen">
|
||||
Color
|
||||
Invert colors
|
||||
</button>
|
||||
<button onclick="$('#colorButton').click()"
|
||||
class="btn btn-outline-info text-mono btn-shadow small-screen"><i class="mi">invert_colors_off</i>
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-sm-2">
|
||||
<button id="scrollButton" class="btn btn-outline-info text-mono btn-shadow regular-screen">
|
||||
Scroll
|
||||
<div class="col-sm-3">
|
||||
<button id="scrollButton" class="btn btn-outline-info text-mono btn-shadow regular-screen">Auto
|
||||
scrolling
|
||||
</button>
|
||||
<button onclick="$('#scrollButton').click()"
|
||||
class="btn btn-outline-info text-mono btn-shadow small-screen"><i
|
||||
class="mi">swap_vert</i></button>
|
||||
</div>
|
||||
<div class="col-sm-2">
|
||||
<button id="resetButton" class="btn btn-danger text-mono btn-shadow regular-screen">Reset
|
||||
<button id="resetButton" class="btn btn-danger text-mono btn-shadow regular-screen">Clear
|
||||
</button>
|
||||
<button onclick="$('#resetButton').click()"
|
||||
class="btn btn-danger text-mono btn-shadow small-screen"><i class="mi">replay</i>
|
||||
|
Loading…
x
Reference in New Issue
Block a user