Implement webui import
This commit is contained in:
@ -104,6 +104,14 @@ fn get_new_uuid() -> String {
|
||||
|
||||
id
|
||||
}
|
||||
pub fn import_user(uid: i64) -> String {
|
||||
let token = get_new_uuid();
|
||||
lock_and_exec(
|
||||
"INSERT INTO users (cert, uuid, user_id) VALUES (?1, ?2, ?3)",
|
||||
params!("", token, uid)
|
||||
);
|
||||
token
|
||||
}
|
||||
fn update_cert(uid: i64, cert: &str) {
|
||||
lock_and_exec("UPDATE users SET cert=?1 WHERE user_id=?2", params!(cert, uid));
|
||||
}
|
||||
@ -187,7 +195,6 @@ fn decrypt_transfer_password(password: &str) -> String {
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn initialize(req: HttpRequest, body: String) -> HttpResponse {
|
||||
let body = json::parse(&body).unwrap();
|
||||
let token = create_acc(&body["token"].to_string());
|
||||
|
@ -434,6 +434,50 @@ pub fn webui_login(uid: i64, password: &str) -> Result<String, String> {
|
||||
Ok(new_token)
|
||||
}
|
||||
|
||||
pub fn webui_import_user(user: JsonValue) -> Result<JsonValue, String> {
|
||||
let mut user = user;
|
||||
create_webui_store();
|
||||
create_migration_store();
|
||||
create_token_store();
|
||||
let uid = user["userdata"]["user"]["id"].as_i64().unwrap();
|
||||
if acc_exists(uid) {
|
||||
return Err(String::from("User already exists"));
|
||||
}
|
||||
if user["missions"].is_empty() {
|
||||
user["missions"] = json::parse(include_str!("chat_missions.json")).unwrap();
|
||||
}
|
||||
if user["sif_cards"].is_empty() {
|
||||
user["sif_cards"] = array![];
|
||||
}
|
||||
|
||||
lock_and_exec("INSERT INTO users (user_id, userdata, userhome, missions, loginbonus, sifcards, friends) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", params!(
|
||||
uid,
|
||||
json::stringify(user["userdata"].clone()),
|
||||
json::stringify(user["home"].clone()),
|
||||
json::stringify(user["missions"].clone()),
|
||||
format!(r#"{{"last_rewarded": 0, "bonus_list": [], "start_time": {}}}"#, global::timestamp()),
|
||||
json::stringify(user["sif_cards"].clone()),
|
||||
r#"{"friend_user_id_list":[],"request_user_id_list":[],"pending_user_id_list":[]}"#
|
||||
));
|
||||
|
||||
let token;
|
||||
if !user["jp"].is_empty() {
|
||||
token = crate::router::gree::import_user(uid);
|
||||
} else {
|
||||
token = format!("{}", Uuid::new_v4());
|
||||
}
|
||||
|
||||
lock_and_exec("INSERT INTO tokens (user_id, token) VALUES (?1, ?2)", params!(uid, token));
|
||||
let mig = crate::router::user::uid_to_code(uid.to_string());
|
||||
|
||||
save_acc_transfer(&mig, &user["password"].to_string());
|
||||
|
||||
Ok(object!{
|
||||
uid: uid,
|
||||
migration_token: mig
|
||||
})
|
||||
}
|
||||
|
||||
pub fn webui_get_user(token: &str) -> Option<JsonValue> {
|
||||
let uid = lock_and_select("SELECT user_id FROM webui WHERE token=?1", params!(token)).unwrap_or(String::new());
|
||||
if uid == String::new() || token == "" {
|
||||
|
@ -41,6 +41,26 @@ pub fn login(_req: HttpRequest, body: String) -> HttpResponse {
|
||||
.body(json::stringify(resp))
|
||||
}
|
||||
|
||||
pub fn import(_req: HttpRequest, body: String) -> HttpResponse {
|
||||
let body = json::parse(&body).unwrap();
|
||||
|
||||
let result = userdata::webui_import_user(body);
|
||||
|
||||
if result.is_err() {
|
||||
return error(&result.unwrap_err());
|
||||
}
|
||||
let result = result.unwrap();
|
||||
|
||||
let resp = object!{
|
||||
result: "OK",
|
||||
uid: result["uid"].clone(),
|
||||
migration_token: result["migration_token"].clone()
|
||||
};
|
||||
HttpResponse::Ok()
|
||||
.insert_header(ContentType::json())
|
||||
.body(json::stringify(resp))
|
||||
}
|
||||
|
||||
pub fn user(req: HttpRequest) -> HttpResponse {
|
||||
let token = get_login_token(&req);
|
||||
if token.is_none() {
|
||||
@ -90,7 +110,7 @@ pub fn main(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
}
|
||||
}
|
||||
if req.path() != "/" && req.path() != "/home/" {
|
||||
if req.path() != "/" && req.path() != "/home/" && req.path() != "/import/" {
|
||||
return HttpResponse::Found()
|
||||
.insert_header(("Location", "/"))
|
||||
.body("");
|
||||
|
Reference in New Issue
Block a user