From b479b6a1ab9e6fcc163d991faf6913c91b08ac09 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 24 Jul 2022 01:08:56 +0200 Subject: [PATCH] Added a new UserModule class. It can be used to easily add data to Users. --- modules/users/SCsub | 1 + modules/users/config.py | 2 + modules/users/register_types.cpp | 2 + modules/users/users/user.cpp | 75 +++++++++++++++++++++++++++++ modules/users/users/user.h | 13 ++++- modules/users/users/user_module.cpp | 62 ++++++++++++++++++++++++ modules/users/users/user_module.h | 43 +++++++++++++++++ 7 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 modules/users/users/user_module.cpp create mode 100644 modules/users/users/user_module.h diff --git a/modules/users/SCsub b/modules/users/SCsub index 80538fe74..a1645c4cc 100644 --- a/modules/users/SCsub +++ b/modules/users/SCsub @@ -8,6 +8,7 @@ sources = [ "register_types.cpp", "users/user.cpp", + "users/user_module.cpp", "managers/user_manager.cpp", "managers/user_manager_static.cpp", diff --git a/modules/users/config.py b/modules/users/config.py index 46b955246..fdba5eb0d 100644 --- a/modules/users/config.py +++ b/modules/users/config.py @@ -11,6 +11,8 @@ def configure(env): def get_doc_classes(): return [ "User", + "UserModule", + "UserManager", "UserManagerStatic", "UserManagerFile", diff --git a/modules/users/register_types.cpp b/modules/users/register_types.cpp index 8e6343fd0..118cadd94 100644 --- a/modules/users/register_types.cpp +++ b/modules/users/register_types.cpp @@ -25,6 +25,7 @@ SOFTWARE. #include "core/engine.h" #include "users/user.h" +#include "users/user_module.h" #include "managers/user_manager.h" #include "managers/user_manager_file.h" @@ -51,6 +52,7 @@ UserDB *_user_db = nullptr; void register_users_types() { ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); diff --git a/modules/users/users/user.cpp b/modules/users/users/user.cpp index 7acf2671d..a0becd883 100644 --- a/modules/users/users/user.cpp +++ b/modules/users/users/user.cpp @@ -1,6 +1,8 @@ #include "user.h" #include "core/class_db.h" +#include "user_module.h" + int User::get_user_id() const { return _user_id; } @@ -71,6 +73,62 @@ void User::set_locked(const bool val) { _locked = val; } +void User::add_module(const Ref &module) { + _modules.push_back(module); + + if (module.is_valid()) { + Ref m = module; + + m->set_module_index(_modules.size() - 1); + m->set_user(this); + } +} +Ref User::get_module(const int index) { + ERR_FAIL_INDEX_V(index, _modules.size(), Ref()); + + return _modules[index]; +} +Ref User::get_module_named(const String &name) { + for (int i = 0; i < _modules.size(); ++i) { + Ref m = _modules[i]; + + if (m.is_valid() && m->get_module_name() == name) { + return m; + } + } + + return Ref(); +} +void User::remove_module(const int index) { + ERR_FAIL_INDEX(index, _modules.size()); + + _modules.remove(index); +} +int User::get_module_count() { + return _modules.size(); +} + +Vector User::get_modules() { + Vector r; + for (int i = 0; i < _modules.size(); i++) { + r.push_back(_modules[i].get_ref_ptr()); + } + return r; +} +void User::set_modules(const Vector &modules) { + _modules.clear(); + for (int i = 0; i < modules.size(); i++) { + Ref um = Ref(modules.get(i)); + + _modules.push_back(um); + + if (um.is_valid()) { + um->set_module_index(_modules.size() - 1); + um->set_user(this); + } + } +} + bool User::check_password(const String &p_password) { return call("_check_password", p_password); } @@ -122,6 +180,13 @@ User::User() { } User::~User() { + for (int i = 0; i < _modules.size(); ++i) { + Ref um = _modules[i]; + + if (um.is_valid()) { + um->set_user(nullptr); + } + } } void User::_bind_methods() { @@ -165,6 +230,16 @@ void User::_bind_methods() { ClassDB::bind_method(D_METHOD("set_locked", "val"), &User::set_locked); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "locked"), "set_locked", "get_locked"); + ClassDB::bind_method(D_METHOD("add_module", "module"), &User::add_module); + ClassDB::bind_method(D_METHOD("get_module", "index"), &User::get_module); + ClassDB::bind_method(D_METHOD("get_module_named", "name"), &User::get_module_named); + ClassDB::bind_method(D_METHOD("remove_module", "index"), &User::remove_module); + ClassDB::bind_method(D_METHOD("get_module_count"), &User::get_module_count); + + ClassDB::bind_method(D_METHOD("get_modules"), &User::get_modules); + ClassDB::bind_method(D_METHOD("set_modules", "modules"), &User::set_modules); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "modules", PROPERTY_HINT_NONE, "17/17:UserModule", PROPERTY_USAGE_DEFAULT, "UserModule"), "set_modules", "get_modules"); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_check_password", PropertyInfo(Variant::STRING, "password"))); BIND_VMETHOD(MethodInfo("_create_password", PropertyInfo(Variant::STRING, "password"))); BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::STRING, "ret"), "_hash_password", PropertyInfo(Variant::STRING, "password"))); diff --git a/modules/users/users/user.h b/modules/users/users/user.h index 511ee7195..65b2f526f 100644 --- a/modules/users/users/user.h +++ b/modules/users/users/user.h @@ -6,7 +6,7 @@ #include "core/resource.h" -//TODO remove _user_input postfixes +class UserModule; class User : public Resource { GDCLASS(User, Resource); @@ -52,6 +52,15 @@ public: bool get_locked() const; void set_locked(const bool val); + void add_module(const Ref &module); + Ref get_module(const int index); + Ref get_module_named(const String &name); + void remove_module(const int index); + int get_module_count(); + + Vector get_modules(); + void set_modules(const Vector &data); + bool check_password(const String &p_password); void create_password(const String &p_password); String hash_password(const String &p_password); @@ -84,6 +93,8 @@ protected: String _password_reset_token; bool _locked; + Vector> _modules; + RWLock _rw_lock; }; diff --git a/modules/users/users/user_module.cpp b/modules/users/users/user_module.cpp new file mode 100644 index 000000000..771ded39a --- /dev/null +++ b/modules/users/users/user_module.cpp @@ -0,0 +1,62 @@ +#include "user_module.h" +#include "core/class_db.h" + +#include "core/object.h" +#include "user.h" + +int UserModule::get_module_index() const { + return _module_id; +} +void UserModule::set_module_index(const int val) { + _module_id = val; +} + +String UserModule::get_module_name() const { + return _module_name; +} +void UserModule::set_module_name(const String &val) { + _module_name = val; +} + +Ref UserModule::get_user() { + return Ref(_user); +} +void UserModule::set_user(User *user) { + _user = user; +} + +void UserModule::read_lock() { + _rw_lock.read_lock(); +} +void UserModule::read_unlock() { + _rw_lock.read_unlock(); +} +void UserModule::write_lock() { + _rw_lock.write_lock(); +} +void UserModule::write_unlock() { + _rw_lock.write_unlock(); +} + +UserModule::UserModule() { + _module_id = -1; + _user = nullptr; +} + +UserModule::~UserModule() { +} + +void UserModule::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_module_index"), &UserModule::get_module_index); + + ClassDB::bind_method(D_METHOD("get_module_name"), &UserModule::get_module_name); + ClassDB::bind_method(D_METHOD("set_module_name", "val"), &UserModule::set_module_name); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "module_name"), "set_module_name", "get_module_name"); + + ClassDB::bind_method(D_METHOD("get_user"), &UserModule::get_user); + + ClassDB::bind_method(D_METHOD("read_lock"), &UserModule::read_lock); + ClassDB::bind_method(D_METHOD("read_unlock"), &UserModule::read_unlock); + ClassDB::bind_method(D_METHOD("write_lock"), &UserModule::write_lock); + ClassDB::bind_method(D_METHOD("write_unlock"), &UserModule::write_unlock); +} diff --git a/modules/users/users/user_module.h b/modules/users/users/user_module.h new file mode 100644 index 000000000..69c346f19 --- /dev/null +++ b/modules/users/users/user_module.h @@ -0,0 +1,43 @@ +#ifndef USER_MODULE_H +#define USER_MODULE_H + +#include "core/os/rw_lock.h" +#include "core/ustring.h" + +#include "core/resource.h" + +class User; + +class UserModule : public Resource { + GDCLASS(UserModule, Resource); + +public: + int get_module_index() const; + void set_module_index(const int val); + + String get_module_name() const; + void set_module_name(const String &val); + + Ref get_user(); + void set_user(User *user); + + void read_lock(); + void read_unlock(); + void write_lock(); + void write_unlock(); + + UserModule(); + ~UserModule(); + +protected: + static void _bind_methods(); + + int _module_id; + String _module_name; + + User *_user; + + RWLock _rw_lock; +}; + +#endif