Skip to content

Commit

Permalink
add admin account creation options from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
EmosewaMC committed Jul 2, 2024
1 parent 9400ee1 commit b870638
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions dDatabase/GameDatabase/ITables/IAccounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class IAccounts {

// Add a new account to the database.
virtual void InsertNewAccount(const std::string_view username, const std::string_view bcryptpassword) = 0;

// Update the GameMaster level of an account.
virtual void UpdateAccountGmLevel(const uint32_t accountId, const eGameMasterLevel gmLevel) = 0;
};

#endif //!__IACCOUNTS__H__
1 change: 1 addition & 0 deletions dDatabase/GameDatabase/MySQL/MySQLDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class MySQLDatabase : public GameDatabase {
void AddBehavior(const IBehaviors::Info& info) override;
std::string GetBehavior(const int32_t behaviorId) override;
void RemoveBehavior(const int32_t characterId) override;
void UpdateAccountGmLevel(const uint32_t accountId, const eGameMasterLevel gmLevel) override;
private:

// Generic query functions that can be used for any query.
Expand Down
4 changes: 4 additions & 0 deletions dDatabase/GameDatabase/MySQL/Tables/Accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ void MySQLDatabase::UpdateAccountPassword(const uint32_t accountId, const std::s
void MySQLDatabase::InsertNewAccount(const std::string_view username, const std::string_view bcryptpassword) {
ExecuteInsert("INSERT INTO accounts (name, password, gm_level) VALUES (?, ?, ?);", username, bcryptpassword, static_cast<int32_t>(eGameMasterLevel::OPERATOR));
}

void MySQLDatabase::UpdateAccountGmLevel(const uint32_t accountId, const eGameMasterLevel gmLevel) {
ExecuteUpdate("UPDATE accounts SET gm_level = ? WHERE id = ?;", static_cast<int32_t>(gmLevel), accountId);
}
31 changes: 27 additions & 4 deletions dMasterServer/MasterServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "Start.h"
#include "Server.h"
#include "CDZoneTableTable.h"
#include "eGameMasterLevel.h"

namespace Game {
Logger* logger = nullptr;
Expand Down Expand Up @@ -187,15 +188,20 @@ int main(int argc, char** argv) {
std::cout << "Enter a username: ";
std::cin >> username;

auto checkIsAdmin = []() {
std::string admin;
std::cout << "Should this account have administrator privileges? y will set their GameMaster level to 9, anything else will set the GameMaster level to 0. [y/n]? ";
std::cin >> admin;
return admin == "y" || admin == "yes";
};

auto accountId = Database::Get()->GetAccountInfo(username);
if (accountId) {
if (accountId && accountId->id != 0) {
LOG("Account with name \"%s\" already exists", username.c_str());
std::cout << "Do you want to change the password of that account? [y/n]?";
std::string prompt = "";
std::cin >> prompt;
if (prompt == "y" || prompt == "yes") {
if (accountId->id == 0) return EXIT_FAILURE;

//Read the password from the console without echoing it.
#ifdef __linux__
//This function is obsolete, but it only meant to be used by the
Expand All @@ -220,6 +226,16 @@ int main(int argc, char** argv) {
} else {
LOG("Account \"%s\" was not updated.", username.c_str());
}

std::cout << "Update admin privileges? [y/n]? ";
std::string admin;
std::cin >> admin;
bool updateAdmin = admin == "y" || admin == "yes";
if (updateAdmin) {
bool isOperator = checkIsAdmin();
Database::Get()->UpdateAccountGmLevel(accountId->id, isOperator ? eGameMasterLevel::OPERATOR : eGameMasterLevel::CIVILIAN);
}

return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -250,6 +266,13 @@ int main(int argc, char** argv) {
}

LOG("Account created successfully!");

accountId = Database::Get()->GetAccountInfo(username);
if (accountId) {
bool isOperator = checkIsAdmin();
Database::Get()->UpdateAccountGmLevel(accountId->id, isOperator ? eGameMasterLevel::OPERATOR : eGameMasterLevel::CIVILIAN);
}

return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -558,7 +581,7 @@ void HandlePacket(Packet* packet) {
inStream.Read(sessionKey);
LUString username;
inStream.Read(username);

for (auto it : activeSessions) {
if (it.second == username.string) {
activeSessions.erase(it.first);
Expand Down

0 comments on commit b870638

Please sign in to comment.