Added a method to get all users from the UserDB and UserManagers.

This commit is contained in:
Relintai 2024-11-20 21:57:35 +01:00
parent e7e91f7aa2
commit 14100c3ac0
10 changed files with 121 additions and 11 deletions

View File

@ -82,8 +82,26 @@ bool UserManager::_is_email_taken(const String &email) {
return false;
}
Vector<Ref<User>> UserManager::get_all() {
return Vector<Ref<User>>();
Vector<Ref<User>> UserManager::get_all_as_vector() {
Vector<Ref<User>> ret;
Array res = call("_get_all");
ret.resize(res.size());
for (int i = 0; i < res.size(); ++i) {
ret.write[i] = Ref<User>(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);
}

View File

@ -66,7 +66,10 @@ public:
virtual bool _is_username_taken(const String &user_name);
virtual bool _is_email_taken(const String &email);
virtual Vector<Ref<User>> get_all();
virtual Vector<Ref<User>> get_all_as_vector();
Array get_all_users();
virtual Array _get_all_users();
UserManager();
~UserManager();

View File

@ -274,7 +274,7 @@ bool UserManagerDB::_is_email_taken(const String &email) {
return r->next_row();
}
Vector<Ref<User>> UserManagerDB::get_all() {
Vector<Ref<User>> UserManagerDB::get_all_as_vector() {
Ref<QueryBuilder> 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<Ref<User>> UserManagerDB::get_all() {
return users;
}
Array UserManagerDB::_get_all_users() {
Ref<QueryBuilder> 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<QueryResult> r = b->run();
while (r->next_row()) {
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(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");
}

View File

@ -70,7 +70,8 @@ public:
bool _is_username_taken(const String &user_name);
bool _is_email_taken(const String &email);
Vector<Ref<User>> get_all();
Vector<Ref<User>> get_all_as_vector();
Array _get_all_users();
void create_table();
void drop_table();

View File

@ -152,10 +152,26 @@ bool UserManagerFile::_is_email_taken(const String &email) {
return false;
}
Vector<Ref<User>> UserManagerFile::get_all() {
Vector<Ref<User>> 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";

View File

@ -59,7 +59,8 @@ public:
bool _is_username_taken(const String &user_name);
bool _is_email_taken(const String &email);
Vector<Ref<User>> get_all();
Vector<Ref<User>> get_all_as_vector();
Array _get_all_users();
UserManagerFile();
~UserManagerFile();

View File

@ -105,10 +105,22 @@ bool UserManagerStatic::_is_email_taken(const String &email) {
return false;
}
Vector<Ref<User>> UserManagerStatic::get_all() {
Vector<Ref<User>> 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<Variant> UserManagerStatic::get_users() {
Vector<Variant> r;
for (int i = 0; i < _users.size(); i++) {

View File

@ -56,7 +56,8 @@ public:
bool _is_username_taken(const String &user_name);
bool _is_email_taken(const String &email);
Vector<Ref<User>> get_all();
Vector<Ref<User>> get_all_as_vector();
Array _get_all_users();
Vector<Variant> get_users();
void set_users(const Vector<Variant> &users);

View File

@ -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);
}

View File

@ -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);