2022-06-30 13:23:14 +02:00
|
|
|
#include "user.h"
|
2022-08-17 13:45:14 +02:00
|
|
|
#include "core/object/class_db.h"
|
2022-07-24 01:42:55 +02:00
|
|
|
#include "core/io/json.h"
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-24 01:08:56 +02:00
|
|
|
#include "user_module.h"
|
|
|
|
|
2022-07-22 10:00:07 +02:00
|
|
|
int User::get_user_id() const {
|
|
|
|
return _user_id;
|
2022-07-07 17:06:00 +02:00
|
|
|
}
|
2022-07-22 10:00:07 +02:00
|
|
|
void User::set_user_id(const int val) {
|
|
|
|
_user_id = val;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 10:00:07 +02:00
|
|
|
String User::get_user_name() const {
|
|
|
|
return _user_name;
|
2022-07-07 17:06:00 +02:00
|
|
|
}
|
2022-07-22 10:00:07 +02:00
|
|
|
void User::set_user_name(const String &val) {
|
|
|
|
_user_name = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
String User::get_email() const {
|
|
|
|
return _email;
|
|
|
|
}
|
|
|
|
void User::set_email(const String &val) {
|
|
|
|
_email = val;
|
2022-07-07 17:06:00 +02:00
|
|
|
}
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-08 01:30:19 +02:00
|
|
|
int User::get_rank() const {
|
2022-07-07 17:06:00 +02:00
|
|
|
return _rank;
|
|
|
|
}
|
2022-07-22 10:00:07 +02:00
|
|
|
void User::set_rank(const int val) {
|
2022-07-07 17:06:00 +02:00
|
|
|
_rank = val;
|
|
|
|
}
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-08 01:30:19 +02:00
|
|
|
String User::get_pre_salt() const {
|
2022-07-07 17:06:00 +02:00
|
|
|
return _pre_salt;
|
|
|
|
}
|
|
|
|
void User::set_pre_salt(const String &val) {
|
|
|
|
_pre_salt = val;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 01:30:19 +02:00
|
|
|
String User::get_post_salt() const {
|
2022-07-07 17:06:00 +02:00
|
|
|
return _post_salt;
|
|
|
|
}
|
|
|
|
void User::set_post_salt(const String &val) {
|
|
|
|
_post_salt = val;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 01:30:19 +02:00
|
|
|
String User::get_password_hash() const {
|
2022-07-07 17:06:00 +02:00
|
|
|
return _password_hash;
|
|
|
|
}
|
|
|
|
void User::set_password_hash(const String &val) {
|
|
|
|
_password_hash = val;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 01:30:19 +02:00
|
|
|
bool User::get_banned() const {
|
2022-07-07 17:06:00 +02:00
|
|
|
return _banned;
|
|
|
|
}
|
2022-07-22 10:00:07 +02:00
|
|
|
void User::set_banned(const bool val) {
|
2022-07-07 17:06:00 +02:00
|
|
|
_banned = val;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 01:30:19 +02:00
|
|
|
String User::get_password_reset_token() const {
|
2022-07-07 17:06:00 +02:00
|
|
|
return _password_reset_token;
|
|
|
|
}
|
|
|
|
void User::set_password_reset_token(const String &val) {
|
|
|
|
_password_reset_token = val;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 01:30:19 +02:00
|
|
|
bool User::get_locked() const {
|
2022-07-07 17:06:00 +02:00
|
|
|
return _locked;
|
|
|
|
}
|
2022-07-22 10:00:07 +02:00
|
|
|
void User::set_locked(const bool val) {
|
2022-07-07 17:06:00 +02:00
|
|
|
_locked = val;
|
|
|
|
}
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-24 01:08:56 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 17:43:35 +02:00
|
|
|
bool User::check_password(const String &p_password) {
|
|
|
|
return call("_check_password", p_password);
|
2022-07-08 10:00:54 +02:00
|
|
|
}
|
2022-07-21 17:43:35 +02:00
|
|
|
void User::create_password(const String &p_password) {
|
|
|
|
call("_create_password", p_password);
|
2022-07-08 10:00:54 +02:00
|
|
|
}
|
2022-07-21 17:43:35 +02:00
|
|
|
String User::hash_password(const String &p_password) {
|
|
|
|
return call("_hash_password", p_password);
|
2022-07-08 10:00:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 17:43:35 +02:00
|
|
|
bool User::_check_password(const String &p_password) {
|
|
|
|
return hash_password(p_password) == get_password_hash();
|
2022-07-08 10:00:54 +02:00
|
|
|
}
|
2022-07-22 10:00:07 +02:00
|
|
|
|
2022-07-21 17:43:35 +02:00
|
|
|
void User::_create_password(const String &p_password) {
|
2022-07-08 10:00:54 +02:00
|
|
|
// todo improve a bit
|
2022-07-22 10:00:07 +02:00
|
|
|
set_pre_salt(hash_password(get_user_name() + get_email()));
|
|
|
|
set_post_salt(hash_password(get_email() + get_user_name()));
|
2022-07-08 10:00:54 +02:00
|
|
|
|
2022-07-21 17:43:35 +02:00
|
|
|
set_password_hash(hash_password(p_password));
|
2022-07-08 10:00:54 +02:00
|
|
|
}
|
2022-07-21 17:43:35 +02:00
|
|
|
String User::_hash_password(const String &p_password) {
|
|
|
|
String p = get_pre_salt() + p_password + get_post_salt();
|
2022-07-08 10:00:54 +02:00
|
|
|
return p.sha256_text();
|
|
|
|
}
|
|
|
|
|
2022-07-24 01:42:55 +02:00
|
|
|
Dictionary User::to_dict() {
|
|
|
|
return call("_to_dict");
|
|
|
|
}
|
|
|
|
void User::from_dict(const Dictionary &dict) {
|
|
|
|
call("_from_dict", dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary User::_to_dict() {
|
|
|
|
Dictionary dict;
|
|
|
|
|
|
|
|
dict["user_id"] = _user_id;
|
|
|
|
dict["user_name"] = _user_name;
|
|
|
|
dict["email"] = _email;
|
|
|
|
dict["rank"] = _rank;
|
|
|
|
dict["pre_salt"] = _pre_salt;
|
|
|
|
dict["post_salt"] = _post_salt;
|
|
|
|
dict["password_hash"] = _password_hash;
|
|
|
|
dict["banned"] = _banned;
|
|
|
|
dict["password_reset_token"] = _password_reset_token;
|
|
|
|
dict["locked"] = _locked;
|
|
|
|
|
|
|
|
Array marr;
|
|
|
|
|
|
|
|
for (int i = 0; i < _modules.size(); ++i) {
|
|
|
|
Ref<UserModule> m = _modules[i];
|
|
|
|
|
|
|
|
if (m.is_valid()) {
|
|
|
|
Dictionary mdict;
|
|
|
|
|
|
|
|
mdict["index"] = i;
|
|
|
|
mdict["data"] = m->to_dict();
|
|
|
|
|
|
|
|
marr.push_back(mdict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dict["modules"] = marr;
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
void User::_from_dict(const Dictionary &dict) {
|
|
|
|
_user_id = dict["user_id "];
|
|
|
|
_user_name = dict["user_name "];
|
|
|
|
_email = dict["email "];
|
|
|
|
_rank = dict["rank "];
|
|
|
|
_pre_salt = dict["pre_salt "];
|
|
|
|
_post_salt = dict["post_salt "];
|
|
|
|
_password_hash = dict["password_hash "];
|
|
|
|
_banned = dict["banned "];
|
|
|
|
_password_reset_token = dict["password_reset_token "];
|
|
|
|
_locked = dict["locked "];
|
|
|
|
|
|
|
|
Array marr = dict["modules"];
|
|
|
|
|
|
|
|
for (int i = 0; i < marr.size(); ++i) {
|
|
|
|
Dictionary mdict = marr[i];
|
|
|
|
|
|
|
|
if (!mdict.has("index") || !mdict.has("data")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = mdict["index"];
|
|
|
|
|
|
|
|
Ref<UserModule> m = _modules[index];
|
|
|
|
|
|
|
|
ERR_CONTINUE(!m.is_valid());
|
|
|
|
|
|
|
|
m->from_dict(mdict["data"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String User::to_json() {
|
|
|
|
return JSON::print(to_dict());
|
|
|
|
}
|
|
|
|
void User::from_json(const String &data) {
|
|
|
|
Error err;
|
|
|
|
String err_txt;
|
|
|
|
int err_line;
|
|
|
|
Variant v;
|
|
|
|
err = JSON::parse(data, v, err_txt, err_line);
|
|
|
|
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
|
|
|
|
Dictionary d = v;
|
|
|
|
|
|
|
|
from_dict(d);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:08:52 +02:00
|
|
|
void User::save() {
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:19:40 +02:00
|
|
|
void User::read_lock() {
|
|
|
|
_rw_lock.read_lock();
|
|
|
|
}
|
|
|
|
void User::read_unlock() {
|
|
|
|
_rw_lock.read_unlock();
|
|
|
|
}
|
|
|
|
void User::write_lock() {
|
|
|
|
_rw_lock.write_lock();
|
|
|
|
}
|
|
|
|
void User::write_unlock() {
|
|
|
|
_rw_lock.write_unlock();
|
|
|
|
}
|
|
|
|
|
2022-07-07 17:06:00 +02:00
|
|
|
User::User() {
|
2022-07-22 10:00:07 +02:00
|
|
|
_user_id = -1;
|
2022-07-07 17:06:00 +02:00
|
|
|
_rank = 0;
|
|
|
|
_banned = false;
|
|
|
|
_locked = false;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
User::~User() {
|
2022-07-24 01:08:56 +02:00
|
|
|
for (int i = 0; i < _modules.size(); ++i) {
|
|
|
|
Ref<UserModule> um = _modules[i];
|
|
|
|
|
|
|
|
if (um.is_valid()) {
|
|
|
|
um->set_user(nullptr);
|
|
|
|
}
|
|
|
|
}
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
2022-07-07 17:06:00 +02:00
|
|
|
|
|
|
|
void User::_bind_methods() {
|
2022-07-22 10:00:07 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_user_id"), &User::get_user_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_user_id", "val"), &User::set_user_id);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "user_id"), "set_user_id", "get_user_id");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_user_name"), &User::get_user_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_user_name", "val"), &User::set_user_name);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "user_name"), "set_user_name", "get_user_name");
|
2022-07-07 17:06:00 +02:00
|
|
|
|
2022-07-22 10:00:07 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_email"), &User::get_email);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_email", "val"), &User::set_email);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "email"), "set_email", "get_email");
|
2022-07-07 17:06:00 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rank"), &User::get_rank);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rank", "val"), &User::set_rank);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "rank"), "set_rank", "get_rank");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pre_salt"), &User::get_pre_salt);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_pre_salt", "val"), &User::set_pre_salt);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "pre_salt"), "set_pre_salt", "get_pre_salt");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_post_salt"), &User::get_post_salt);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_post_salt", "val"), &User::set_post_salt);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "post_salt"), "set_post_salt", "get_post_salt");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_password_hash"), &User::get_password_hash);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_password_hash", "val"), &User::set_password_hash);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "password_hash"), "set_password_hash", "get_password_hash");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_banned"), &User::get_banned);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_banned", "val"), &User::set_banned);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "banned"), "set_banned", "get_banned");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_password_reset_token"), &User::get_password_reset_token);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_password_reset_token", "val"), &User::set_password_reset_token);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "password_reset_token"), "set_password_reset_token", "get_password_reset_token");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_locked"), &User::get_locked);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_locked", "val"), &User::set_locked);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "locked"), "set_locked", "get_locked");
|
|
|
|
|
2022-07-24 01:08:56 +02:00
|
|
|
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);
|
2023-06-10 20:58:49 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "modules", PROPERTY_HINT_NONE, "23/20:UserModule", PROPERTY_USAGE_DEFAULT, "UserModule"), "set_modules", "get_modules");
|
2022-07-24 01:08:56 +02:00
|
|
|
|
2022-07-21 17:43:35 +02:00
|
|
|
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")));
|
2022-07-08 10:00:54 +02:00
|
|
|
|
2022-07-21 17:43:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("check_password", "password"), &User::check_password);
|
|
|
|
ClassDB::bind_method(D_METHOD("create_password", "password"), &User::create_password);
|
|
|
|
ClassDB::bind_method(D_METHOD("hash_password", "password"), &User::hash_password);
|
2022-07-08 10:00:54 +02:00
|
|
|
|
2022-07-21 17:43:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_check_password", "password"), &User::_check_password);
|
|
|
|
ClassDB::bind_method(D_METHOD("_create_password", "password"), &User::_create_password);
|
|
|
|
ClassDB::bind_method(D_METHOD("_hash_password", "password"), &User::_hash_password);
|
2022-07-08 10:00:54 +02:00
|
|
|
|
2022-07-24 01:42:55 +02:00
|
|
|
BIND_VMETHOD(MethodInfo("_from_dict", PropertyInfo(Variant::DICTIONARY, "dict")));
|
|
|
|
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::DICTIONARY, "dict"), "_to_dict"));
|
|
|
|
ClassDB::bind_method(D_METHOD("from_dict", "dict"), &User::from_dict);
|
|
|
|
ClassDB::bind_method(D_METHOD("to_dict"), &User::to_dict);
|
|
|
|
ClassDB::bind_method(D_METHOD("_from_dict", "dict"), &User::_from_dict);
|
|
|
|
ClassDB::bind_method(D_METHOD("_to_dict"), &User::_to_dict);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("to_json"), &User::to_json);
|
|
|
|
ClassDB::bind_method(D_METHOD("from_json", "data"), &User::from_json);
|
|
|
|
|
2022-07-21 18:08:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("save"), &User::save);
|
|
|
|
|
2022-07-21 18:19:40 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("read_lock"), &User::read_lock);
|
|
|
|
ClassDB::bind_method(D_METHOD("read_unlock"), &User::read_unlock);
|
|
|
|
ClassDB::bind_method(D_METHOD("write_lock"), &User::write_lock);
|
|
|
|
ClassDB::bind_method(D_METHOD("write_unlock"), &User::write_unlock);
|
|
|
|
|
2022-07-07 17:06:00 +02:00
|
|
|
BIND_ENUM_CONSTANT(PERMISSION_CREATE);
|
|
|
|
BIND_ENUM_CONSTANT(PERMISSION_READ);
|
|
|
|
BIND_ENUM_CONSTANT(PERMISSION_UPDATE);
|
|
|
|
BIND_ENUM_CONSTANT(PERMISSION_DELETE);
|
|
|
|
BIND_ENUM_CONSTANT(PERMISSION_ALL);
|
|
|
|
BIND_ENUM_CONSTANT(PERMISSION_NONE);
|
|
|
|
}
|