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

View File

@ -21,6 +21,11 @@ public class UserManager {
userCollection = db.getCollection("user"); userCollection = db.getCollection("user");
} }
/**
* Get and deserialise all users from mongo
*
* @return list of de-serialized users
*/
public ArrayList<User> getUsers() { public ArrayList<User> getUsers() {
ArrayList<User> userList = new ArrayList<>(); ArrayList<User> userList = new ArrayList<>();
@ -37,6 +42,12 @@ public class UserManager {
return userList; 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 { public void registerUser(String username, String password) throws RegistrationException {
if (username.length() < 5 || username.length() > 20) { 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) { public boolean validateUser(String username, String password) {
DBObject where = new BasicDBObject(); DBObject where = new BasicDBObject();
@ -79,6 +96,12 @@ public class UserManager {
return user != null && BCrypt.checkpw(password, (String) user.get("password")); 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 { public void changePassword(String username, String newPassword) throws RegistrationException {
if (newPassword.length() < 8 || newPassword.length() > 96) { if (newPassword.length() < 8 || newPassword.length() > 96) {
@ -111,19 +134,24 @@ public class UserManager {
RandomStringGenerator generator = new RandomStringGenerator(128); RandomStringGenerator generator = new RandomStringGenerator(128);
String token = generator.nextString(); 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); LogManager.LOGGER.fine("(Web) Generated access token for " + username);
return token; 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) { public User validateAuthToken(String token) {
for (User user : GameServer.INSTANCE.getGameUniverse().getUsers()) { for (User user : GameServer.INSTANCE.getGameUniverse().getUsers()) {
if (user.getAccessToken().equals(token)) { if (user.getAccessToken() != null && user.getAccessToken().equals(token)) {
user.setAccessToken(""); user.setAccessToken(""); //Token is erased when used
return user; return user;
} }

View File

@ -49,25 +49,47 @@
<hr> <hr>
<h5 class="card-title">Debug information</h5> <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion">
<div class="card card-block bg-light text-dark text-mono" style="padding: 1em"> <div class="card-body">
<h5>CPU</h5> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.
<pre>$user.getCpu()</pre> 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
<h5>Controlled unit</h5> coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes
<p>id: $user.getControlledUnit().getObjectId()</p> anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings
<p>energy: $user.getControlledUnit().getEnergy()</p> occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard
<p>x: $user.getControlledUnit().getX()</p> of them accusamus labore sustainable VHS.
<p>y: $user.getControlledUnit().getY()</p> </div>
<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> </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>
<h5>Controlled unit</h5>
<p>id: $user.getControlledUnit().getObjectId()</p>
<p>energy: $user.getControlledUnit().getEnergy()</p>
<p>x: $user.getControlledUnit().getX()</p>
<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>
</div>
#else #else
## NOT LOGGED IN ## NOT LOGGED IN
<h5 class="card-title">Login</h5> <h5 class="card-title">Login</h5>
@ -115,7 +137,4 @@
#parse("footer.vm") #parse("footer.vm")
</body> </body>
<script src="js/popper.min.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</html> </html>

View File

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