IW4M-Admin/Plugins/Login/Plugin.cs

95 lines
2.9 KiB
C#
Raw Permalink Normal View History

using System.Threading;
2018-04-14 00:51:38 -04:00
using System.Threading.Tasks;
using IW4MAdmin.Plugins.Login.Commands;
2023-02-11 21:46:57 -05:00
using Microsoft.Extensions.DependencyInjection;
2018-04-14 00:51:38 -04:00
using SharedLibraryCore;
using SharedLibraryCore.Commands;
2023-02-11 21:46:57 -05:00
using SharedLibraryCore.Events.Management;
2018-04-14 00:51:38 -04:00
using SharedLibraryCore.Interfaces;
2023-02-11 21:46:57 -05:00
using SharedLibraryCore.Interfaces.Events;
using EFClient = Data.Models.Client.EFClient;
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
namespace IW4MAdmin.Plugins.Login;
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
public class Plugin : IPluginV2
{
public string Name => "Login";
public string Version => Utilities.GetVersionAsString();
public string Author => "RaidMax";
private readonly LoginStates _loginStates;
2018-04-14 00:51:38 -04:00
public Plugin(LoginConfiguration configuration, LoginStates loginStates)
2023-02-11 21:46:57 -05:00
{
_loginStates = loginStates;
if (!(configuration?.RequirePrivilegedClientLogin ?? false))
{
2023-02-11 21:46:57 -05:00
return;
}
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
IManagementEventSubscriptions.Load += OnLoad;
IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized;
IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, token) =>
2018-04-14 00:51:38 -04:00
{
_loginStates.AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _);
2023-02-11 21:46:57 -05:00
return Task.CompletedTask;
};
}
public static void RegisterDependencies(IServiceCollection serviceCollection)
{
serviceCollection.AddConfiguration<LoginConfiguration>("LoginPluginSettings");
serviceCollection.AddSingleton(new LoginStates());
2023-02-11 21:46:57 -05:00
}
private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
2023-02-11 21:46:57 -05:00
{
_loginStates.AuthorizedClients.TryAdd(clientEvent.Client.ClientId, false);
clientEvent.Client.SetAdditionalProperty(LoginStates.LoginKey, false);
2023-02-11 21:46:57 -05:00
return Task.CompletedTask;
}
2018-04-14 00:51:38 -04:00
private Task OnLoad(IManager manager, CancellationToken token)
2023-02-11 21:46:57 -05:00
{
manager.CommandInterceptors.Add(gameEvent =>
{
if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null || gameEvent.IsRemote)
2018-04-14 00:51:38 -04:00
{
2023-02-11 21:46:57 -05:00
return true;
2018-04-14 00:51:38 -04:00
}
2023-02-11 21:46:57 -05:00
if (gameEvent.Origin.Level is < EFClient.Permission.Moderator or EFClient.Permission.Console)
2018-04-14 00:51:38 -04:00
{
2023-02-11 21:46:57 -05:00
return true;
2018-04-14 00:51:38 -04:00
}
2023-02-11 21:46:57 -05:00
if (gameEvent.Extra.GetType() == typeof(SetPasswordCommand) &&
gameEvent.Origin?.Password == null)
2018-04-14 00:51:38 -04:00
{
2023-02-11 21:46:57 -05:00
return true;
}
2023-02-11 21:46:57 -05:00
if (gameEvent.Extra.GetType() == typeof(LoginCommand))
{
return true;
}
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
if (gameEvent.Extra.GetType() == typeof(RequestTokenCommand))
{
return true;
2023-02-11 21:46:57 -05:00
}
2018-04-14 00:51:38 -04:00
if (!_loginStates.AuthorizedClients[gameEvent.Origin.ClientId])
2018-04-14 00:51:38 -04:00
{
2023-02-11 21:46:57 -05:00
return false;
2018-04-14 00:51:38 -04:00
}
gameEvent.Origin.SetAdditionalProperty(LoginStates.LoginKey, true);
2023-02-11 21:46:57 -05:00
return true;
});
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
return Task.CompletedTask;
2018-04-14 00:51:38 -04:00
}
}