IW4M-Admin/Plugins/Login/Plugin.cs

92 lines
3.1 KiB
C#
Raw Permalink Normal View History

2018-04-14 00:51:38 -04:00
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading.Tasks;
using IW4MAdmin.Plugins.Login.Commands;
2018-04-14 00:51:38 -04:00
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Database.Models;
2018-04-14 00:51:38 -04:00
using SharedLibraryCore.Exceptions;
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Plugins.Login
{
public class Plugin : IPlugin
{
public string Name => "Login";
public float Version => Assembly.GetExecutingAssembly().GetName().Version.Major + Assembly.GetExecutingAssembly().GetName().Version.Minor / 10.0f;
public string Author => "RaidMax";
public static ConcurrentDictionary<int, bool> AuthorizedClients { get; private set; }
private readonly IConfigurationHandler<Configuration> _configHandler;
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
{
_configHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("LoginPluginSettings");
}
2018-04-14 00:51:38 -04:00
public Task OnEventAsync(GameEvent E, Server S)
{
if (E.IsRemote || _configHandler.Configuration().RequirePrivilegedClientLogin == false)
2018-04-14 00:51:38 -04:00
return Task.CompletedTask;
if (E.Type == GameEvent.EventType.Connect)
{
AuthorizedClients.TryAdd(E.Origin.ClientId, false);
E.Origin.SetAdditionalProperty("IsLoggedIn", false);
2018-04-14 00:51:38 -04:00
}
if (E.Type == GameEvent.EventType.Disconnect)
{
AuthorizedClients.TryRemove(E.Origin.ClientId, out bool value);
}
if (E.Type == GameEvent.EventType.Command)
{
if (E.Origin.Level < EFClient.Permission.Moderator ||
E.Origin.Level == EFClient.Permission.Console)
return Task.CompletedTask;
if (E.Extra.GetType() == typeof(SetPasswordCommand) &&
E.Origin?.Password == null)
2018-04-14 00:51:38 -04:00
return Task.CompletedTask;
if (E.Extra.GetType() == typeof(LoginCommand))
2018-04-14 00:51:38 -04:00
return Task.CompletedTask;
if (E.Extra.GetType() == typeof(RequestTokenCommand))
return Task.CompletedTask;
2018-04-14 00:51:38 -04:00
if (!AuthorizedClients[E.Origin.ClientId])
{
throw new AuthorizationException(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_LOGIN_AUTH"]);
}
else
{
E.Origin.SetAdditionalProperty("IsLoggedIn", true);
}
2018-04-14 00:51:38 -04:00
}
return Task.CompletedTask;
}
public async Task OnLoadAsync(IManager manager)
{
AuthorizedClients = new ConcurrentDictionary<int, bool>();
2022-01-28 10:35:01 -05:00
await _configHandler.BuildAsync();
if (_configHandler.Configuration() == null)
2018-04-14 00:51:38 -04:00
{
_configHandler.Set((Configuration)new Configuration().Generate());
await _configHandler.Save();
2018-04-14 00:51:38 -04:00
}
}
public Task OnTickAsync(Server S) => Task.CompletedTask;
2018-04-14 00:51:38 -04:00
public Task OnUnloadAsync() => Task.CompletedTask;
2018-04-14 00:51:38 -04:00
}
}