From fbf7aaedb434cb7dbbec0faab3050fe2a3e0b16a Mon Sep 17 00:00:00 2001 From: Luke Date: Sat, 3 Jul 2021 00:11:01 +0200 Subject: [PATCH] add changepw todo: cmd nicht in der konsole loggen @hydrant --- .../assets/js/chat/main.js | 7 +++- .../Commands/UserCommands.cs | 38 +++++++++++++++++++ ReallifeGamemode.Server/Events/Register.cs | 7 ++-- ReallifeGamemode.Server/Util/GlobalHelper.cs | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/ReallifeGamemode.Client/assets/js/chat/main.js b/ReallifeGamemode.Client/assets/js/chat/main.js index 4fa78a5c..6c089f1b 100644 --- a/ReallifeGamemode.Client/assets/js/chat/main.js +++ b/ReallifeGamemode.Client/assets/js/chat/main.js @@ -163,7 +163,12 @@ $(document).ready(function () { chat.historyMsgs.pop(); } - chat.historyMsgs.unshift(value); + if (value.toLowerCase().startsWith("/changepw") && value.length >= "/changepw ".length) { + chat.historyMsgs.unshift("/changepw * *"); + } else { + chat.historyMsgs.unshift(value); + } + chat.currentIndex = 0; var elmnt = document.getElementById("chat_messages"); diff --git a/ReallifeGamemode.Server/Commands/UserCommands.cs b/ReallifeGamemode.Server/Commands/UserCommands.cs index 132eaa23..7c307c3b 100644 --- a/ReallifeGamemode.Server/Commands/UserCommands.cs +++ b/ReallifeGamemode.Server/Commands/UserCommands.cs @@ -22,6 +22,7 @@ namespace ReallifeGamemode.Server.Commands { private static readonly ILogger logger = LogManager.GetLogger(); + private const int PASSWORD_CHANGE_FEE = 5000; private const int SMS_PRICE = 5; [Command("rent", "~m~rent stop")] @@ -114,6 +115,43 @@ namespace ReallifeGamemode.Server.Commands player.SendChatMessage("~y~[EVENTPORT] ~s~Du hast dich zum Event teleportiert"); } + [Command("changepw", "~m~Benutzung: ~s~/changepw [Aktuelles Passwort] [Neues Passwort]", SensitiveInfo = true)] + public void CmdUserChangePw(Player player, string oldPassword, string newPassword) + { + if (!player.IsLoggedIn()) return; + + if (oldPassword == newPassword) + { + ChatService.ErrorMessage(player, "Die eingegebenen Passwörter sind identisch"); + return; + } + + if (newPassword.Length < GlobalHelper.requiredPasswordLength) + { + ChatService.ErrorMessage(player, "Das neue Passwort muss aus mindestens sechs Zeichen bestehen"); + return; + } + + using (var dbContext = new DatabaseContext()) + { + User user = player.GetUser(dbContext); + + if (user.Password != NAPI.Util.GetHashSha256(oldPassword)) + { + ChatService.ErrorMessage(player, "Du hast dein aktuelles Passwort falsch eingegeben"); + return; + } + + user.Password = NAPI.Util.GetHashSha256(newPassword); + logger.LogInformation("Player {0} changed their password for {1}", player.Name, PASSWORD_CHANGE_FEE.ToMoneyString()); + + user.BankAccount.Balance -= PASSWORD_CHANGE_FEE; + dbContext.SaveChanges(); + } + + ChatService.SendMessage(player, $"~y~[ACCOUNT] ~s~Du hast dein Passwort erfolgreich geändert. Dir wurden ~y~{ PASSWORD_CHANGE_FEE.ToMoneyString() } ~s~in Rechnung gestellt."); + } + [Command("sms", "~m~Benutzung: ~s~/sms [Spieler] [Nachricht]", GreedyArg = true)] public void CmdUserSMS(Player player, string name, string msg) { diff --git a/ReallifeGamemode.Server/Events/Register.cs b/ReallifeGamemode.Server/Events/Register.cs index af1111e7..38cd9500 100644 --- a/ReallifeGamemode.Server/Events/Register.cs +++ b/ReallifeGamemode.Server/Events/Register.cs @@ -25,14 +25,15 @@ namespace ReallifeGamemode.Server.Events string username = player.Name; using (var dbContext = new DatabaseContext()) { - if (password.Length < 6) + if (password.Length < GlobalHelper.requiredPasswordLength) { - player.TriggerEvent("SERVER:Login_Error", "Das Passwort muss aus mindestens 6 Zeichen bestehen."); + player.TriggerEvent("SERVER:Login_Error", "Das Passwort muss aus mindestens sechs Zeichen bestehen."); return; } + if (dbContext.Users.Where(u => u.SocialClubName == player.SocialClubName).Count() != 0) { - player.TriggerEvent("SERVER:Login_Error", "Es ist schon ein Konto mit dieser Socialclub-ID registriert."); + player.TriggerEvent("SERVER:Login_Error", "Diese Social Club ID wird bereits verwendet."); return; } diff --git a/ReallifeGamemode.Server/Util/GlobalHelper.cs b/ReallifeGamemode.Server/Util/GlobalHelper.cs index 172757eb..71bbe8d9 100644 --- a/ReallifeGamemode.Server/Util/GlobalHelper.cs +++ b/ReallifeGamemode.Server/Util/GlobalHelper.cs @@ -21,6 +21,7 @@ namespace ReallifeGamemode.Server.Util { "Prelex", "zigaretten (Makkaroni)" } }; + public static int requiredPasswordLength = 6; public static int newbiePlayedMinutesThreshold = 30 * 60; public static DateTime CountdownUntil { get; internal set; }