From 0ef9dc8fcf1f7d89fe730bc0a9b8890b612cb0e8 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 21 Jul 2022 18:08:52 +0200 Subject: [PATCH] Added a few bindings to UserManagerStatic to help with creating users form the editor. --- .../users/managers/user_manager_static.cpp | 63 ++++++++++++++++++- modules/users/managers/user_manager_static.h | 16 +++++ modules/users/users/user.cpp | 6 ++ modules/users/users/user.h | 2 + 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/modules/users/managers/user_manager_static.cpp b/modules/users/managers/user_manager_static.cpp index e6a9e5cc6..0d097fdc1 100644 --- a/modules/users/managers/user_manager_static.cpp +++ b/modules/users/managers/user_manager_static.cpp @@ -24,8 +24,12 @@ void UserManagerStatic::_save_user(Ref user) { //With this class Users are serialized via editor properties, ignore } Ref UserManagerStatic::_create_user() { - //With this class Users are managed via the editor editor - return Ref(); + Ref u; + u.instance(); + + _users.push_back(u); + + return u; } bool UserManagerStatic::_is_username_taken(const String &user_name) { for (int i = 0; i < _users.size(); ++i) { @@ -74,6 +78,45 @@ void UserManagerStatic::set_users(const Vector &users) { } } +String UserManagerStatic::get_create_user_name_bind() { + return _create_user_name; +} +void UserManagerStatic::set_create_user_name_bind(const String &val) { + _create_user_name = val; +} + +String UserManagerStatic::get_create_user_email_bind() { + return _create_user_email; +} +void UserManagerStatic::set_create_user_email_bind(const String &val) { + _create_user_email = val; +} + +String UserManagerStatic::get_create_user_password_bind() { + return _create_user_password; +} +void UserManagerStatic::set_create_user_password_bind(const String &val) { + _create_user_password = val; +} + +bool UserManagerStatic::get_create_user_bind() { + return false; +} +void UserManagerStatic::set_create_user_bind(const bool val) { + if (val) { + Ref u = create_user(); + + u->set_name_user_input(_create_user_name); + u->set_email_user_input(_create_user_email); + u->create_password(_create_user_password); + u->save(); + + _create_user_password = ""; + _create_user_email = ""; + _create_user_name = ""; + } +} + UserManagerStatic::UserManagerStatic() { } @@ -84,4 +127,20 @@ void UserManagerStatic::_bind_methods() { ClassDB::bind_method(D_METHOD("get_users"), &UserManagerStatic::get_users); ClassDB::bind_method(D_METHOD("set_users", "users"), &UserManagerStatic::set_users); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "users", PROPERTY_HINT_NONE, "17/17:User", PROPERTY_USAGE_DEFAULT, "User"), "set_users", "get_users"); + + ClassDB::bind_method(D_METHOD("get_create_user_name"), &UserManagerStatic::get_create_user_name_bind); + ClassDB::bind_method(D_METHOD("set_create_user_name", "val"), &UserManagerStatic::set_create_user_name_bind); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "create_user_name"), "set_create_user_name", "get_create_user_name"); + + ClassDB::bind_method(D_METHOD("get_create_user_email"), &UserManagerStatic::get_create_user_email_bind); + ClassDB::bind_method(D_METHOD("set_create_user_email", "val"), &UserManagerStatic::set_create_user_email_bind); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "create_user_email"), "set_create_user_email", "get_create_user_email"); + + ClassDB::bind_method(D_METHOD("get_create_user_password"), &UserManagerStatic::get_create_user_password_bind); + ClassDB::bind_method(D_METHOD("set_create_user_password", "val"), &UserManagerStatic::set_create_user_password_bind); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "create_user_password"), "set_create_user_password", "get_create_user_password"); + + ClassDB::bind_method(D_METHOD("get_create_user"), &UserManagerStatic::get_create_user_bind); + ClassDB::bind_method(D_METHOD("set_create_user", "val"), &UserManagerStatic::set_create_user_bind); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "create_user"), "set_create_user", "get_create_user"); } diff --git a/modules/users/managers/user_manager_static.h b/modules/users/managers/user_manager_static.h index fd691488c..6fdee7402 100644 --- a/modules/users/managers/user_manager_static.h +++ b/modules/users/managers/user_manager_static.h @@ -28,6 +28,18 @@ public: Vector get_users(); void set_users(const Vector &users); + String get_create_user_name_bind(); + void set_create_user_name_bind(const String &val); + + String get_create_user_email_bind(); + void set_create_user_email_bind(const String &val); + + String get_create_user_password_bind(); + void set_create_user_password_bind(const String &val); + + bool get_create_user_bind(); + void set_create_user_bind(const bool val); + UserManagerStatic(); ~UserManagerStatic(); @@ -35,6 +47,10 @@ protected: static void _bind_methods(); Vector> _users; + + String _create_user_name; + String _create_user_email; + String _create_user_password; }; #endif diff --git a/modules/users/users/user.cpp b/modules/users/users/user.cpp index 3d5c46b26..c5e0e3dd2 100644 --- a/modules/users/users/user.cpp +++ b/modules/users/users/user.cpp @@ -89,6 +89,10 @@ String User::_hash_password(const String &p_password) { return p.sha256_text(); } +void User::save() { + emit_changed(); +} + User::User() { _rank = 0; _banned = false; @@ -147,6 +151,8 @@ void User::_bind_methods() { ClassDB::bind_method(D_METHOD("_create_password", "password"), &User::_create_password); ClassDB::bind_method(D_METHOD("_hash_password", "password"), &User::_hash_password); + ClassDB::bind_method(D_METHOD("save"), &User::save); + BIND_ENUM_CONSTANT(PERMISSION_CREATE); BIND_ENUM_CONSTANT(PERMISSION_READ); BIND_ENUM_CONSTANT(PERMISSION_UPDATE); diff --git a/modules/users/users/user.h b/modules/users/users/user.h index 514e591a3..e6e1fb26c 100644 --- a/modules/users/users/user.h +++ b/modules/users/users/user.h @@ -56,6 +56,8 @@ public: virtual void _create_password(const String &p_password); virtual String _hash_password(const String &p_password); + void save(); + User(); ~User();