Codechange: create helper class for useful NetworkAuthorizedKeys functions

This commit is contained in:
Rubidium
2024-03-18 18:56:46 +01:00
committed by rubidium42
parent 2485de9462
commit 7580eac2d5
7 changed files with 81 additions and 38 deletions

View File

@@ -1957,22 +1957,11 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
}
/** All the known authorized keys with their name. */
static std::vector<std::pair<std::string_view, std::vector<std::string> *>> _console_cmd_authorized_keys{
static std::vector<std::pair<std::string_view, NetworkAuthorizedKeys *>> _console_cmd_authorized_keys{
{ "rcon", &_settings_client.network.rcon_authorized_keys },
{ "server", &_settings_client.network.server_authorized_keys },
};
/**
* Simple helper to find the location of the given authorized key in the authorized keys.
* @param authorized_keys The keys to look through.
* @param authorized_key The key to look for.
* @return The iterator to the location of the authorized key, or \c authorized_keys.end().
*/
static auto FindKey(std::vector<std::string> *authorized_keys, std::string_view authorized_key)
{
return std::find_if(authorized_keys->begin(), authorized_keys->end(), [authorized_key](auto &value) { return StrEqualsIgnoreCase(value, authorized_key); });
}
DEF_CONSOLE_CMD(ConNetworkAuthorizedKey)
{
if (argc <= 2) {
@@ -2016,11 +2005,8 @@ DEF_CONSOLE_CMD(ConNetworkAuthorizedKey)
}
}
auto iter = FindKey(authorized_keys, authorized_key);
if (StrEqualsIgnoreCase(argv[1], "add")) {
if (iter == authorized_keys->end()) {
authorized_keys->push_back(authorized_key);
if (authorized_keys->Add(authorized_key)) {
IConsolePrint(CC_INFO, "Added {} to {}.", authorized_key, name);
} else {
IConsolePrint(CC_WARNING, "Not added {} to {} as it already exists.", authorized_key, name);
@@ -2029,8 +2015,7 @@ DEF_CONSOLE_CMD(ConNetworkAuthorizedKey)
}
if (StrEqualsIgnoreCase(argv[1], "remove")) {
if (iter != authorized_keys->end()) {
authorized_keys->erase(iter);
if (authorized_keys->Remove(authorized_key)) {
IConsolePrint(CC_INFO, "Removed {} from {}.", authorized_key, name);
} else {
IConsolePrint(CC_WARNING, "Not removed {} from {} as it does not exist.", authorized_key, name);