Reorganised debug info in account page and extracted user loading to UserManager

This commit is contained in:
simon 2018-04-27 21:59:07 -04:00
parent 113aa50d87
commit e3a650a4fc
4 changed files with 76 additions and 36 deletions

View File

@ -2,7 +2,6 @@ package net.simon987.server;
import com.mongodb.*;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.crypto.CryptoProvider;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventDispatcher;
@ -19,6 +18,7 @@ import net.simon987.server.websocket.SocketServer;
import java.io.File;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class GameServer implements Runnable {
@ -202,7 +202,6 @@ public class GameServer implements Runnable {
DB db = mongo.getDB("mar");
DBCollection worlds = db.getCollection("world");
DBCollection users = db.getCollection("user");
DBCollection server = db.getCollection("server");
BasicDBObject whereQuery = new BasicDBObject();
@ -215,13 +214,9 @@ public class GameServer implements Runnable {
}
//Load users
cursor = users.find();
while (cursor.hasNext()) {
try {
universe.addUser(User.deserialize(cursor.next()));
} catch (CancelledException e) {
e.printStackTrace();
}
ArrayList<User> userList = userManager.getUsers();
for (User user : userList) {
universe.addUser(user);
}
//Load misc server info

View File

@ -21,6 +21,11 @@ public class UserManager {
userCollection = db.getCollection("user");
}
/**
* Get and deserialise all users from mongo
*
* @return list of de-serialized users
*/
public ArrayList<User> getUsers() {
ArrayList<User> userList = new ArrayList<>();
@ -37,6 +42,12 @@ public class UserManager {
return userList;
}
/**
* Register an user and initialises its controlled unit
* @param username username
* @param password plain password
* @throws RegistrationException is username/password length is invalid
*/
public void registerUser(String username, String password) throws RegistrationException {
if (username.length() < 5 || username.length() > 20) {
@ -70,6 +81,12 @@ public class UserManager {
}
}
/**
* Validate a username/password combo
* @param username username
* @param password plain password
* @return true if combo is valid
*/
public boolean validateUser(String username, String password) {
DBObject where = new BasicDBObject();
@ -79,6 +96,12 @@ public class UserManager {
return user != null && BCrypt.checkpw(password, (String) user.get("password"));
}
/**
* Change the password of an user and immediately save it
* @param username Username
* @param newPassword New plain password
* @throws RegistrationException When password length is invalid
*/
public void changePassword(String username, String newPassword) throws RegistrationException {
if (newPassword.length() < 8 || newPassword.length() > 96) {
@ -111,19 +134,24 @@ public class UserManager {
RandomStringGenerator generator = new RandomStringGenerator(128);
String token = generator.nextString();
user.setAccessToken(token);
user.setAccessToken(token); //Token is not saved in DB, and is erased when used
LogManager.LOGGER.fine("(Web) Generated access token for " + username);
return token;
}
/**
* Validate an access token sent by the client
* @param token 128-char accesss token
* @return username of the corresponding user, null if not found
*/
public User validateAuthToken(String token) {
for (User user : GameServer.INSTANCE.getGameUniverse().getUsers()) {
if (user.getAccessToken().equals(token)) {
user.setAccessToken("");
if (user.getAccessToken() != null && user.getAccessToken().equals(token)) {
user.setAccessToken(""); //Token is erased when used
return user;
}

View File

@ -49,8 +49,27 @@
<hr>
<h5 class="card-title">Debug information</h5>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion">
<div class="card-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.
3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt
laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin
coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes
anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings
occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard
of them accusamus labore sustainable VHS.
</div>
</div>
<p>
<a class="btn btn-primary" role=button data-toggle="collapse" data-target="#debugInfo"
aria-expanded="false" aria-controls="debugInfo">Debug information</a>
</p>
<div class="collapse" id="debugInfo">
<div class="card card-block bg-light text-dark text-mono" style="padding: 1em">
<p>Moderator: $user.isModerator()</p>
<h5>CPU</h5>
<pre>$user.getCpu()</pre>
@ -68,6 +87,9 @@
<h5>User code</h5>
<pre>$user.getUserCode()</pre>
</div>
</div>
#else
## NOT LOGGED IN
<h5 class="card-title">Login</h5>
@ -115,7 +137,4 @@
#parse("footer.vm")
</body>
<script src="js/popper.min.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</html>

View File

@ -40,7 +40,5 @@
#parse("footer.vm")
</body>
<script src="js/popper.min.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</html>