From 4faeb67438ea11f1f2e3a260b2d20d0a8a3be35e Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 22 Sep 2023 16:33:21 +0200 Subject: [PATCH] Implemented getting users using their email from the UserDB. --- modules/users/managers/user_manager.cpp | 10 +++++ modules/users/managers/user_manager.h | 6 ++- modules/users/managers/user_manager_db.cpp | 37 ++++++++++++++++++- modules/users/managers/user_manager_db.h | 2 + modules/users/managers/user_manager_file.cpp | 19 ++++++++++ modules/users/managers/user_manager_file.h | 2 + .../users/managers/user_manager_static.cpp | 14 +++++++ modules/users/managers/user_manager_static.h | 2 + modules/users/singleton/user_db.cpp | 28 +++++++++++++- modules/users/singleton/user_db.h | 2 + 10 files changed, 117 insertions(+), 5 deletions(-) diff --git a/modules/users/managers/user_manager.cpp b/modules/users/managers/user_manager.cpp index 931c85538..887469e1a 100644 --- a/modules/users/managers/user_manager.cpp +++ b/modules/users/managers/user_manager.cpp @@ -11,6 +11,9 @@ Ref UserManager::get_user(const int id) { Ref UserManager::get_user_name(const String &user_name) { return call("_get_user_name", user_name); } +Ref UserManager::get_user_email(const String &user_email) { + return call("_get_user_email", user_email); +} void UserManager::save_user(const Ref &user) { call("_save_user", user); } @@ -30,6 +33,9 @@ Ref UserManager::_get_user(const int id) { Ref UserManager::_get_user_name(const String &user_name) { return Ref(); } +Ref UserManager::_get_user_email(const String &user_email) { + return Ref(); +} void UserManager::_save_user(Ref user) { } Ref UserManager::_create_user() { @@ -66,6 +72,7 @@ void UserManager::_notification(int p_what) { void UserManager::_bind_methods() { BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "ret", PROPERTY_HINT_RESOURCE_TYPE, "User"), "_get_user", PropertyInfo(Variant::INT, "id"))); BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "ret", PROPERTY_HINT_RESOURCE_TYPE, "User"), "_get_user_name", PropertyInfo(Variant::STRING, "user_name"))); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "ret", PROPERTY_HINT_RESOURCE_TYPE, "User"), "_get_user_email", PropertyInfo(Variant::STRING, "user_email"))); BIND_VMETHOD(MethodInfo("_save_user", PropertyInfo(Variant::OBJECT, "user", PROPERTY_HINT_RESOURCE_TYPE, "User"))); BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "ret", PROPERTY_HINT_RESOURCE_TYPE, "User"), "_create_user")); @@ -75,6 +82,8 @@ void UserManager::_bind_methods() { 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); + ClassDB::bind_method(D_METHOD("get_user_email", "user_email"), &UserManager::get_user_email); + ClassDB::bind_method(D_METHOD("save_user", "user"), &UserManager::save_user); ClassDB::bind_method(D_METHOD("create_user"), &UserManager::create_user); @@ -84,6 +93,7 @@ void UserManager::_bind_methods() { 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); + ClassDB::bind_method(D_METHOD("_get_user_email", "user_email"), &UserManager::_get_user_email); ClassDB::bind_method(D_METHOD("_save_user", "user"), &UserManager::_save_user); ClassDB::bind_method(D_METHOD("_create_user"), &UserManager::_create_user); diff --git a/modules/users/managers/user_manager.h b/modules/users/managers/user_manager.h index 132485bdc..8bc199531 100644 --- a/modules/users/managers/user_manager.h +++ b/modules/users/managers/user_manager.h @@ -9,14 +9,14 @@ class User; -//TODO UserManagerStatic, UserManagerDB, UserManagerFile - class UserManager : public Node { GDCLASS(UserManager, Node); public: Ref get_user(const int id); Ref get_user_name(const String &user_name); + Ref get_user_email(const String &user_email); + void save_user(const Ref &user); Ref create_user(); @@ -26,6 +26,8 @@ public: virtual Ref _get_user(const int id); virtual Ref _get_user_name(const String &user); + virtual Ref _get_user_email(const String &user_email); + virtual void _save_user(Ref user); virtual Ref _create_user(); diff --git a/modules/users/managers/user_manager_db.cpp b/modules/users/managers/user_manager_db.cpp index 16832cba2..a1c831c01 100644 --- a/modules/users/managers/user_manager_db.cpp +++ b/modules/users/managers/user_manager_db.cpp @@ -103,7 +103,7 @@ Ref UserManagerDB::_get_user(const int id) { return user; } Ref UserManagerDB::_get_user_name(const String &user_name) { - if (user_name == "") { + if (user_name.empty()) { return Ref(); } @@ -136,6 +136,41 @@ Ref UserManagerDB::_get_user_name(const String &user_name) { return user; } +Ref UserManagerDB::_get_user_email(const String &user_email) { + if (user_email.empty()) { + return Ref(); + } + + Ref b = get_query_builder(); + + b->select("id, username, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); + b->from(_database_table_name); + b->where()->wps("email", user_email); + b->end_command(); + + Ref r = b->run(); + + if (!r->next_row()) { + return Ref(); + } + + Ref user; + user = create_user(); + + user->set_user_id(r->get_cell_int(0)); + user->set_user_name(r->get_cell(1)); + user->set_email(user_email); + user->set_rank(r->get_cell_int(2)); + user->set_pre_salt(r->get_cell(3)); + user->set_post_salt(r->get_cell(4)); + user->set_password_hash(r->get_cell(5)); + user->set_banned(r->get_cell_bool(6)); + user->set_password_reset_token(r->get_cell(7)); + user->set_locked(r->get_cell_bool(8)); + + return user; +} + void UserManagerDB::_save_user(Ref user) { Ref b = get_query_builder(); diff --git a/modules/users/managers/user_manager_db.h b/modules/users/managers/user_manager_db.h index a60d05f36..a63c35e66 100644 --- a/modules/users/managers/user_manager_db.h +++ b/modules/users/managers/user_manager_db.h @@ -30,6 +30,8 @@ public: Ref _get_user(const int id); Ref _get_user_name(const String &user); + Ref _get_user_email(const String &user_email); + void _save_user(Ref user); Ref _create_user(); diff --git a/modules/users/managers/user_manager_file.cpp b/modules/users/managers/user_manager_file.cpp index edfc9d1d1..ef642c93b 100644 --- a/modules/users/managers/user_manager_file.cpp +++ b/modules/users/managers/user_manager_file.cpp @@ -50,6 +50,25 @@ Ref UserManagerFile::_get_user_name(const String &user_name) { _rw_lock.read_unlock(); return Ref(); } +Ref UserManagerFile::_get_user_email(const String &user_email) { + _rw_lock.read_lock(); + + for (int i = 0; i < _users.size(); ++i) { + Ref u = _users[i]; + + if (u.is_valid()) { + if (u->get_email() == user_email) { + _rw_lock.read_unlock(); + + return u; + } + } + } + + _rw_lock.read_unlock(); + return Ref(); +} + void UserManagerFile::_save_user(Ref user) { _save_queued = true; } diff --git a/modules/users/managers/user_manager_file.h b/modules/users/managers/user_manager_file.h index f98208ca5..9cadff9f9 100644 --- a/modules/users/managers/user_manager_file.h +++ b/modules/users/managers/user_manager_file.h @@ -19,6 +19,8 @@ public: Ref _get_user(const int id); Ref _get_user_name(const String &user); + Ref _get_user_email(const String &user_email); + void _save_user(Ref user); Ref _create_user(); diff --git a/modules/users/managers/user_manager_static.cpp b/modules/users/managers/user_manager_static.cpp index 0333db808..4ef3dcc4f 100644 --- a/modules/users/managers/user_manager_static.cpp +++ b/modules/users/managers/user_manager_static.cpp @@ -20,6 +20,20 @@ Ref UserManagerStatic::_get_user_name(const String &user_name) { return Ref(); } +Ref UserManagerStatic::_get_user_email(const String &user_email) { + for (int i = 0; i < _users.size(); ++i) { + Ref u = _users[i]; + + if (u.is_valid()) { + if (u->get_email() == user_email) { + return u; + } + } + } + + return Ref(); +} + void UserManagerStatic::_save_user(Ref user) { //With this class Users are serialized via editor properties, ignore } diff --git a/modules/users/managers/user_manager_static.h b/modules/users/managers/user_manager_static.h index 904f6f192..c3779e654 100644 --- a/modules/users/managers/user_manager_static.h +++ b/modules/users/managers/user_manager_static.h @@ -16,6 +16,8 @@ class UserManagerStatic : public UserManager { public: Ref _get_user(const int id); Ref _get_user_name(const String &user); + Ref _get_user_email(const String &user_email); + void _save_user(Ref user); Ref _create_user(); diff --git a/modules/users/singleton/user_db.cpp b/modules/users/singleton/user_db.cpp index e16c4bdd5..d970caa49 100644 --- a/modules/users/singleton/user_db.cpp +++ b/modules/users/singleton/user_db.cpp @@ -38,6 +38,28 @@ Ref UserDB::get_user_name(const String &user_name) { return Ref(); } +Ref UserDB::get_user_email(const String &user_email) { + if (_user_manager) { + return _user_manager->get_user_email(user_email); + } + + _lock.read_lock(); + + for (int i = 0; i < _users.size(); ++i) { + Ref u = _users[i]; + + if (u.is_valid()) { + if (u->get_email() == user_email) { + _lock.read_unlock(); + return u; + } + } + } + + _lock.read_unlock(); + + return Ref(); +} void UserDB::save_user(const Ref &user) { if (_user_manager) { _user_manager->save_user(user); @@ -121,16 +143,18 @@ UserDB *UserDB::get_singleton() { UserDB::UserDB() { _self = this; - _user_manager = nullptr; + _user_manager = NULL; } UserDB::~UserDB() { - _self = nullptr; + _self = NULL; } void UserDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_user", "id"), &UserDB::get_user); ClassDB::bind_method(D_METHOD("get_user_name", "user_name"), &UserDB::get_user_name); + ClassDB::bind_method(D_METHOD("get_user_email", "user_email"), &UserDB::get_user_email); + ClassDB::bind_method(D_METHOD("save_user", "user"), &UserDB::save_user); ClassDB::bind_method(D_METHOD("create_user"), &UserDB::create_user); diff --git a/modules/users/singleton/user_db.h b/modules/users/singleton/user_db.h index 5844ce96f..969cf3c8f 100644 --- a/modules/users/singleton/user_db.h +++ b/modules/users/singleton/user_db.h @@ -29,6 +29,8 @@ public: Ref get_user(const int id); Ref get_user_name(const String &user_name); + Ref get_user_email(const String &user_email); + void save_user(const Ref &user); Ref create_user();