From 279016c4056ec59bfe3b894cb583532f54cce29d Mon Sep 17 00:00:00 2001 From: Ayymoss Date: Tue, 16 Jul 2024 23:58:18 +0100 Subject: [PATCH] Refactor Login plugin and clean up code The Login plugin code has been optimized and cleaned up. This refactoring includes removing unnecessary conditions, switching to a more readable syntax, and deleting deprecated methods or properties. The possibility of a null configuration is also eliminated to improve code reliability. --- Plugins/Login/Commands/LoginCommand.cs | 21 +++++++++++---------- Plugins/Login/Login.csproj | 1 + Plugins/Login/LoginConfiguration.cs | 16 +++------------- Plugins/Login/LoginStates.cs | 2 +- Plugins/Login/Plugin.cs | 24 ++++++++---------------- 5 files changed, 24 insertions(+), 40 deletions(-) diff --git a/Plugins/Login/Commands/LoginCommand.cs b/Plugins/Login/Commands/LoginCommand.cs index cbdbcae4..750f8eb6 100644 --- a/Plugins/Login/Commands/LoginCommand.cs +++ b/Plugins/Login/Commands/LoginCommand.cs @@ -2,9 +2,9 @@ using SharedLibraryCore; using SharedLibraryCore.Commands; using SharedLibraryCore.Configuration; -using SharedLibraryCore.Database.Models; using SharedLibraryCore.Helpers; using SharedLibraryCore.Interfaces; +using EFClient = Data.Models.Client.EFClient; namespace IW4MAdmin.Plugins.Login.Commands { @@ -13,7 +13,8 @@ namespace IW4MAdmin.Plugins.Login.Commands private readonly LoginConfiguration _loginConfig; private readonly LoginStates _loginStates; - public LoginCommand(CommandConfiguration config, ITranslationLookup translationLookup, LoginConfiguration loginConfig, LoginStates loginStates) : base(config, translationLookup) + public LoginCommand(CommandConfiguration config, ITranslationLookup translationLookup, LoginConfiguration loginConfig, + LoginStates loginStates) : base(config, translationLookup) { _loginConfig = loginConfig; _loginStates = loginStates; @@ -22,14 +23,14 @@ namespace IW4MAdmin.Plugins.Login.Commands Alias = "li"; Permission = EFClient.Permission.Trusted; RequiresTarget = false; - Arguments = new CommandArgument[] - { - new() + Arguments = + [ + new CommandArgument { Name = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_ARGS_PASSWORD"], Required = true } - }; + ]; } public override async Task ExecuteAsync(GameEvent gameEvent) @@ -39,7 +40,7 @@ namespace IW4MAdmin.Plugins.Login.Commands gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DISABLED"]); return; } - + var success = gameEvent.Owner.Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier { ClientId = gameEvent.Origin.ClientId, @@ -57,9 +58,9 @@ namespace IW4MAdmin.Plugins.Login.Commands _loginStates.AuthorizedClients[gameEvent.Origin.ClientId] = true; } - _ = success ? - gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_SUCCESS"]) : - gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_FAIL"]); + gameEvent.Origin.Tell(success + ? _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_SUCCESS"] + : _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_FAIL"]); } } } diff --git a/Plugins/Login/Login.csproj b/Plugins/Login/Login.csproj index 80dea50e..910ff4e0 100644 --- a/Plugins/Login/Login.csproj +++ b/Plugins/Login/Login.csproj @@ -13,6 +13,7 @@ Debug;Release;Prerelease Latest IW4MAdmin.Plugins.Login + enable diff --git a/Plugins/Login/LoginConfiguration.cs b/Plugins/Login/LoginConfiguration.cs index a4d3594c..43b2e453 100644 --- a/Plugins/Login/LoginConfiguration.cs +++ b/Plugins/Login/LoginConfiguration.cs @@ -1,16 +1,6 @@ -using SharedLibraryCore.Interfaces; +namespace IW4MAdmin.Plugins.Login; -namespace IW4MAdmin.Plugins.Login +public class LoginConfiguration { - public class LoginConfiguration : IBaseConfiguration - { - public bool RequirePrivilegedClientLogin { get; set; } - - public IBaseConfiguration Generate() - { - return this; - } - - public string Name() => "LoginConfiguration"; - } + public bool RequirePrivilegedClientLogin { get; set; } } diff --git a/Plugins/Login/LoginStates.cs b/Plugins/Login/LoginStates.cs index f38fb1d5..375e6065 100644 --- a/Plugins/Login/LoginStates.cs +++ b/Plugins/Login/LoginStates.cs @@ -4,6 +4,6 @@ namespace IW4MAdmin.Plugins.Login; public class LoginStates { - public ConcurrentDictionary AuthorizedClients { get; } = new(); + public ConcurrentDictionary AuthorizedClients { get; } = []; public const string LoginKey = "IsLoggedIn"; } diff --git a/Plugins/Login/Plugin.cs b/Plugins/Login/Plugin.cs index 8eee782f..2e11844b 100644 --- a/Plugins/Login/Plugin.cs +++ b/Plugins/Login/Plugin.cs @@ -16,13 +16,13 @@ public class Plugin : IPluginV2 public string Name => "Login"; public string Version => Utilities.GetVersionAsString(); public string Author => "RaidMax"; - + private readonly LoginStates _loginStates; public Plugin(LoginConfiguration configuration, LoginStates loginStates) { _loginStates = loginStates; - if (!(configuration?.RequirePrivilegedClientLogin ?? false)) + if (!configuration.RequirePrivilegedClientLogin) { return; } @@ -38,7 +38,7 @@ public class Plugin : IPluginV2 public static void RegisterDependencies(IServiceCollection serviceCollection) { - serviceCollection.AddConfiguration("LoginPluginSettings"); + serviceCollection.AddConfiguration("LoginPluginSettings", new LoginConfiguration()); serviceCollection.AddSingleton(new LoginStates()); } @@ -64,20 +64,12 @@ public class Plugin : IPluginV2 return true; } - if (gameEvent.Extra.GetType() == typeof(SetPasswordCommand) && - gameEvent.Origin?.Password == null) + switch (gameEvent.Extra) { - return true; - } - - if (gameEvent.Extra.GetType() == typeof(LoginCommand)) - { - return true; - } - - if (gameEvent.Extra.GetType() == typeof(RequestTokenCommand)) - { - return true; + case SetPasswordCommand when gameEvent.Origin?.Password is null: + case LoginCommand: + case RequestTokenCommand: + return true; } if (!_loginStates.AuthorizedClients[gameEvent.Origin.ClientId])