2022-04-17 12:43:07 +00:00
|
|
|
package emu.grasscutter.game;
|
|
|
|
|
2022-04-21 06:07:05 +00:00
|
|
|
import dev.morphia.annotations.*;
|
2022-04-17 12:43:07 +00:00
|
|
|
import emu.grasscutter.database.DatabaseHelper;
|
|
|
|
import emu.grasscutter.utils.Crypto;
|
|
|
|
import emu.grasscutter.utils.Utils;
|
|
|
|
|
2022-04-19 04:38:19 +00:00
|
|
|
import java.util.ArrayList;
|
2022-04-19 03:46:04 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-04-23 02:23:32 +00:00
|
|
|
import org.bson.Document;
|
|
|
|
|
2022-04-19 05:00:01 +00:00
|
|
|
import com.mongodb.DBObject;
|
|
|
|
|
2022-04-21 01:11:57 +00:00
|
|
|
@Entity(value = "accounts", useDiscriminator = false)
|
2022-04-17 12:43:07 +00:00
|
|
|
public class Account {
|
|
|
|
@Id private String id;
|
|
|
|
|
|
|
|
@Indexed(options = @IndexOptions(unique = true))
|
|
|
|
@Collation(locale = "simple", caseLevel = true)
|
|
|
|
private String username;
|
|
|
|
private String password; // Unused for now
|
|
|
|
|
2022-04-21 04:50:33 +00:00
|
|
|
@AlsoLoad("playerUid") private int playerId;
|
2022-04-17 12:43:07 +00:00
|
|
|
private String email;
|
|
|
|
|
|
|
|
private String token;
|
|
|
|
private String sessionKey; // Session token for dispatch server
|
2022-04-19 03:46:04 +00:00
|
|
|
private List<String> permissions;
|
2022-04-21 06:07:05 +00:00
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
@Deprecated
|
2022-04-19 04:38:19 +00:00
|
|
|
public Account() {
|
|
|
|
this.permissions = new ArrayList<>();
|
|
|
|
}
|
2022-04-17 12:43:07 +00:00
|
|
|
|
|
|
|
public String getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setId(String id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername() {
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUsername(String username) {
|
|
|
|
this.username = username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPassword(String password) {
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken() {
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setToken(String token) {
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:50:33 +00:00
|
|
|
public int getPlayerUid() {
|
2022-04-17 12:43:07 +00:00
|
|
|
return this.playerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPlayerId(int playerId) {
|
|
|
|
this.playerId = playerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getEmail() {
|
2022-05-01 06:31:39 +00:00
|
|
|
if(email != null && !email.isEmpty()) {
|
|
|
|
return email;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setEmail(String email) {
|
|
|
|
this.email = email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSessionKey() {
|
|
|
|
return this.sessionKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String generateSessionKey() {
|
|
|
|
this.sessionKey = Utils.bytesToHex(Crypto.createSessionKey(32));
|
|
|
|
this.save();
|
|
|
|
return this.sessionKey;
|
|
|
|
}
|
2022-04-19 03:46:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The collection of a player's permissions.
|
|
|
|
*/
|
|
|
|
public List<String> getPermissions() {
|
|
|
|
return this.permissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean addPermission(String permission) {
|
|
|
|
if(this.permissions.contains(permission)) return false;
|
|
|
|
this.permissions.add(permission); return true;
|
|
|
|
}
|
2022-04-19 10:17:19 +00:00
|
|
|
|
|
|
|
public boolean hasPermission(String permission) {
|
2022-04-27 06:45:53 +00:00
|
|
|
return this.permissions.contains(permission) ||
|
|
|
|
this.permissions.contains("*") ||
|
|
|
|
(this.permissions.contains("player") || this.permissions.contains("player.*")) && permission.startsWith("player.") ||
|
|
|
|
(this.permissions.contains("server") || this.permissions.contains("server.*")) && permission.startsWith("server.");
|
2022-04-19 10:17:19 +00:00
|
|
|
}
|
2022-04-19 03:46:04 +00:00
|
|
|
|
|
|
|
public boolean removePermission(String permission) {
|
|
|
|
return this.permissions.remove(permission);
|
|
|
|
}
|
2022-04-17 12:43:07 +00:00
|
|
|
|
|
|
|
// TODO make unique
|
|
|
|
public String generateLoginToken() {
|
|
|
|
this.token = Utils.bytesToHex(Crypto.createSessionKey(32));
|
|
|
|
this.save();
|
|
|
|
return this.token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save() {
|
|
|
|
DatabaseHelper.saveAccount(this);
|
|
|
|
}
|
2022-04-23 02:23:32 +00:00
|
|
|
|
2022-04-19 05:00:01 +00:00
|
|
|
@PreLoad
|
2022-04-23 02:23:32 +00:00
|
|
|
public void onLoad(Document document) {
|
2022-04-19 05:00:01 +00:00
|
|
|
// Grant the superuser permissions to accounts created before the permissions update
|
2022-04-23 02:23:32 +00:00
|
|
|
if (!document.containsKey("permissions")) {
|
2022-04-19 05:00:01 +00:00
|
|
|
this.addPermission("*");
|
|
|
|
}
|
|
|
|
}
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|