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 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 ?

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 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<int, bool> 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>("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);
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<int, bool>();
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;
});