From c5a283a02ed24826a3aec1977d84d2ff93d6dac3 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Sat, 8 Apr 2023 09:43:33 -0500 Subject: [PATCH] improve login plugin structure and fix load issue --- Plugins/Login/Commands/LoginCommand.cs | 15 ++++++++++-- Plugins/Login/LoginStates.cs | 9 ++++++++ Plugins/Login/Plugin.cs | 32 ++++++++++++-------------- 3 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 Plugins/Login/LoginStates.cs diff --git a/Plugins/Login/Commands/LoginCommand.cs b/Plugins/Login/Commands/LoginCommand.cs index 613920688..ff71b9f09 100644 --- a/Plugins/Login/Commands/LoginCommand.cs +++ b/Plugins/Login/Commands/LoginCommand.cs @@ -10,8 +10,13 @@ namespace IW4MAdmin.Plugins.Login.Commands { public class LoginCommand : Command { - public LoginCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup) + private readonly LoginConfiguration _loginConfig; + private readonly LoginStates _loginStates; + + public LoginCommand(CommandConfiguration config, ITranslationLookup translationLookup, LoginConfiguration loginConfig, LoginStates loginStates) : base(config, translationLookup) { + _loginConfig = loginConfig; + _loginStates = loginStates; Name = "login"; Description = _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DESC"]; Alias = "li"; @@ -29,6 +34,12 @@ namespace IW4MAdmin.Plugins.Login.Commands public override async Task ExecuteAsync(GameEvent gameEvent) { + if (!_loginConfig.RequirePrivilegedClientLogin) + { + gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DISABLED"]); + return; + } + var success = gameEvent.Owner.Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier { ClientId = gameEvent.Origin.ClientId, @@ -43,7 +54,7 @@ namespace IW4MAdmin.Plugins.Login.Commands if (success) { - Plugin.AuthorizedClients[gameEvent.Origin.ClientId] = true; + _loginStates.AuthorizedClients[gameEvent.Origin.ClientId] = true; } _ = success ? diff --git a/Plugins/Login/LoginStates.cs b/Plugins/Login/LoginStates.cs new file mode 100644 index 000000000..f38fb1d5e --- /dev/null +++ b/Plugins/Login/LoginStates.cs @@ -0,0 +1,9 @@ +using System.Collections.Concurrent; + +namespace IW4MAdmin.Plugins.Login; + +public class LoginStates +{ + public ConcurrentDictionary AuthorizedClients { get; } = new(); + public const string LoginKey = "IsLoggedIn"; +} diff --git a/Plugins/Login/Plugin.cs b/Plugins/Login/Plugin.cs index f5d52d968..8eee782f3 100644 --- a/Plugins/Login/Plugin.cs +++ b/Plugins/Login/Plugin.cs @@ -1,5 +1,4 @@ -using System.Collections.Concurrent; -using System.Threading; +using System.Threading; using System.Threading.Tasks; using IW4MAdmin.Plugins.Login.Commands; using Microsoft.Extensions.DependencyInjection; @@ -17,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 static ConcurrentDictionary AuthorizedClients { get; private set; } - private const string LoginKey = "IsLoggedIn"; - - public Plugin(LoginConfiguration configuration) + public Plugin(LoginConfiguration configuration, LoginStates loginStates) { - if (!configuration?.RequirePrivilegedClientLogin ?? false) + _loginStates = loginStates; + if (!(configuration?.RequirePrivilegedClientLogin ?? false)) { return; } @@ -32,28 +31,27 @@ public class Plugin : IPluginV2 IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized; IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, token) => { - AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _); + _loginStates.AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _); return Task.CompletedTask; }; } public static void RegisterDependencies(IServiceCollection serviceCollection) { - serviceCollection.AddConfiguration("LoginConfiguration"); + serviceCollection.AddConfiguration("LoginPluginSettings"); + serviceCollection.AddSingleton(new LoginStates()); } - private static Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token) + private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token) { - AuthorizedClients.TryAdd(clientEvent.Client.ClientId, false); - clientEvent.Client.SetAdditionalProperty(LoginKey, false); + _loginStates.AuthorizedClients.TryAdd(clientEvent.Client.ClientId, false); + clientEvent.Client.SetAdditionalProperty(LoginStates.LoginKey, false); return Task.CompletedTask; } - private static Task OnLoad(IManager manager, CancellationToken token) + private Task OnLoad(IManager manager, CancellationToken token) { - AuthorizedClients = new ConcurrentDictionary(); - manager.CommandInterceptors.Add(gameEvent => { if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null || gameEvent.IsRemote) @@ -82,12 +80,12 @@ public class Plugin : IPluginV2 return true; } - if (!AuthorizedClients[gameEvent.Origin.ClientId]) + if (!_loginStates.AuthorizedClients[gameEvent.Origin.ClientId]) { return false; } - gameEvent.Origin.SetAdditionalProperty(LoginKey, true); + gameEvent.Origin.SetAdditionalProperty(LoginStates.LoginKey, true); return true; });