mirror of
https://github.com/EQEmu/Server.git
synced 2026-06-02 15:20:26 +00:00
[Loginserver] Modernize codebase (#4647)
* Beginning of cleanup * More cleanup * More cleanup * Enc cleanup * client manager cleanup * client cleanup * More cleanup * More cleanup * Cleanup * More cleanup, account context, account management * Remove positional fmt bindings * Use LoginAccountContext * Update loginserver_webserver.cpp * Remove comments * Port CreateLoginServerAccount to repositories * More cleanup * More cleanup * More cleanup * More cleanup * Remove a ton of functions * More cleanup * More cleanup * More cleanup * Cleanup SendClientAuthToWorld * Consolidate world server logic * Update login_accounts_repository.h * Update login_accounts_repository.h * Move api tokens to repositories * Cleanup options * Move everything else to repositories * Update account_management.cpp * uint64 account * Update login_schema.sql * Fix * Update world_server.cpp * auto
This commit is contained in:
@@ -4,47 +4,94 @@
|
||||
#include "../database.h"
|
||||
#include "../strings.h"
|
||||
#include "base/base_login_accounts_repository.h"
|
||||
#include "../../loginserver/encryption.h"
|
||||
#include "../../loginserver/login_types.h"
|
||||
|
||||
class LoginAccountsRepository: public BaseLoginAccountsRepository {
|
||||
class LoginAccountsRepository : public BaseLoginAccountsRepository {
|
||||
public:
|
||||
static int64 GetFreeID(Database &db, const std::string &loginserver)
|
||||
{
|
||||
auto query = fmt::format(
|
||||
"SELECT IFNULL(MAX(id), 0) + 1 FROM login_accounts WHERE source_loginserver = '{}'",
|
||||
Strings::Escape(loginserver)
|
||||
);
|
||||
|
||||
/**
|
||||
* This file was auto generated and can be modified and extended upon
|
||||
*
|
||||
* Base repository methods are automatically
|
||||
* generated in the "base" version of this repository. The base repository
|
||||
* is immutable and to be left untouched, while methods in this class
|
||||
* are used as extension methods for more specific persistence-layer
|
||||
* accessors or mutators.
|
||||
*
|
||||
* Base Methods (Subject to be expanded upon in time)
|
||||
*
|
||||
* Note: Not all tables are designed appropriately to fit functionality with all base methods
|
||||
*
|
||||
* InsertOne
|
||||
* UpdateOne
|
||||
* DeleteOne
|
||||
* FindOne
|
||||
* GetWhere(std::string where_filter)
|
||||
* DeleteWhere(std::string where_filter)
|
||||
* InsertMany
|
||||
* All
|
||||
*
|
||||
* Example custom methods in a repository
|
||||
*
|
||||
* LoginAccountsRepository::GetByZoneAndVersion(int zone_id, int zone_version)
|
||||
* LoginAccountsRepository::GetWhereNeverExpires()
|
||||
* LoginAccountsRepository::GetWhereXAndY()
|
||||
* LoginAccountsRepository::DeleteWhereXAndY()
|
||||
*
|
||||
* Most of the above could be covered by base methods, but if you as a developer
|
||||
* find yourself re-using logic for other parts of the code, its best to just make a
|
||||
* method that can be re-used easily elsewhere especially if it can use a base repository
|
||||
* method and encapsulate filters there
|
||||
*/
|
||||
auto results = db.QueryDatabase(query);
|
||||
if (!results.Success() || results.RowCount() != 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Custom extended repository methods here
|
||||
auto row = results.begin();
|
||||
|
||||
return Strings::ToUnsignedInt(row[0]);
|
||||
}
|
||||
|
||||
static LoginAccountsRepository::LoginAccounts CreateAccountFromContext(
|
||||
Database &db,
|
||||
LoginAccountContext c
|
||||
)
|
||||
{
|
||||
auto a = LoginAccountsRepository::NewEntity();
|
||||
|
||||
if (!c.password_is_encrypted) {
|
||||
auto e = EncryptPasswordFromContext(c);
|
||||
a.account_password = e.password;
|
||||
}
|
||||
|
||||
a.id = c.login_account_id > 0 ? c.login_account_id : GetFreeID(db, c.source_loginserver);
|
||||
a.account_name = c.username;
|
||||
a.account_email = !c.email.empty() ? c.email : "local_creation";
|
||||
a.source_loginserver = c.source_loginserver;
|
||||
a.last_ip_address = "127.0.0.1";
|
||||
a.last_login_date = std::time(nullptr);
|
||||
a.created_at = std::time(nullptr);
|
||||
LoginAccountsRepository::InsertOne(db, a);
|
||||
|
||||
return GetAccountFromContext(db, c).id > 0 ? a : NewEntity();
|
||||
}
|
||||
|
||||
static LoginAccountsRepository::LoginAccounts GetAccountFromContext(
|
||||
Database &db,
|
||||
LoginAccountContext c
|
||||
)
|
||||
{
|
||||
std::string where = fmt::format(
|
||||
"account_name = '{}' AND source_loginserver = '{}'",
|
||||
Strings::Escape(c.username),
|
||||
Strings::Escape(c.source_loginserver)
|
||||
);
|
||||
|
||||
if (!c.email.empty()) {
|
||||
where += fmt::format(" AND account_email = '{}'", Strings::Escape(c.email));
|
||||
}
|
||||
if (c.login_account_id > 0) {
|
||||
where += fmt::format(" AND id = {}", c.login_account_id);
|
||||
}
|
||||
|
||||
where += " LIMIT 1";
|
||||
|
||||
auto results = LoginAccountsRepository::GetWhere(db, where);
|
||||
|
||||
auto e = LoginAccountsRepository::NewEntity();
|
||||
if (results.size() == 1) {
|
||||
e = results.front();
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
static LoginAccounts UpdateAccountPassword(Database &db, LoginAccounts a, std::string password)
|
||||
{
|
||||
LoginAccountContext c;
|
||||
c.username = a.account_name;
|
||||
c.password = password;
|
||||
auto e = EncryptPasswordFromContext(c);
|
||||
a.account_password = e.password;
|
||||
|
||||
int success = LoginAccountsRepository::UpdateOne(db, a);
|
||||
|
||||
return success ? a : NewEntity();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EQEMU_LOGIN_ACCOUNTS_REPOSITORY_H
|
||||
|
||||
Reference in New Issue
Block a user