Added a new UserModule class. It can be used to easily add data to Users.

This commit is contained in:
Relintai 2022-07-24 01:08:56 +02:00
parent ae36b35436
commit b479b6a1ab
7 changed files with 197 additions and 1 deletions

View File

@ -8,6 +8,7 @@ sources = [
"register_types.cpp",
"users/user.cpp",
"users/user_module.cpp",
"managers/user_manager.cpp",
"managers/user_manager_static.cpp",

View File

@ -11,6 +11,8 @@ def configure(env):
def get_doc_classes():
return [
"User",
"UserModule",
"UserManager",
"UserManagerStatic",
"UserManagerFile",

View File

@ -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<User>();
ClassDB::register_class<UserModule>();
ClassDB::register_class<UserManager>();
ClassDB::register_class<UserManagerStatic>();

View File

@ -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<UserModule> &module) {
_modules.push_back(module);
if (module.is_valid()) {
Ref<UserModule> m = module;
m->set_module_index(_modules.size() - 1);
m->set_user(this);
}
}
Ref<UserModule> User::get_module(const int index) {
ERR_FAIL_INDEX_V(index, _modules.size(), Ref<UserModule>());
return _modules[index];
}
Ref<UserModule> User::get_module_named(const String &name) {
for (int i = 0; i < _modules.size(); ++i) {
Ref<UserModule> m = _modules[i];
if (m.is_valid() && m->get_module_name() == name) {
return m;
}
}
return Ref<UserModule>();
}
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<Variant> User::get_modules() {
Vector<Variant> 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<Variant> &modules) {
_modules.clear();
for (int i = 0; i < modules.size(); i++) {
Ref<UserModule> um = Ref<UserModule>(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<UserModule> 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")));

View File

@ -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<UserModule> &module);
Ref<UserModule> get_module(const int index);
Ref<UserModule> get_module_named(const String &name);
void remove_module(const int index);
int get_module_count();
Vector<Variant> get_modules();
void set_modules(const Vector<Variant> &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<Ref<UserModule>> _modules;
RWLock _rw_lock;
};

View File

@ -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<User> UserModule::get_user() {
return Ref<User>(_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);
}

View File

@ -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<User> 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