|
|
@ -9,7 +9,7 @@ use std::fmt::Debug;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
|
models::{self, access, app, app_user, user, UserPlugin},
|
|
|
|
models::{self, access, app, app_user, user, UserPlugin},
|
|
|
|
AppState, Error, Result, CONFIG,
|
|
|
|
AppState, Error, Result,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use actix_web::{delete, get, head, http, post, web, HttpResponse, Responder};
|
|
|
|
use actix_web::{delete, get, head, http, post, web, HttpResponse, Responder};
|
|
|
|
use base64;
|
|
|
|
use base64;
|
|
|
@ -21,9 +21,9 @@ use tracing::info;
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/user/{id}")]
|
|
|
|
#[get("/user/{id}")]
|
|
|
|
#[access_read("user", id = "&id.clone()")]
|
|
|
|
#[access_read("user", id = "&id.clone()")]
|
|
|
|
pub async fn get(id: web::Path<String>, data: web::Data<AppState>) -> Result<impl Responder> {
|
|
|
|
pub async fn get(id: web::Path<String>, stat: web::Data<AppState>) -> Result<impl Responder> {
|
|
|
|
let n = id.into_inner();
|
|
|
|
let n = id.into_inner();
|
|
|
|
let db = &data.db;
|
|
|
|
let db = stat.db();
|
|
|
|
if !n.is_empty() {
|
|
|
|
if !n.is_empty() {
|
|
|
|
let d: Option<models::entity::user::Model> =
|
|
|
|
let d: Option<models::entity::user::Model> =
|
|
|
|
models::entity::user::Entity::find_by_id(n).one(db).await?;
|
|
|
|
models::entity::user::Entity::find_by_id(n).one(db).await?;
|
|
|
@ -35,26 +35,27 @@ pub async fn get(id: web::Path<String>, data: web::Data<AppState>) -> Result<imp
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/user/")]
|
|
|
|
#[get("/user/")]
|
|
|
|
#[access_read("user")]
|
|
|
|
#[access_read("user")]
|
|
|
|
pub async fn list() -> Result<impl Responder> {
|
|
|
|
pub async fn list(stat: web::Data<AppState>) -> Result<impl Responder> {
|
|
|
|
let result = sqlx::query!(
|
|
|
|
let res: Vec<user::Model> = user::Entity::find().all(stat.db()).await?;
|
|
|
|
"select id,updated,created,username,nickname,email,icon,status, used, space from user",
|
|
|
|
// let result = sqlx::query!(
|
|
|
|
)
|
|
|
|
// "select id,updated,created,username,nickname,email,icon,status, used, space from user",
|
|
|
|
.map(|row| models::user::Model {
|
|
|
|
// )
|
|
|
|
id: row.id,
|
|
|
|
// .map(|row| models::user::Model {
|
|
|
|
created: row.created,
|
|
|
|
// id: row.id,
|
|
|
|
updated: row.updated,
|
|
|
|
// created: row.created,
|
|
|
|
username: row.username,
|
|
|
|
// updated: row.updated,
|
|
|
|
nickname: row.nickname,
|
|
|
|
// username: row.username,
|
|
|
|
email: row.email,
|
|
|
|
// nickname: row.nickname,
|
|
|
|
status: row.status,
|
|
|
|
// email: row.email,
|
|
|
|
used: row.used,
|
|
|
|
// status: row.status,
|
|
|
|
space: row.space,
|
|
|
|
// used: row.used,
|
|
|
|
icon: row.icon,
|
|
|
|
// space: row.space,
|
|
|
|
..Default::default()
|
|
|
|
// icon: row.icon,
|
|
|
|
})
|
|
|
|
// ..Default::default()
|
|
|
|
.fetch_all(CONFIG.sqlx())
|
|
|
|
// })
|
|
|
|
.await?;
|
|
|
|
// .fetch_all(CONFIG.sqlx())
|
|
|
|
Ok(web::Json(result))
|
|
|
|
// .await?;
|
|
|
|
|
|
|
|
Ok(web::Json(res))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
@ -67,9 +68,9 @@ pub struct LoginOpt {
|
|
|
|
pub async fn login(
|
|
|
|
pub async fn login(
|
|
|
|
q: web::Query<LoginOpt>,
|
|
|
|
q: web::Query<LoginOpt>,
|
|
|
|
id: web::Path<String>,
|
|
|
|
id: web::Path<String>,
|
|
|
|
data: web::Data<AppState>,
|
|
|
|
stat: web::Data<AppState>,
|
|
|
|
) -> Result<HttpResponse> {
|
|
|
|
) -> Result<HttpResponse> {
|
|
|
|
let db = &data.db;
|
|
|
|
let db = stat.db();
|
|
|
|
let id = id.into_inner();
|
|
|
|
let id = id.into_inner();
|
|
|
|
let q = q.into_inner();
|
|
|
|
let q = q.into_inner();
|
|
|
|
let filter = match q.typ {
|
|
|
|
let filter = match q.typ {
|
|
|
@ -96,7 +97,7 @@ pub async fn login(
|
|
|
|
|
|
|
|
|
|
|
|
u.check_pass(p)?;
|
|
|
|
u.check_pass(p)?;
|
|
|
|
let au: Option<app_user::Model> = app_user::Entity::find()
|
|
|
|
let au: Option<app_user::Model> = app_user::Entity::find()
|
|
|
|
.filter(app_user::Column::AppId.eq(&CONFIG.uuid))
|
|
|
|
.filter(app_user::Column::AppId.eq(&stat.uuid))
|
|
|
|
.filter(app_user::Column::UserId.eq(&u.id))
|
|
|
|
.filter(app_user::Column::UserId.eq(&u.id))
|
|
|
|
.one(db)
|
|
|
|
.one(db)
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
@ -121,7 +122,7 @@ pub async fn login(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
None => {
|
|
|
|
let app_obj: app::Model = app::Entity::find_by_id(CONFIG.uuid.clone())
|
|
|
|
let app_obj: app::Model = app::Entity::find_by_id(stat.uuid.clone())
|
|
|
|
.one(db)
|
|
|
|
.one(db)
|
|
|
|
.await?
|
|
|
|
.await?
|
|
|
|
.unwrap();
|
|
|
|
.unwrap();
|
|
|
@ -143,7 +144,7 @@ values ( ?, ?, ? )
|
|
|
|
.bind(&app_obj.id)
|
|
|
|
.bind(&app_obj.id)
|
|
|
|
.bind(&u.id)
|
|
|
|
.bind(&u.id)
|
|
|
|
.bind(&s)
|
|
|
|
.bind(&s)
|
|
|
|
.execute(CONFIG.sqlx())
|
|
|
|
.execute(stat.sqlx())
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
|
match s {
|
|
|
|
match s {
|
|
|
|
models::AUStatus::OK => 0,
|
|
|
|
models::AUStatus::OK => 0,
|
|
|
@ -157,15 +158,15 @@ values ( ?, ?, ? )
|
|
|
|
"select access.name,access.rid,access.level from access, user_role, role WHERE user_role.user_id = ? && access.role_id=user_role.role_id && role.id=user_role.role_id && role.app_id = ?",
|
|
|
|
"select access.name,access.rid,access.level from access, user_role, role WHERE user_role.user_id = ? && access.role_id=user_role.role_id && role.id=user_role.role_id && role.app_id = ?",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.bind(&u.id)
|
|
|
|
.bind(&u.id)
|
|
|
|
.bind(CONFIG.uuid.clone())
|
|
|
|
.bind(stat.uuid.clone())
|
|
|
|
.fetch_all(CONFIG.sqlx())
|
|
|
|
.fetch_all(stat.sqlx())
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
|
Ok(HttpResponse::build(http::StatusCode::OK)
|
|
|
|
Ok(HttpResponse::build(http::StatusCode::OK)
|
|
|
|
.insert_header(("auth_token", u.token(result).to_string()?))
|
|
|
|
.insert_header(("auth_token", u.token(result).to_string()?))
|
|
|
|
.body("".to_string()))
|
|
|
|
.body("".to_string()))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Ok(HttpResponse::build(http::StatusCode::OK)
|
|
|
|
Ok(HttpResponse::build(http::StatusCode::OK)
|
|
|
|
.insert_header(("data", "applying"))
|
|
|
|
.insert_header(("stat", "applying"))
|
|
|
|
.body("".to_string()))
|
|
|
|
.body("".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -177,14 +178,14 @@ pub struct RegisterOpt {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[post("/user/")]
|
|
|
|
#[post("/user/")]
|
|
|
|
pub async fn register(q: web::Json<RegisterOpt>) -> Result<String> {
|
|
|
|
pub async fn register(q: web::Json<RegisterOpt>, stat: web::Data<AppState>) -> Result<String> {
|
|
|
|
let q = q.into_inner();
|
|
|
|
let q = q.into_inner();
|
|
|
|
// let mut tx = dbtx().await;
|
|
|
|
// let mut tx = dbtx().await;
|
|
|
|
info!("{:#?}", q);
|
|
|
|
info!("{:#?}", q);
|
|
|
|
let u: Option<models::user::Model> =
|
|
|
|
let u: Option<models::user::Model> =
|
|
|
|
sqlx::query_as::<_, models::user::Model>("select * from user where username = ?")
|
|
|
|
sqlx::query_as::<_, models::user::Model>("select * from user where username = ?")
|
|
|
|
.bind(q.username.clone())
|
|
|
|
.bind(q.username.clone())
|
|
|
|
.fetch_optional(CONFIG.sqlx())
|
|
|
|
.fetch_optional(stat.sqlx())
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
|
let u: models::user::Model = match u {
|
|
|
|
let u: models::user::Model = match u {
|
|
|
|
Some(_) => return Err(Error::ArgDuplicated(format!("username: {}", q.username))),
|
|
|
|
Some(_) => return Err(Error::ArgDuplicated(format!("username: {}", q.username))),
|
|
|
@ -209,8 +210,8 @@ pub async fn register(q: web::Json<RegisterOpt>) -> Result<String> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let oa: app::Model = sqlx::query_as::<_, app::Model>("select * from app where id = ?")
|
|
|
|
let oa: app::Model = sqlx::query_as::<_, app::Model>("select * from app where id = ?")
|
|
|
|
.bind(CONFIG.uuid.clone())
|
|
|
|
.bind(stat.uuid.clone())
|
|
|
|
.fetch_one(CONFIG.sqlx())
|
|
|
|
.fetch_one(stat.sqlx())
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut au = app_user::Model::default();
|
|
|
|
let mut au = app_user::Model::default();
|
|
|
@ -223,7 +224,7 @@ pub async fn register(q: web::Json<RegisterOpt>) -> Result<String> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
models::AppJoin::Applying => au.status = models::AUStatus::Applying as i32,
|
|
|
|
models::AppJoin::Applying => au.status = models::AUStatus::Applying as i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut c = CONFIG.sqlx().begin().await?;
|
|
|
|
let mut c = stat.sqlx().begin().await?;
|
|
|
|
// 创建用户
|
|
|
|
// 创建用户
|
|
|
|
sqlx::query!(
|
|
|
|
sqlx::query!(
|
|
|
|
r#"
|
|
|
|
r#"
|
|
|
|