Skip to content

Commit a0c2e41

Browse files
authored
Add function setAccountSerial (PR #3587)
1 parent 76f7cc2 commit a0c2e41

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

Server/mods/deathmatch/acl.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<right name="function.removeAccount" access="false" />
104104
<right name="function.setAccountName" access="false" />
105105
<right name="function.setAccountPassword" access="false" />
106+
<right name="function.setAccountSerial" access="false" />
106107
<right name="function.kickPlayer" access="false" />
107108
<right name="function.banPlayer" access="false" />
108109
<right name="function.getBans" access="false" />
@@ -188,6 +189,7 @@
188189
<right name="function.addAccount" access="true" />
189190
<right name="function.setAccountName" access="true" />
190191
<right name="function.setAccountPassword" access="true" />
192+
<right name="function.setAccountSerial" access="true" />
191193
<right name="function.renameResource" access="true" />
192194
<right name="function.deleteResource" access="true" />
193195
</acl>

Server/mods/deathmatch/logic/CAccount.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CAccountManager.h"
1515
#include "CIdArray.h"
1616
#include "CClient.h"
17+
#include <regex>
1718

1819
CAccount::CAccount(CAccountManager* pManager, EAccountType accountType, const std::string& strName, const std::string& strPassword, int iUserID,
1920
const std::string& strIP, const std::string& strSerial, const SString& strHttpPassAppend)
@@ -255,6 +256,20 @@ bool CAccount::IsIpAuthorized(const SString& strIp)
255256
return false;
256257
}
257258

259+
//
260+
// Check if the serial has 32 hexadecimal characters
261+
//
262+
bool CAccount::IsValidSerial(const std::string& serial) const noexcept
263+
{
264+
const std::regex serialPattern("^[A-Fa-f0-9]{32}$");
265+
266+
try{
267+
return std::regex_match(serial, serialPattern);
268+
} catch (...) {
269+
return false;
270+
}
271+
}
272+
258273
//
259274
// Mark pending serial as authorized for this account
260275
//
@@ -289,6 +304,19 @@ bool CAccount::RemoveSerial(const SString& strSerial)
289304
return false;
290305
}
291306

307+
//
308+
// Replace the serial number for a specific account
309+
//
310+
bool CAccount::SetAccountSerial(const std::string& serial) noexcept
311+
{
312+
if (!IsValidSerial(serial))
313+
return false;
314+
315+
m_strSerial = serial;
316+
m_pManager->MarkAsChanged(this);
317+
return true;
318+
}
319+
292320
//
293321
// Cleanup unauthorized serials
294322
//

Server/mods/deathmatch/logic/CAccount.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ class CAccount
9797
SSerialUsage* GetSerialUsage(const SString& strSerial);
9898
bool IsIpAuthorized(const SString& strIp);
9999
bool IsSerialAuthorized(const SString& strSerial);
100+
bool IsValidSerial(const std::string& serial) const noexcept;
100101
bool AddSerialForAuthorization(const SString& strSerial, const SString& strIp);
101102
bool AuthorizeSerial(const SString& strSerial, const SString& strWho);
102103
bool RemoveSerial(const SString& strSerial);
103104
void RemoveUnauthorizedSerials();
105+
bool SetAccountSerial(const std::string& serial) noexcept;
104106

105107
CClient* GetClient() const { return m_pClient; }
106108
void SetClient(CClient* pClient);

Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11492,6 +11492,15 @@ bool CStaticFunctionDefinitions::GetAccountSerial(CAccount* pAccount, SString& s
1149211492
return bRegistered;
1149311493
}
1149411494

11495+
11496+
bool CStaticFunctionDefinitions::SetAccountSerial(CAccount* account, const std::string& serial) noexcept
11497+
{
11498+
if (account && account->IsRegistered())
11499+
return account->SetAccountSerial(serial);
11500+
11501+
return false;
11502+
}
11503+
1149511504
bool CStaticFunctionDefinitions::GetAccountsBySerial(const SString& strSerial, std::vector<CAccount*>& outAccounts)
1149611505
{
1149711506
m_pAccountManager->GetAccountsBySerial(strSerial, outAccounts);

Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ class CStaticFunctionDefinitions
705705
// Account set funcs
706706
static CAccount* AddAccount(const SString& strName, const SString& strPassword, bool bAllowCaseVariations, SString& strOutError);
707707
static bool RemoveAccount(CAccount* pAccount);
708+
static bool SetAccountSerial(CAccount* account, const std::string& serial) noexcept;
708709
static bool SetAccountName(CAccount* pAccount, SString strNewName, bool bAllowCaseVariations, SString& strOutError);
709710
static bool SetAccountPassword(CAccount* pAccount, SString szPassword, CAccountPassword::EAccountPasswordType ePasswordType);
710711
static bool SetAccountData(CAccount* pAccount, const char* szKey, CLuaArgument* pArgument);

Server/mods/deathmatch/logic/luadefs/CLuaAccountDefs.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void CLuaAccountDefs::LoadFunctions()
4242
{"addAccount", AddAccount},
4343
{"removeAccount", RemoveAccount},
4444
{"setAccountPassword", SetAccountPassword},
45+
{"setAccountSerial", ArgumentParser<SetAccountSerial>},
4546
{"setAccountData", SetAccountData},
4647
{"setAccountName", SetAccountName},
4748
{"copyAccountData", CopyAccountData},
@@ -71,6 +72,7 @@ void CLuaAccountDefs::AddClass(lua_State* luaVM)
7172
lua_classfunction(luaVM, "setData", "setAccountData");
7273
lua_classfunction(luaVM, "setPassword", "setAccountPassword");
7374
lua_classfunction(luaVM, "setName", "setAccountName");
75+
lua_classfunction(luaVM, "setSerial", "setAccountSerial");
7476

7577
lua_classfunction(luaVM, "getSerial", "getAccountSerial");
7678
lua_classfunction(luaVM, "getIP", "getAccountIP");
@@ -82,7 +84,7 @@ void CLuaAccountDefs::AddClass(lua_State* luaVM)
8284
lua_classfunction(luaVM, "getPlayer", "getAccountPlayer");
8385
lua_classfunction(luaVM, "isGuest", "isGuestAccount");
8486

85-
lua_classvariable(luaVM, "serial", NULL, "getAccountSerial");
87+
lua_classvariable(luaVM, "serial", "setAccountSerial", "getAccountSerial");
8688
lua_classvariable(luaVM, "name", "setAccountName", "getAccountName");
8789
lua_classvariable(luaVM, "id", NULL, "getAccountID");
8890
lua_classvariable(luaVM, "ip", NULL, "getAccountIP");
@@ -513,6 +515,12 @@ int CLuaAccountDefs::RemoveAccount(lua_State* luaVM)
513515
return 1;
514516
}
515517

518+
bool CLuaAccountDefs::SetAccountSerial(CAccount* account, std::string serial) noexcept
519+
{
520+
return CStaticFunctionDefinitions::SetAccountSerial(account, serial);
521+
}
522+
523+
516524
int CLuaAccountDefs::SetAccountName(lua_State* luaVM)
517525
{
518526
// bool setAccountPassword ( account theAccount, string name[, bool allowCaseVariations = false ] )

Server/mods/deathmatch/logic/luadefs/CLuaAccountDefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ class CLuaAccountDefs : public CLuaDefs
4848
LUA_DECLARE(SetAccountPassword);
4949
LUA_DECLARE(SetAccountData);
5050
LUA_DECLARE(CopyAccountData);
51+
static bool SetAccountSerial(CAccount* account, std::string serial) noexcept;
5152
};

0 commit comments

Comments
 (0)