mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 17:34:39 +01:00
Add Dispatch Password authentication
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package emu.grasscutter.command.commands;
|
||||
|
||||
import at.favre.lib.crypto.bcrypt.BCrypt;
|
||||
import emu.grasscutter.Configuration;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.command.Command;
|
||||
import emu.grasscutter.command.CommandHandler;
|
||||
@@ -11,7 +13,7 @@ import java.util.List;
|
||||
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
@Command(label = "account", usage = "account <create|delete> <username> [uid]", description = "commands.account.description", targetRequirement = Command.TargetRequirement.NONE)
|
||||
@Command(label = "account", usage = "account <create|delete|resetpass> <username> [uid|password] [uid] ", description = "commands.account.description", targetRequirement = Command.TargetRequirement.NONE)
|
||||
public final class AccountCommand implements CommandHandler {
|
||||
|
||||
@Override
|
||||
@@ -35,13 +37,38 @@ public final class AccountCommand implements CommandHandler {
|
||||
return;
|
||||
case "create":
|
||||
int uid = 0;
|
||||
if (args.size() > 2) {
|
||||
try {
|
||||
uid = Integer.parseInt(args.get(2));
|
||||
} catch (NumberFormatException ignored) {
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.invalid"));
|
||||
String password = "";
|
||||
|
||||
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
|
||||
if(args.size() < 3) {
|
||||
CommandHandler.sendMessage(null, "EXPERIMENTAL_RealPassword requires a password argument");
|
||||
CommandHandler.sendMessage(null, "Usage: account create <username> <password> [uid]");
|
||||
|
||||
return;
|
||||
}
|
||||
password = args.get(2);
|
||||
|
||||
if (args.size() == 4) {
|
||||
try {
|
||||
uid = Integer.parseInt(args.get(3));
|
||||
} catch (NumberFormatException ignored) {
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.invalid"));
|
||||
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
|
||||
CommandHandler.sendMessage(null, "EXPERIMENTAL_RealPassword requires argument 2 to be a password, not a uid");
|
||||
CommandHandler.sendMessage(null, "Usage: account create <username> <password> [uid]");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (args.size() > 2) {
|
||||
try {
|
||||
uid = Integer.parseInt(args.get(2));
|
||||
} catch (NumberFormatException ignored) {
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.invalid"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid);
|
||||
@@ -49,6 +76,9 @@ public final class AccountCommand implements CommandHandler {
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.exists"));
|
||||
return;
|
||||
} else {
|
||||
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
|
||||
account.setPassword(BCrypt.withDefaults().hashToString(12, password.toCharArray()));
|
||||
}
|
||||
account.addPermission("*");
|
||||
account.save(); // Save account to database.
|
||||
|
||||
@@ -74,6 +104,37 @@ public final class AccountCommand implements CommandHandler {
|
||||
// Finally, we do the actual deletion.
|
||||
DatabaseHelper.deleteAccount(toDelete);
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.delete"));
|
||||
return;
|
||||
case "resetpass":
|
||||
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword != true) {
|
||||
CommandHandler.sendMessage(null, "resetpass requires EXPERIMENTAL_RealPassword to be true.");
|
||||
return;
|
||||
}
|
||||
|
||||
if(args.size() != 3) {
|
||||
CommandHandler.sendMessage(null, "Invalid Args");
|
||||
CommandHandler.sendMessage(null, "Usage: account resetpass <username> <password>");
|
||||
return;
|
||||
}
|
||||
|
||||
Account toUpdate = DatabaseHelper.getAccountByName(username);
|
||||
|
||||
if (toUpdate == null) {
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.no_account"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the player for the account.
|
||||
// If that player is currently online, we kick them before proceeding with the deletion.
|
||||
Player uPlayer = Grasscutter.getGameServer().getPlayerByAccountId(toUpdate.getId());
|
||||
if (uPlayer != null) {
|
||||
uPlayer.getSession().close();
|
||||
}
|
||||
|
||||
toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray()));
|
||||
toUpdate.save();
|
||||
CommandHandler.sendMessage(null, "Password Updated.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user