mirror of https://github.com/veypi/OneAuth.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.0 KiB
Rust
69 lines
2.0 KiB
Rust
|
2 years ago
|
//
|
||
|
|
// user.rs
|
||
|
|
// Copyright (C) 2023 veypi <i@veypi.com>
|
||
|
|
// 2023-10-02 21:00
|
||
|
|
// Distributed under terms of the MIT license.
|
||
|
|
//
|
||
|
|
|
||
|
|
use crate::{
|
||
|
|
models::{self, app, app_user, user, user_role},
|
||
|
|
Error, Result,
|
||
|
|
};
|
||
|
|
use sea_orm::{ActiveModelTrait, ConnectionTrait, DatabaseTransaction, EntityTrait};
|
||
|
|
|
||
|
|
// 尝试绑定应用
|
||
|
|
pub async fn connect_to_app(
|
||
|
|
u: user::Model,
|
||
|
|
aid: String,
|
||
|
|
db: &DatabaseTransaction,
|
||
|
|
) -> Result<app_user::Model> {
|
||
|
|
let app_obj: Option<app::Model> = app::Entity::find_by_id(&aid).one(db).await?;
|
||
|
|
let app_obj = match app_obj {
|
||
|
|
Some(o) => o,
|
||
|
|
None => return Err(Error::NotFound(aid.clone())),
|
||
|
|
};
|
||
|
|
let m = match app_obj.join_method.into() {
|
||
|
|
models::AppJoin::Disabled => return Err(Error::AppDisabledRegister),
|
||
|
|
models::AppJoin::Auto => models::AUStatus::OK,
|
||
|
|
models::AppJoin::Applying => models::AUStatus::Applying,
|
||
|
|
};
|
||
|
|
let au = app_user::ActiveModel {
|
||
|
|
app_id: sea_orm::ActiveValue::Set(aid.clone()),
|
||
|
|
user_id: sea_orm::ActiveValue::Set(u.id.clone()),
|
||
|
|
status: sea_orm::ActiveValue::Set(m.clone() as i32),
|
||
|
|
..Default::default()
|
||
|
|
};
|
||
|
|
let au = au.insert(db).await?;
|
||
|
|
if m == models::AUStatus::OK {
|
||
|
|
after_connected_to_app(u, app_obj, db).await?;
|
||
|
|
}
|
||
|
|
Ok(au)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 成功绑定应用后操作
|
||
|
|
pub async fn after_connected_to_app(
|
||
|
|
u: user::Model,
|
||
|
|
obj: app::Model,
|
||
|
|
db: &DatabaseTransaction,
|
||
|
|
) -> Result<()> {
|
||
|
|
if obj.role_id.is_some() {
|
||
|
|
user_role::ActiveModel {
|
||
|
|
user_id: sea_orm::ActiveValue::Set(u.id.clone()),
|
||
|
|
role_id: sea_orm::ActiveValue::Set(obj.role_id.unwrap().clone()),
|
||
|
|
..Default::default()
|
||
|
|
}
|
||
|
|
.insert(db)
|
||
|
|
.await?;
|
||
|
|
};
|
||
|
|
let sql = format!(
|
||
|
|
"update app set user_count = user_count + 1 where app_id = {}",
|
||
|
|
obj.id
|
||
|
|
);
|
||
|
|
db.execute(sea_orm::Statement::from_string(
|
||
|
|
sea_orm::DatabaseBackend::MySql,
|
||
|
|
sql,
|
||
|
|
))
|
||
|
|
.await?;
|
||
|
|
Ok(())
|
||
|
|
}
|