1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-09 06:37:58 -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;
using SharedLibraryCore.Commands; using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration; using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Helpers; using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using EFClient = Data.Models.Client.EFClient;
namespace IW4MAdmin.Plugins.Login.Commands namespace IW4MAdmin.Plugins.Login.Commands
{ {
@ -13,7 +13,8 @@ namespace IW4MAdmin.Plugins.Login.Commands
private readonly LoginConfiguration _loginConfig; private readonly LoginConfiguration _loginConfig;
private readonly LoginStates _loginStates; 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; _loginConfig = loginConfig;
_loginStates = loginStates; _loginStates = loginStates;
@ -22,14 +23,14 @@ namespace IW4MAdmin.Plugins.Login.Commands
Alias = "li"; Alias = "li";
Permission = EFClient.Permission.Trusted; Permission = EFClient.Permission.Trusted;
RequiresTarget = false; RequiresTarget = false;
Arguments = new CommandArgument[] Arguments =
{ [
new() new CommandArgument
{ {
Name = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_ARGS_PASSWORD"], Name = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_ARGS_PASSWORD"],
Required = true Required = true
} }
}; ];
} }
public override async Task ExecuteAsync(GameEvent gameEvent) public override async Task ExecuteAsync(GameEvent gameEvent)
@ -57,9 +58,9 @@ namespace IW4MAdmin.Plugins.Login.Commands
_loginStates.AuthorizedClients[gameEvent.Origin.ClientId] = true; _loginStates.AuthorizedClients[gameEvent.Origin.ClientId] = true;
} }
_ = success ? gameEvent.Origin.Tell(success
gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_SUCCESS"]) : ? _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_SUCCESS"]
gameEvent.Origin.Tell(_translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_FAIL"]); : _translationLookup["PLUGINS_LOGIN_COMMANDS_LOGIN_FAIL"]);
} }
} }
} }

View File

@ -13,6 +13,7 @@
<Configurations>Debug;Release;Prerelease</Configurations> <Configurations>Debug;Release;Prerelease</Configurations>
<LangVersion>Latest</LangVersion> <LangVersion>Latest</LangVersion>
<RootNamespace>IW4MAdmin.Plugins.Login</RootNamespace> <RootNamespace>IW4MAdmin.Plugins.Login</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <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 bool RequirePrivilegedClientLogin { get; set; }
public IBaseConfiguration Generate()
{
return this;
}
public string Name() => "LoginConfiguration";
}
} }

View File

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

View File

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