update login plugin to IPluginV2

This commit is contained in:
RaidMax 2023-02-11 20:46:57 -06:00
parent ba9e393363
commit 83207b4b40
3 changed files with 76 additions and 75 deletions

View File

@ -19,7 +19,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.10.13.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2023.2.11.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -1,9 +1,8 @@
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Plugins.Login
{
class Configuration : IBaseConfiguration
public class LoginConfiguration : IBaseConfiguration
{
public bool RequirePrivilegedClientLogin { get; set; }

View File

@ -1,51 +1,56 @@
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using IW4MAdmin.Plugins.Login.Commands;
using Microsoft.Extensions.DependencyInjection;
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Exceptions;
using SharedLibraryCore.Events.Management;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Interfaces.Events;
using EFClient = Data.Models.Client.EFClient;
namespace IW4MAdmin.Plugins.Login
{
public class Plugin : IPlugin
namespace IW4MAdmin.Plugins.Login;
public class Plugin : IPluginV2
{
public string Name => "Login";
public float Version => Assembly.GetExecutingAssembly().GetName().Version.Major + Assembly.GetExecutingAssembly().GetName().Version.Minor / 10.0f;
public string Version => Utilities.GetVersionAsString();
public string Author => "RaidMax";
public static ConcurrentDictionary<int, bool> AuthorizedClients { get; private set; }
private readonly IConfigurationHandler<Configuration> _configHandler;
private const string LoginKey = "IsLoggedIn";
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
public Plugin(LoginConfiguration configuration)
{
_configHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("LoginPluginSettings");
if (!configuration?.RequirePrivilegedClientLogin ?? false)
{
return;
}
public Task OnEventAsync(GameEvent gameEvent, Server server)
IManagementEventSubscriptions.Load += OnLoad;
IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized;
IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, token) =>
{
if (gameEvent.IsRemote || _configHandler.Configuration().RequirePrivilegedClientLogin == false)
AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _);
return Task.CompletedTask;
if (gameEvent.Type == GameEvent.EventType.Connect)
{
AuthorizedClients.TryAdd(gameEvent.Origin.ClientId, false);
gameEvent.Origin.SetAdditionalProperty("IsLoggedIn", false);
};
}
if (gameEvent.Type == GameEvent.EventType.Disconnect)
public static void RegisterDependencies(IServiceCollection serviceCollection)
{
AuthorizedClients.TryRemove(gameEvent.Origin.ClientId, out _);
serviceCollection.AddConfiguration<LoginConfiguration>("LoginConfiguration");
}
private static Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
{
AuthorizedClients.TryAdd(clientEvent.Client.ClientId, false);
clientEvent.Client.SetAdditionalProperty(LoginKey, false);
return Task.CompletedTask;
}
public async Task OnLoadAsync(IManager manager)
private static Task OnLoad(IManager manager, CancellationToken token)
{
AuthorizedClients = new ConcurrentDictionary<int, bool>();
@ -56,39 +61,36 @@ namespace IW4MAdmin.Plugins.Login
return true;
}
if (gameEvent.Origin.Level < EFClient.Permission.Moderator ||
gameEvent.Origin.Level == EFClient.Permission.Console)
if (gameEvent.Origin.Level is < EFClient.Permission.Moderator or EFClient.Permission.Console)
{
return true;
}
if (gameEvent.Extra.GetType() == typeof(SetPasswordCommand) &&
gameEvent.Origin?.Password == null)
{
return true;
}
if (gameEvent.Extra.GetType() == typeof(LoginCommand))
{
return true;
}
if (gameEvent.Extra.GetType() == typeof(RequestTokenCommand))
{
return true;
}
if (!AuthorizedClients[gameEvent.Origin.ClientId])
{
return false;
}
gameEvent.Origin.SetAdditionalProperty("IsLoggedIn", true);
gameEvent.Origin.SetAdditionalProperty(LoginKey, true);
return true;
});
await _configHandler.BuildAsync();
if (_configHandler.Configuration() == null)
{
_configHandler.Set((Configuration)new Configuration().Generate());
await _configHandler.Save();
}
}
public Task OnTickAsync(Server S) => Task.CompletedTask;
public Task OnUnloadAsync() => Task.CompletedTask;
return Task.CompletedTask;
}
}