improve login plugin structure and fix load issue

This commit is contained in:
RaidMax 2023-04-08 09:43:33 -05:00
parent d0911b7b8a
commit c5a283a02e
3 changed files with 37 additions and 19 deletions

View File

@ -10,8 +10,13 @@ namespace IW4MAdmin.Plugins.Login.Commands
{ {
public class LoginCommand : Command 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"; Name = "login";
Description = _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DESC"]; Description = _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DESC"];
Alias = "li"; Alias = "li";
@ -29,6 +34,12 @@ namespace IW4MAdmin.Plugins.Login.Commands
public override async Task ExecuteAsync(GameEvent gameEvent) 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 var success = gameEvent.Owner.Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier
{ {
ClientId = gameEvent.Origin.ClientId, ClientId = gameEvent.Origin.ClientId,
@ -43,7 +54,7 @@ namespace IW4MAdmin.Plugins.Login.Commands
if (success) if (success)
{ {
Plugin.AuthorizedClients[gameEvent.Origin.ClientId] = true; _loginStates.AuthorizedClients[gameEvent.Origin.ClientId] = true;
} }
_ = success ? _ = success ?

View File

@ -0,0 +1,9 @@
using System.Collections.Concurrent;
namespace IW4MAdmin.Plugins.Login;
public class LoginStates
{
public ConcurrentDictionary<int, bool> AuthorizedClients { get; } = new();
public const string LoginKey = "IsLoggedIn";
}

View File

@ -1,5 +1,4 @@
using System.Collections.Concurrent; using System.Threading;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using IW4MAdmin.Plugins.Login.Commands; using IW4MAdmin.Plugins.Login.Commands;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -17,13 +16,13 @@ public class Plugin : IPluginV2
public string Name => "Login"; public string Name => "Login";
public string Version => Utilities.GetVersionAsString(); public string Version => Utilities.GetVersionAsString();
public string Author => "RaidMax"; public string Author => "RaidMax";
private readonly LoginStates _loginStates;
public static ConcurrentDictionary<int, bool> AuthorizedClients { get; private set; } public Plugin(LoginConfiguration configuration, LoginStates loginStates)
private const string LoginKey = "IsLoggedIn";
public Plugin(LoginConfiguration configuration)
{ {
if (!configuration?.RequirePrivilegedClientLogin ?? false) _loginStates = loginStates;
if (!(configuration?.RequirePrivilegedClientLogin ?? false))
{ {
return; return;
} }
@ -32,28 +31,27 @@ public class Plugin : IPluginV2
IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized; IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized;
IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, token) => IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, token) =>
{ {
AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _); _loginStates.AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _);
return Task.CompletedTask; return Task.CompletedTask;
}; };
} }
public static void RegisterDependencies(IServiceCollection serviceCollection) public static void RegisterDependencies(IServiceCollection serviceCollection)
{ {
serviceCollection.AddConfiguration<LoginConfiguration>("LoginConfiguration"); serviceCollection.AddConfiguration<LoginConfiguration>("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); _loginStates.AuthorizedClients.TryAdd(clientEvent.Client.ClientId, false);
clientEvent.Client.SetAdditionalProperty(LoginKey, false); clientEvent.Client.SetAdditionalProperty(LoginStates.LoginKey, false);
return Task.CompletedTask; return Task.CompletedTask;
} }
private static Task OnLoad(IManager manager, CancellationToken token) private Task OnLoad(IManager manager, CancellationToken token)
{ {
AuthorizedClients = new ConcurrentDictionary<int, bool>();
manager.CommandInterceptors.Add(gameEvent => manager.CommandInterceptors.Add(gameEvent =>
{ {
if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null || gameEvent.IsRemote) if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null || gameEvent.IsRemote)
@ -82,12 +80,12 @@ public class Plugin : IPluginV2
return true; return true;
} }
if (!AuthorizedClients[gameEvent.Origin.ClientId]) if (!_loginStates.AuthorizedClients[gameEvent.Origin.ClientId])
{ {
return false; return false;
} }
gameEvent.Origin.SetAdditionalProperty(LoginKey, true); gameEvent.Origin.SetAdditionalProperty(LoginStates.LoginKey, true);
return true; return true;
}); });