From 14100c3ac0ff211b0deb0b36558969e6190c25b0 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 20 Nov 2024 21:57:35 +0100 Subject: [PATCH] Added a method to get all users from the UserDB and UserManagers. --- modules/users/managers/user_manager.cpp | 26 ++++++++++++-- modules/users/managers/user_manager.h | 5 ++- modules/users/managers/user_manager_db.cpp | 34 ++++++++++++++++++- modules/users/managers/user_manager_db.h | 3 +- modules/users/managers/user_manager_file.cpp | 18 +++++++++- modules/users/managers/user_manager_file.h | 3 +- .../users/managers/user_manager_static.cpp | 14 +++++++- modules/users/managers/user_manager_static.h | 3 +- modules/users/singleton/user_db.cpp | 22 ++++++++++++ modules/users/singleton/user_db.h | 4 +-- 10 files changed, 121 insertions(+), 11 deletions(-) diff --git a/modules/users/managers/user_manager.cpp b/modules/users/managers/user_manager.cpp index 8b52e4b81..d1c5a1e4a 100644 --- a/modules/users/managers/user_manager.cpp +++ b/modules/users/managers/user_manager.cpp @@ -82,8 +82,26 @@ bool UserManager::_is_email_taken(const String &email) { return false; } -Vector> UserManager::get_all() { - return Vector>(); +Vector> UserManager::get_all_as_vector() { + Vector> ret; + + Array res = call("_get_all"); + + ret.resize(res.size()); + + for (int i = 0; i < res.size(); ++i) { + ret.write[i] = Ref(res.get(i)); + } + + return ret; +} + +Array UserManager::get_all_users() { + return call("_get_all_users"); +} + +Array UserManager::_get_all_users() { + return Array(); } UserManager::UserManager() { @@ -110,6 +128,7 @@ void UserManager::_bind_methods() { BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_is_username_taken", PropertyInfo(Variant::STRING, "user_name"))); BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_is_email_taken", PropertyInfo(Variant::STRING, "email"))); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::ARRAY, "ret"), "_get_all_users")); ClassDB::bind_method(D_METHOD("get_user", "id"), &UserManager::get_user); ClassDB::bind_method(D_METHOD("get_user_name", "user_name"), &UserManager::get_user_name); @@ -131,4 +150,7 @@ void UserManager::_bind_methods() { ClassDB::bind_method(D_METHOD("_is_username_taken", "user_name"), &UserManager::_is_username_taken); ClassDB::bind_method(D_METHOD("_is_email_taken", "email"), &UserManager::_is_email_taken); + + ClassDB::bind_method(D_METHOD("get_all_users"), &UserManager::get_all_users); + ClassDB::bind_method(D_METHOD("_get_all_users"), &UserManager::_get_all_users); } diff --git a/modules/users/managers/user_manager.h b/modules/users/managers/user_manager.h index 7d1300be7..a3b04db41 100644 --- a/modules/users/managers/user_manager.h +++ b/modules/users/managers/user_manager.h @@ -66,7 +66,10 @@ public: virtual bool _is_username_taken(const String &user_name); virtual bool _is_email_taken(const String &email); - virtual Vector> get_all(); + virtual Vector> get_all_as_vector(); + + Array get_all_users(); + virtual Array _get_all_users(); UserManager(); ~UserManager(); diff --git a/modules/users/managers/user_manager_db.cpp b/modules/users/managers/user_manager_db.cpp index 1db976246..7e15374cd 100644 --- a/modules/users/managers/user_manager_db.cpp +++ b/modules/users/managers/user_manager_db.cpp @@ -274,7 +274,7 @@ bool UserManagerDB::_is_email_taken(const String &email) { return r->next_row(); } -Vector> UserManagerDB::get_all() { +Vector> UserManagerDB::get_all_as_vector() { Ref b = get_query_builder(); b->select("id, username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); @@ -306,6 +306,38 @@ Vector> UserManagerDB::get_all() { return users; } +Array UserManagerDB::_get_all_users() { + Ref b = get_query_builder(); + + b->select("id, username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); + b->from(_database_table_name); + b->end_command(); + // b->print(); + + Array users; + + Ref r = b->run(); + + while (r->next_row()) { + Ref user = create_user(); + + user->set_user_id(r->get_cell_int(0)); + user->set_user_name(r->get_cell(1)); + user->set_email(r->get_cell(2)); + user->set_rank(r->get_cell_int(3)); + user->set_pre_salt(r->get_cell(4)); + user->set_post_salt(r->get_cell(5)); + user->set_password_hash(r->get_cell(6)); + user->set_banned(r->get_cell_bool(7)); + user->set_password_reset_token(r->get_cell(8)); + user->set_locked(r->get_cell_bool(9)); + + users.push_back(user.get_ref_ptr()); + } + + return users; +} + void UserManagerDB::create_table() { call("_create_table"); } diff --git a/modules/users/managers/user_manager_db.h b/modules/users/managers/user_manager_db.h index bfc038c99..8f1596e47 100644 --- a/modules/users/managers/user_manager_db.h +++ b/modules/users/managers/user_manager_db.h @@ -70,7 +70,8 @@ public: bool _is_username_taken(const String &user_name); bool _is_email_taken(const String &email); - Vector> get_all(); + Vector> get_all_as_vector(); + Array _get_all_users(); void create_table(); void drop_table(); diff --git a/modules/users/managers/user_manager_file.cpp b/modules/users/managers/user_manager_file.cpp index 62d6b1653..fe270bc1a 100644 --- a/modules/users/managers/user_manager_file.cpp +++ b/modules/users/managers/user_manager_file.cpp @@ -152,10 +152,26 @@ bool UserManagerFile::_is_email_taken(const String &email) { return false; } -Vector> UserManagerFile::get_all() { +Vector> UserManagerFile::get_all_as_vector() { return _users; } +Array UserManagerFile::_get_all_users() { + Array ret; + + _rw_lock.read_lock(); + + ret.resize(_users.size()); + + for (int i = 0; i < _users.size(); ++i) { + ret[i] = Variant(_users[i].get_ref_ptr()); + } + + _rw_lock.read_unlock(); + + return ret; +} + UserManagerFile::UserManagerFile() { set_process_internal(true); _save_folder_path = "users"; diff --git a/modules/users/managers/user_manager_file.h b/modules/users/managers/user_manager_file.h index 8b3b3a2fe..2c7eee853 100644 --- a/modules/users/managers/user_manager_file.h +++ b/modules/users/managers/user_manager_file.h @@ -59,7 +59,8 @@ public: bool _is_username_taken(const String &user_name); bool _is_email_taken(const String &email); - Vector> get_all(); + Vector> get_all_as_vector(); + Array _get_all_users(); UserManagerFile(); ~UserManagerFile(); diff --git a/modules/users/managers/user_manager_static.cpp b/modules/users/managers/user_manager_static.cpp index a5490691e..b42bea66a 100644 --- a/modules/users/managers/user_manager_static.cpp +++ b/modules/users/managers/user_manager_static.cpp @@ -105,10 +105,22 @@ bool UserManagerStatic::_is_email_taken(const String &email) { return false; } -Vector> UserManagerStatic::get_all() { +Vector> UserManagerStatic::get_all_as_vector() { return _users; } +Array UserManagerStatic::_get_all_users() { + Array ret; + + ret.resize(_users.size()); + + for (int i = 0; i < _users.size(); ++i) { + ret[i] = Variant(_users[i].get_ref_ptr()); + } + + return ret; +} + Vector UserManagerStatic::get_users() { Vector r; for (int i = 0; i < _users.size(); i++) { diff --git a/modules/users/managers/user_manager_static.h b/modules/users/managers/user_manager_static.h index b26a2b334..360aee218 100644 --- a/modules/users/managers/user_manager_static.h +++ b/modules/users/managers/user_manager_static.h @@ -56,7 +56,8 @@ public: bool _is_username_taken(const String &user_name); bool _is_email_taken(const String &email); - Vector> get_all(); + Vector> get_all_as_vector(); + Array _get_all_users(); Vector get_users(); void set_users(const Vector &users); diff --git a/modules/users/singleton/user_db.cpp b/modules/users/singleton/user_db.cpp index 43bf4709e..1521d8365 100644 --- a/modules/users/singleton/user_db.cpp +++ b/modules/users/singleton/user_db.cpp @@ -158,6 +158,26 @@ bool UserDB::is_email_taken(const String &email) { return false; } +Array UserDB::get_all_users() { + if (_user_manager) { + return _user_manager->get_all_users(); + } + + Array ret; + + _lock.read_lock(); + + ret.resize(_users.size()); + + for (int i = 0; i < _users.size(); ++i) { + ret[i] = Variant(_users[i].get_ref_ptr()); + } + + _lock.read_unlock(); + + return ret; +} + UserManager *UserDB::get_user_manager() { return _user_manager; } @@ -193,6 +213,8 @@ void UserDB::_bind_methods() { ClassDB::bind_method(D_METHOD("is_username_taken", "user_name"), &UserDB::is_username_taken); ClassDB::bind_method(D_METHOD("is_email_taken", "email"), &UserDB::is_email_taken); + ClassDB::bind_method(D_METHOD("get_all_users"), &UserDB::get_all_users); + ClassDB::bind_method(D_METHOD("get_user_manager"), &UserDB::get_user_manager_bind); } diff --git a/modules/users/singleton/user_db.h b/modules/users/singleton/user_db.h index 33958d46b..bcd63fb89 100644 --- a/modules/users/singleton/user_db.h +++ b/modules/users/singleton/user_db.h @@ -50,8 +50,6 @@ public: //get_user_id //get_user_index //get_user_count - //get_users - //get_users_bind //delete_user_id //delete_user_index //delete_user_name @@ -69,6 +67,8 @@ public: bool is_username_taken(const String &user_name); bool is_email_taken(const String &email); + Array get_all_users(); + UserManager *get_user_manager(); void set_user_manager(UserManager *um);