Implemented getting users using their email from the UserDB.

This commit is contained in:
Relintai 2023-09-22 16:33:21 +02:00
parent 56ae9e3734
commit 4faeb67438
10 changed files with 117 additions and 5 deletions

View File

@ -11,6 +11,9 @@ Ref<User> UserManager::get_user(const int id) {
Ref<User> UserManager::get_user_name(const String &user_name) {
return call("_get_user_name", user_name);
}
Ref<User> UserManager::get_user_email(const String &user_email) {
return call("_get_user_email", user_email);
}
void UserManager::save_user(const Ref<User> &user) {
call("_save_user", user);
}
@ -30,6 +33,9 @@ Ref<User> UserManager::_get_user(const int id) {
Ref<User> UserManager::_get_user_name(const String &user_name) {
return Ref<User>();
}
Ref<User> UserManager::_get_user_email(const String &user_email) {
return Ref<User>();
}
void UserManager::_save_user(Ref<User> user) {
}
Ref<User> 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);

View File

@ -9,14 +9,14 @@
class User;
//TODO UserManagerStatic, UserManagerDB, UserManagerFile
class UserManager : public Node {
GDCLASS(UserManager, Node);
public:
Ref<User> get_user(const int id);
Ref<User> get_user_name(const String &user_name);
Ref<User> get_user_email(const String &user_email);
void save_user(const Ref<User> &user);
Ref<User> create_user();
@ -26,6 +26,8 @@ public:
virtual Ref<User> _get_user(const int id);
virtual Ref<User> _get_user_name(const String &user);
virtual Ref<User> _get_user_email(const String &user_email);
virtual void _save_user(Ref<User> user);
virtual Ref<User> _create_user();

View File

@ -103,7 +103,7 @@ Ref<User> UserManagerDB::_get_user(const int id) {
return user;
}
Ref<User> UserManagerDB::_get_user_name(const String &user_name) {
if (user_name == "") {
if (user_name.empty()) {
return Ref<User>();
}
@ -136,6 +136,41 @@ Ref<User> UserManagerDB::_get_user_name(const String &user_name) {
return user;
}
Ref<User> UserManagerDB::_get_user_email(const String &user_email) {
if (user_email.empty()) {
return Ref<User>();
}
Ref<QueryBuilder> 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<QueryResult> r = b->run();
if (!r->next_row()) {
return Ref<User>();
}
Ref<User> 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> user) {
Ref<QueryBuilder> b = get_query_builder();

View File

@ -30,6 +30,8 @@ public:
Ref<User> _get_user(const int id);
Ref<User> _get_user_name(const String &user);
Ref<User> _get_user_email(const String &user_email);
void _save_user(Ref<User> user);
Ref<User> _create_user();

View File

@ -50,6 +50,25 @@ Ref<User> UserManagerFile::_get_user_name(const String &user_name) {
_rw_lock.read_unlock();
return Ref<User>();
}
Ref<User> UserManagerFile::_get_user_email(const String &user_email) {
_rw_lock.read_lock();
for (int i = 0; i < _users.size(); ++i) {
Ref<User> 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<User>();
}
void UserManagerFile::_save_user(Ref<User> user) {
_save_queued = true;
}

View File

@ -19,6 +19,8 @@ public:
Ref<User> _get_user(const int id);
Ref<User> _get_user_name(const String &user);
Ref<User> _get_user_email(const String &user_email);
void _save_user(Ref<User> user);
Ref<User> _create_user();

View File

@ -20,6 +20,20 @@ Ref<User> UserManagerStatic::_get_user_name(const String &user_name) {
return Ref<User>();
}
Ref<User> UserManagerStatic::_get_user_email(const String &user_email) {
for (int i = 0; i < _users.size(); ++i) {
Ref<User> u = _users[i];
if (u.is_valid()) {
if (u->get_email() == user_email) {
return u;
}
}
}
return Ref<User>();
}
void UserManagerStatic::_save_user(Ref<User> user) {
//With this class Users are serialized via editor properties, ignore
}

View File

@ -16,6 +16,8 @@ class UserManagerStatic : public UserManager {
public:
Ref<User> _get_user(const int id);
Ref<User> _get_user_name(const String &user);
Ref<User> _get_user_email(const String &user_email);
void _save_user(Ref<User> user);
Ref<User> _create_user();

View File

@ -38,6 +38,28 @@ Ref<User> UserDB::get_user_name(const String &user_name) {
return Ref<User>();
}
Ref<User> 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<User> u = _users[i];
if (u.is_valid()) {
if (u->get_email() == user_email) {
_lock.read_unlock();
return u;
}
}
}
_lock.read_unlock();
return Ref<User>();
}
void UserDB::save_user(const Ref<User> &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);

View File

@ -29,6 +29,8 @@ public:
Ref<User> get_user(const int id);
Ref<User> get_user_name(const String &user_name);
Ref<User> get_user_email(const String &user_email);
void save_user(const Ref<User> &user);
Ref<User> create_user();