Prevent blank text when logging in

This commit is contained in:
KingRainbow44 2023-05-04 00:09:57 -04:00
parent 2d12e0a4d9
commit afd22a9bcd
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE

View File

@ -1,244 +1,245 @@
package emu.grasscutter.game; package emu.grasscutter.game;
import static emu.grasscutter.config.Configuration.ACCOUNT; import static emu.grasscutter.config.Configuration.ACCOUNT;
import static emu.grasscutter.config.Configuration.LANGUAGE; import static emu.grasscutter.config.Configuration.LANGUAGE;
import dev.morphia.annotations.*; import dev.morphia.annotations.*;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import java.util.*; import java.util.*;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.bson.Document; import org.bson.Document;
@Entity(value = "accounts", useDiscriminator = false) @Entity(value = "accounts", useDiscriminator = false)
public class Account { public class Account {
@Id private String id; @Id private String id;
@Indexed(options = @IndexOptions(unique = true)) @Indexed(options = @IndexOptions(unique = true))
@Collation(locale = "simple", caseLevel = true) @Collation(locale = "simple", caseLevel = true)
private String username; private String username;
private String password; // Unused for now private String password; // Unused for now
private int reservedPlayerId; private int reservedPlayerId;
private String email; private String email;
private String token; private String token;
private String sessionKey; // Session token for dispatch server private String sessionKey; // Session token for dispatch server
private final List<String> permissions; private final List<String> permissions;
private Locale locale; private Locale locale;
private String banReason; private String banReason;
private int banEndTime; private int banEndTime;
private int banStartTime; private int banStartTime;
private boolean isBanned; private boolean isBanned;
@Deprecated @Deprecated
public Account() { public Account() {
this.permissions = new ArrayList<>(); this.permissions = new ArrayList<>();
this.locale = LANGUAGE; this.locale = LANGUAGE;
} }
public static boolean permissionMatchesWildcard(String wildcard, String[] permissionParts) { public static boolean permissionMatchesWildcard(String wildcard, String[] permissionParts) {
String[] wildcardParts = wildcard.split("\\."); String[] wildcardParts = wildcard.split("\\.");
if (permissionParts.length if (permissionParts.length
< wildcardParts.length) { // A longer wildcard can never match a shorter permission < wildcardParts.length) { // A longer wildcard can never match a shorter permission
return false; return false;
} }
for (int i = 0; i < wildcardParts.length; i++) { for (int i = 0; i < wildcardParts.length; i++) {
switch (wildcardParts[i]) { switch (wildcardParts[i]) {
case "**": // Recursing match case "**": // Recursing match
return true; return true;
case "*": // Match only one layer case "*": // Match only one layer
if (i >= (permissionParts.length - 1)) { if (i >= (permissionParts.length - 1)) {
return true; return true;
} }
break; break;
default: // This layer isn't a wildcard, it needs to match exactly default: // This layer isn't a wildcard, it needs to match exactly
if (!wildcardParts[i].equals(permissionParts[i])) { if (!wildcardParts[i].equals(permissionParts[i])) {
return false; return false;
} }
} }
} }
// At this point the wildcard will have matched every layer, but if it is shorter then the // At this point the wildcard will have matched every layer, but if it is shorter then the
// permission then this is not a match at this point (no **). // permission then this is not a match at this point (no **).
return (wildcardParts.length == permissionParts.length); return (wildcardParts.length == permissionParts.length);
} }
public String getId() { public String getId() {
return id; return id;
} }
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
} }
public String getUsername() { public String getUsername() {
return username; return username;
} }
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.username = username;
} }
public String getPassword() { public String getPassword() {
return password; return password;
} }
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public String getToken() { public String getToken() {
return token; return token;
} }
public void setToken(String token) { public void setToken(String token) {
this.token = token; this.token = token;
} }
public int getReservedPlayerUid() { public int getReservedPlayerUid() {
return this.reservedPlayerId; return this.reservedPlayerId;
} }
public void setReservedPlayerUid(int playerId) { public void setReservedPlayerUid(int playerId) {
this.reservedPlayerId = playerId; this.reservedPlayerId = playerId;
} }
public String getEmail() { public String getEmail() {
if (email != null && !email.isEmpty()) { if (email != null && !email.isEmpty()) {
return email; return email;
} else { } else {
return ""; // As of game version 3.5+, only the email is displayed to a user.
} return this.getUsername() + "@grasscutter.io";
} }
}
public void setEmail(String email) {
this.email = email; public void setEmail(String email) {
} this.email = email;
}
public String getSessionKey() {
return this.sessionKey; public String getSessionKey() {
} return this.sessionKey;
}
public String generateSessionKey() {
this.sessionKey = Utils.bytesToHex(Crypto.createSessionKey(32)); public String generateSessionKey() {
this.save(); this.sessionKey = Utils.bytesToHex(Crypto.createSessionKey(32));
return this.sessionKey; this.save();
} return this.sessionKey;
}
public Locale getLocale() {
return locale; public Locale getLocale() {
} return locale;
}
public void setLocale(Locale locale) {
this.locale = locale; public void setLocale(Locale locale) {
} this.locale = locale;
}
public String getBanReason() {
return banReason; public String getBanReason() {
} return banReason;
}
public void setBanReason(String banReason) {
this.banReason = banReason; public void setBanReason(String banReason) {
} this.banReason = banReason;
}
public int getBanEndTime() {
return banEndTime; public int getBanEndTime() {
} return banEndTime;
}
public void setBanEndTime(int banEndTime) {
this.banEndTime = banEndTime; public void setBanEndTime(int banEndTime) {
} this.banEndTime = banEndTime;
}
public int getBanStartTime() {
return banStartTime; public int getBanStartTime() {
} return banStartTime;
}
public void setBanStartTime(int banStartTime) {
this.banStartTime = banStartTime; public void setBanStartTime(int banStartTime) {
} this.banStartTime = banStartTime;
}
public boolean isBanned() {
if (banEndTime > 0 && banEndTime < System.currentTimeMillis() / 1000) { public boolean isBanned() {
this.isBanned = false; if (banEndTime > 0 && banEndTime < System.currentTimeMillis() / 1000) {
this.banEndTime = 0; this.isBanned = false;
this.banStartTime = 0; this.banEndTime = 0;
this.banReason = null; this.banStartTime = 0;
save(); this.banReason = null;
} save();
}
return isBanned;
} return isBanned;
}
public void setBanned(boolean isBanned) {
this.isBanned = isBanned; public void setBanned(boolean isBanned) {
} this.isBanned = isBanned;
}
/** The collection of a player's permissions. */
public List<String> getPermissions() { /** The collection of a player's permissions. */
return this.permissions; public List<String> getPermissions() {
} return this.permissions;
}
public boolean addPermission(String permission) {
if (this.permissions.contains(permission)) return false; public boolean addPermission(String permission) {
this.permissions.add(permission); if (this.permissions.contains(permission)) return false;
return true; this.permissions.add(permission);
} return true;
}
public boolean hasPermission(String permission) {
if (permission.isEmpty()) return true; public boolean hasPermission(String permission) {
if (this.permissions.contains("*") && this.permissions.size() == 1) return true; if (permission.isEmpty()) return true;
if (this.permissions.contains("*") && this.permissions.size() == 1) return true;
// Add default permissions if it doesn't exist
List<String> permissions = // Add default permissions if it doesn't exist
Stream.of(this.permissions, Arrays.asList(ACCOUNT.defaultPermissions)) List<String> permissions =
.flatMap(Collection::stream) Stream.of(this.permissions, Arrays.asList(ACCOUNT.defaultPermissions))
.distinct() .flatMap(Collection::stream)
.toList(); .distinct()
.toList();
if (permissions.contains(permission)) return true;
if (permissions.contains(permission)) return true;
String[] permissionParts = permission.split("\\.");
for (String p : permissions) { String[] permissionParts = permission.split("\\.");
if (p.startsWith("-") && permissionMatchesWildcard(p.substring(1), permissionParts)) for (String p : permissions) {
return false; if (p.startsWith("-") && permissionMatchesWildcard(p.substring(1), permissionParts))
if (permissionMatchesWildcard(p, permissionParts)) return true; return false;
} if (permissionMatchesWildcard(p, permissionParts)) return true;
}
return permissions.contains("*");
} return permissions.contains("*");
}
public boolean removePermission(String permission) {
return this.permissions.remove(permission); public boolean removePermission(String permission) {
} return this.permissions.remove(permission);
}
public void clearPermission() {
this.permissions.clear(); public void clearPermission() {
} this.permissions.clear();
}
// TODO make unique
public String generateLoginToken() { // TODO make unique
this.token = Utils.bytesToHex(Crypto.createSessionKey(32)); public String generateLoginToken() {
this.save(); this.token = Utils.bytesToHex(Crypto.createSessionKey(32));
return this.token; this.save();
} return this.token;
}
public void save() {
DatabaseHelper.saveAccount(this); public void save() {
} DatabaseHelper.saveAccount(this);
}
@PreLoad
public void onLoad(Document document) { @PreLoad
// Grant the superuser permissions to accounts created before the permissions update public void onLoad(Document document) {
if (!document.containsKey("permissions")) { // Grant the superuser permissions to accounts created before the permissions update
this.addPermission("*"); if (!document.containsKey("permissions")) {
} this.addPermission("*");
}
// Set account default language as server default language
if (!document.containsKey("locale")) { // Set account default language as server default language
this.locale = LANGUAGE; if (!document.containsKey("locale")) {
} this.locale = LANGUAGE;
} }
} }
}