1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-07 21:58:06 -05:00

Refactor Login plugin and clean up code

The Login plugin code has been optimized and cleaned up. This refactoring includes removing unnecessary conditions, switching to a more readable syntax, and deleting deprecated methods or properties. The possibility of a null configuration is also eliminated to improve code reliability.
This commit is contained in:
Ayymoss 2024-07-16 23:58:18 +01:00
parent c4defacabc
commit 279016c405
No known key found for this signature in database
GPG Key ID: 6F64388D52A78E9E
5 changed files with 24 additions and 40 deletions

View File

@ -2,9 +2,9 @@
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces;
using EFClient = Data.Models.Client.EFClient;
namespace IW4MAdmin.Plugins.Login.Commands
{
@ -13,7 +13,8 @@ namespace IW4MAdmin.Plugins.Login.Commands
private readonly LoginConfiguration _loginConfig;
private readonly LoginStates _loginStates;
public LoginCommand(CommandConfiguration config, ITranslationLookup translationLookup, LoginConfiguration loginConfig, LoginStates loginStates) : base(config, translationLookup)
public LoginCommand(CommandConfiguration config, ITranslationLookup translationLookup, LoginConfiguration loginConfig,
LoginStates loginStates) : base(config, translationLookup)
{
_loginConfig = loginConfig;
_loginStates = loginStates;
@ -22,14 +23,14 @@ namespace IW4MAdmin.Plugins.Login.Commands
Alias = "li";
Permission = EFClient.Permission.Trusted;
RequiresTarget = false;
Arguments = new CommandArgument[]
{
new()
Arguments =
[
new CommandArgument
{
Name = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_ARGS_PASSWORD"],
Required = true
}
};
];
}
public override async Task ExecuteAsync(GameEvent gameEvent)
@ -39,7 +40,7 @@ namespace IW4MAdmin.Plugins.Login.Commands
gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_DISABLED"]);
return;
}
var success = gameEvent.Owner.Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier
{
ClientId = gameEvent.Origin.ClientId,
@ -57,9 +58,9 @@ namespace IW4MAdmin.Plugins.Login.Commands
_loginStates.AuthorizedClients[gameEvent.Origin.ClientId] = true;
}
_ = success ?
gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_SUCCESS"]) :
gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_FAIL"]);
gameEvent.Origin.Tell(success
? _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_SUCCESS"]
: _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_FAIL"]);
}
}
}

View File

@ -13,6 +13,7 @@
<Configurations>Debug;Release;Prerelease</Configurations>
<LangVersion>Latest</LangVersion>
<RootNamespace>IW4MAdmin.Plugins.Login</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -1,16 +1,6 @@
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Plugins.Login;
namespace IW4MAdmin.Plugins.Login
public class LoginConfiguration
{
public class LoginConfiguration : IBaseConfiguration
{
public bool RequirePrivilegedClientLogin { get; set; }
public IBaseConfiguration Generate()
{
return this;
}
public string Name() => "LoginConfiguration";
}
public bool RequirePrivilegedClientLogin { get; set; }
}

View File

@ -4,6 +4,6 @@ namespace IW4MAdmin.Plugins.Login;
public class LoginStates
{
public ConcurrentDictionary<int, bool> AuthorizedClients { get; } = new();
public ConcurrentDictionary<int, bool> AuthorizedClients { get; } = [];
public const string LoginKey = "IsLoggedIn";
}

View File

@ -16,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 Plugin(LoginConfiguration configuration, LoginStates loginStates)
{
_loginStates = loginStates;
if (!(configuration?.RequirePrivilegedClientLogin ?? false))
if (!configuration.RequirePrivilegedClientLogin)
{
return;
}
@ -38,7 +38,7 @@ public class Plugin : IPluginV2
public static void RegisterDependencies(IServiceCollection serviceCollection)
{
serviceCollection.AddConfiguration<LoginConfiguration>("LoginPluginSettings");
serviceCollection.AddConfiguration("LoginPluginSettings", new LoginConfiguration());
serviceCollection.AddSingleton(new LoginStates());
}
@ -64,20 +64,12 @@ public class Plugin : IPluginV2
return true;
}
if (gameEvent.Extra.GetType() == typeof(SetPasswordCommand) &&
gameEvent.Origin?.Password == null)
switch (gameEvent.Extra)
{
return true;
}
if (gameEvent.Extra.GetType() == typeof(LoginCommand))
{
return true;
}
if (gameEvent.Extra.GetType() == typeof(RequestTokenCommand))
{
return true;
case SetPasswordCommand when gameEvent.Origin?.Password is null:
case LoginCommand:
case RequestTokenCommand:
return true;
}
if (!_loginStates.AuthorizedClients[gameEvent.Origin.ClientId])