|
|
@ -5,34 +5,36 @@
|
|
|
|
// Distributed under terms of the Apache license.
|
|
|
|
// Distributed under terms of the Apache license.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{dbtx, models, Error, Result, CONFIG, DB};
|
|
|
|
use std::fmt::{format, Debug};
|
|
|
|
use actix_web::{delete, get, head, post, web, Responder};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{models, Error, Result, CONFIG};
|
|
|
|
|
|
|
|
use actix_web::{delete, get, head, http, post, web, HttpResponse, Responder};
|
|
|
|
use base64;
|
|
|
|
use base64;
|
|
|
|
use rbatis::crud::CRUD;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tracing::info;
|
|
|
|
use tracing::info;
|
|
|
|
|
|
|
|
use uuid::uuid;
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/user/{id}")]
|
|
|
|
#[get("/user/{id}")]
|
|
|
|
pub async fn get(id: web::Path<String>) -> Result<models::User> {
|
|
|
|
pub async fn get(id: web::Path<String>) -> Result<models::User> {
|
|
|
|
let n = id.into_inner();
|
|
|
|
let n = id.into_inner();
|
|
|
|
if !n.is_empty() {
|
|
|
|
if !n.is_empty() {
|
|
|
|
let u: Option<models::User> = DB.fetch_by_column("id", &n).await?;
|
|
|
|
let s = sqlx::query_as::<_, models::User>("select *& from user where id = ?")
|
|
|
|
match u {
|
|
|
|
.bind(n)
|
|
|
|
Some(u) => {
|
|
|
|
.fetch_one(CONFIG.db())
|
|
|
|
info!("{:#?}", u.token());
|
|
|
|
.await?;
|
|
|
|
return Ok(u);
|
|
|
|
info!("{:#?}", s);
|
|
|
|
}
|
|
|
|
Ok(s)
|
|
|
|
None => Err(Error::NotFound(format!("user {}", n))),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Err(Error::Missing("id".to_string()))
|
|
|
|
Err(Error::Missing("id".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/user/")]
|
|
|
|
#[get("/user/")]
|
|
|
|
pub async fn list() -> impl Responder {
|
|
|
|
pub async fn list() -> Result<impl Responder> {
|
|
|
|
let result: Vec<models::User> = DB.fetch_list().await.unwrap();
|
|
|
|
let result = sqlx::query_as::<_, models::User>("select * from user")
|
|
|
|
web::Json(result)
|
|
|
|
.fetch_all(CONFIG.db())
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(web::Json(result))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
@ -42,18 +44,97 @@ pub struct LoginOpt {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[head("/user/{id}")]
|
|
|
|
#[head("/user/{id}")]
|
|
|
|
pub async fn login(q: web::Query<LoginOpt>, id: web::Path<String>) -> impl Responder {
|
|
|
|
pub async fn login(q: web::Query<LoginOpt>, id: web::Path<String>) -> Result<HttpResponse> {
|
|
|
|
let id = id.into_inner();
|
|
|
|
let id = id.into_inner();
|
|
|
|
let q = q.into_inner();
|
|
|
|
let q = q.into_inner();
|
|
|
|
info!("{} try to login{:#?}", id, q);
|
|
|
|
let typ = match q.typ {
|
|
|
|
let mut w = DB.new_wrapper();
|
|
|
|
Some(t) => match t.as_str() {
|
|
|
|
match q.typ {
|
|
|
|
"phone" => "phone",
|
|
|
|
_ => w = w.eq("username", id),
|
|
|
|
"email" => "email",
|
|
|
|
|
|
|
|
_ => "username",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => "username",
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let p = match base64::decode(q.password.as_bytes()) {
|
|
|
|
|
|
|
|
Err(_) => return Err(Error::ArgInvalid("password".to_string())),
|
|
|
|
|
|
|
|
Ok(p) => p,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let p = match std::str::from_utf8(&p) {
|
|
|
|
|
|
|
|
Ok(p) => p,
|
|
|
|
|
|
|
|
Err(_) => return Err(Error::ArgInvalid("password".to_string())),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let sql = format!("select * from user where {} = ?", typ);
|
|
|
|
|
|
|
|
let u = sqlx::query_as::<_, models::User>(&sql)
|
|
|
|
|
|
|
|
.bind(id)
|
|
|
|
|
|
|
|
.fetch_optional(CONFIG.db())
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let u = match u {
|
|
|
|
|
|
|
|
Some(u) => u,
|
|
|
|
|
|
|
|
None => return Err(Error::NotFound("user".to_string())),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u.check_pass(p)?;
|
|
|
|
|
|
|
|
let au = sqlx::query_as::<_, models::AppUser>(
|
|
|
|
|
|
|
|
"select * from app_user where app_id = ? and user_id = ?",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.bind(&CONFIG.uuid)
|
|
|
|
|
|
|
|
.bind(&u.id)
|
|
|
|
|
|
|
|
.fetch_optional(CONFIG.db())
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let i: i64 = match au {
|
|
|
|
|
|
|
|
Some(au) => match au.status {
|
|
|
|
|
|
|
|
models::AUStatus::OK => 0,
|
|
|
|
|
|
|
|
models::AUStatus::Deny => {
|
|
|
|
|
|
|
|
return Err(Error::BusinessException("apply denied".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let u: Option<models::User> = DB.fetch_by_wrapper(w).await.unwrap();
|
|
|
|
models::AUStatus::Disabled => {
|
|
|
|
|
|
|
|
return Err(Error::BusinessException("login disabled".to_string()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
models::AUStatus::Applying => {
|
|
|
|
|
|
|
|
return Err(Error::BusinessException("applying".to_string()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
None => {
|
|
|
|
|
|
|
|
let app = sqlx::query_as::<_, models::App>("select * from app where id = ?")
|
|
|
|
|
|
|
|
.bind(CONFIG.uuid.clone())
|
|
|
|
|
|
|
|
.fetch_one(CONFIG.db())
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
info!("{:#?}", u);
|
|
|
|
info!("{:#?}", u);
|
|
|
|
|
|
|
|
let s = match app.join_method {
|
|
|
|
""
|
|
|
|
models::app::AppJoin::Disabled => {
|
|
|
|
|
|
|
|
return Err(Error::BusinessException(
|
|
|
|
|
|
|
|
"this app diabled login".to_string(),
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
models::app::AppJoin::Auto => models::AUStatus::OK,
|
|
|
|
|
|
|
|
models::app::AppJoin::Applying => models::AUStatus::Applying,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
sqlx::query(
|
|
|
|
|
|
|
|
r#"
|
|
|
|
|
|
|
|
insert into app_user (app_id,user_id,status)
|
|
|
|
|
|
|
|
values ( ?, ?, ? )
|
|
|
|
|
|
|
|
"#,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.bind(&app.id)
|
|
|
|
|
|
|
|
.bind(&u.id)
|
|
|
|
|
|
|
|
.bind(&s)
|
|
|
|
|
|
|
|
.execute(CONFIG.db())
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
match s {
|
|
|
|
|
|
|
|
models::AUStatus::OK => 0,
|
|
|
|
|
|
|
|
_ => 1,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
|
|
|
|
Ok(HttpResponse::build(http::StatusCode::OK)
|
|
|
|
|
|
|
|
.insert_header(("auth_token", u.token().to_string()?))
|
|
|
|
|
|
|
|
.body("".to_string()))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Ok(HttpResponse::build(http::StatusCode::OK)
|
|
|
|
|
|
|
|
.insert_header(("data", "applying"))
|
|
|
|
|
|
|
|
.body("".to_string()))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
@ -67,7 +148,11 @@ pub async fn register(q: web::Json<RegisterOpt>) -> Result<String> {
|
|
|
|
let q = q.into_inner();
|
|
|
|
let q = q.into_inner();
|
|
|
|
// let mut tx = dbtx().await;
|
|
|
|
// let mut tx = dbtx().await;
|
|
|
|
println!("{:#?}", q);
|
|
|
|
println!("{:#?}", q);
|
|
|
|
let u: Option<models::User> = DB.fetch_by_column("username", &q.username).await.unwrap();
|
|
|
|
let u: Option<models::User> =
|
|
|
|
|
|
|
|
sqlx::query_as::<_, models::User>("select * from user where username = ?")
|
|
|
|
|
|
|
|
.bind(q.username.clone())
|
|
|
|
|
|
|
|
.fetch_optional(CONFIG.db())
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
let u: models::User = match u {
|
|
|
|
let u: models::User = match u {
|
|
|
|
Some(_) => return Err(Error::ArgDuplicated(format!("username: {}", q.username))),
|
|
|
|
Some(_) => return Err(Error::ArgDuplicated(format!("username: {}", q.username))),
|
|
|
|
None => {
|
|
|
|
None => {
|
|
|
@ -86,8 +171,11 @@ pub async fn register(q: web::Json<RegisterOpt>) -> Result<String> {
|
|
|
|
u
|
|
|
|
u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let oa: Option<models::App> = DB.fetch_by_column("id", CONFIG.uuid.clone()).await?;
|
|
|
|
let oa: models::App = sqlx::query_as::<_, models::App>("select * from app where id = ?")
|
|
|
|
let oa = oa.unwrap();
|
|
|
|
.bind(CONFIG.uuid.clone())
|
|
|
|
|
|
|
|
.fetch_one(CONFIG.db())
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut au = models::AppUser::new();
|
|
|
|
let mut au = models::AppUser::new();
|
|
|
|
au.app_id = oa.id;
|
|
|
|
au.app_id = oa.id;
|
|
|
|
au.user_id = u.id.clone();
|
|
|
|
au.user_id = u.id.clone();
|
|
|
@ -96,8 +184,31 @@ pub async fn register(q: web::Json<RegisterOpt>) -> Result<String> {
|
|
|
|
models::app::AppJoin::Auto => au.status = models::app::AUStatus::OK,
|
|
|
|
models::app::AppJoin::Auto => au.status = models::app::AUStatus::OK,
|
|
|
|
models::app::AppJoin::Applying => au.status = models::app::AUStatus::Applying,
|
|
|
|
models::app::AppJoin::Applying => au.status = models::app::AUStatus::Applying,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB.save(&u, &[]).await?;
|
|
|
|
let mut c = CONFIG.db().begin().await?;
|
|
|
|
DB.save(&au, &[]).await?;
|
|
|
|
sqlx::query!(
|
|
|
|
|
|
|
|
r#"
|
|
|
|
|
|
|
|
insert into user (id,username,real_code,check_code)
|
|
|
|
|
|
|
|
values ( ?, ?, ?, ?)
|
|
|
|
|
|
|
|
"#,
|
|
|
|
|
|
|
|
u.id,
|
|
|
|
|
|
|
|
u.username,
|
|
|
|
|
|
|
|
u.real_code,
|
|
|
|
|
|
|
|
u.check_code,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.execute(&mut c)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
|
|
|
|
r#"
|
|
|
|
|
|
|
|
insert into app_user ( app_id, user_id, status)
|
|
|
|
|
|
|
|
values (?, ?, ? )
|
|
|
|
|
|
|
|
"#,
|
|
|
|
|
|
|
|
au.app_id,
|
|
|
|
|
|
|
|
au.user_id,
|
|
|
|
|
|
|
|
au.status,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.execute(&mut c)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
c.commit().await?;
|
|
|
|
Ok("ok".to_string())
|
|
|
|
Ok("ok".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|