diff --git a/crates/rtss_api/src/apis/draft_data.rs b/crates/rtss_api/src/apis/draft_data.rs index 921ec22..69758af 100644 --- a/crates/rtss_api/src/apis/draft_data.rs +++ b/crates/rtss_api/src/apis/draft_data.rs @@ -14,7 +14,7 @@ use super::common::{DataOptions, IscsDataOptions}; use super::release_data::ReleaseDataId; use crate::RtssDbLoader; -use crate::user_auth::{Role, RoleGuard, UserInfoDto}; +use crate::user_auth::{Role, RoleGuard, Token, UserAuthCache}; #[derive(Default)] pub struct DraftDataQuery; @@ -46,7 +46,10 @@ impl DraftDataQuery { paging: PageQueryDto, mut query: UserDraftDataFilterDto, ) -> async_graphql::Result> { - let user = ctx.data::()?; + let user = ctx + .data::()? + .query_user(&ctx.data::()?.0) + .await?; query.user_id = user.id_i32(); query.data_type = Some(DataType::Iscs); let db_accessor = ctx.data::()?; @@ -84,7 +87,10 @@ impl DraftDataQuery { ctx: &Context<'_>, name: String, ) -> async_graphql::Result { - let user = ctx.data::()?; + let user = ctx + .data::()? + .query_user(&ctx.data::()?.0) + .await?; let user_id = user.id_i32(); let db_accessor = ctx.data::()?; let exist = db_accessor.is_draft_data_exist(user_id, &name).await?; @@ -101,7 +107,10 @@ impl DraftDataMutation { ctx: &Context<'_>, mut input: CreateDraftDataDto, ) -> async_graphql::Result { - let user = ctx.data::()?; + let user = ctx + .data::()? + .query_user(&ctx.data::()?.0) + .await?; input = input.with_user_id(user.id_i32()); let db_accessor = ctx.data::()?; let draft_data = db_accessor.create_draft_data(input.into()).await?; @@ -180,7 +189,10 @@ impl DraftDataMutation { id: i32, name: String, ) -> async_graphql::Result { - let user = ctx.data::()?; + let user = ctx + .data::()? + .query_user(&ctx.data::()?.0) + .await?; let user_id = user.id_i32(); let db_accessor = ctx.data::()?; let draft_data = db_accessor.save_as_new_draft(id, &name, user_id).await?; diff --git a/crates/rtss_api/src/apis/release_data.rs b/crates/rtss_api/src/apis/release_data.rs index 32c53d6..7c8c458 100644 --- a/crates/rtss_api/src/apis/release_data.rs +++ b/crates/rtss_api/src/apis/release_data.rs @@ -17,7 +17,7 @@ use crate::RtssDbLoader; use super::common::{DataOptions, IscsDataOptions}; use super::{PageDto, PageQueryDto}; -use crate::user_auth::{Role, RoleGuard, UserInfoDto}; +use crate::user_auth::{Role, RoleGuard, Token, UserAuthCache}; #[derive(Default)] pub struct ReleaseDataQuery; @@ -126,7 +126,10 @@ impl ReleaseDataMutation { name: String, description: String, ) -> async_graphql::Result { - let user = ctx.data::()?; + let user = ctx + .data::()? + .query_user(&ctx.data::()?.0) + .await?; let user_id = user.id_i32(); let db_accessor = ctx.data::()?; let result = db_accessor @@ -143,7 +146,10 @@ impl ReleaseDataMutation { draft_id: i32, description: String, ) -> async_graphql::Result { - let user = ctx.data::()?; + let user = ctx + .data::()? + .query_user(&ctx.data::()?.0) + .await?; let user_id = user.id_i32(); let db_accessor = ctx.data::()?; let result = db_accessor @@ -202,7 +208,10 @@ impl ReleaseDataMutation { ctx: &Context<'_>, version_id: i32, ) -> async_graphql::Result { - let user = ctx.data::()?; + let user = ctx + .data::()? + .query_user(&ctx.data::()?.0) + .await?; let user_id = user.id_i32(); let db_accessor = ctx.data::()?; let result = db_accessor diff --git a/crates/rtss_api/src/user_auth/mod.rs b/crates/rtss_api/src/user_auth/mod.rs index 2ee3493..4e66601 100644 --- a/crates/rtss_api/src/user_auth/mod.rs +++ b/crates/rtss_api/src/user_auth/mod.rs @@ -1,5 +1,5 @@ use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, sync::{Arc, Mutex}, }; @@ -8,7 +8,7 @@ use axum::http::HeaderMap; use rtss_log::tracing::error; use serde::{Deserialize, Serialize}; -#[derive(Eq, PartialEq, Clone, Copy)] +#[derive(Eq, PartialEq, Clone, Copy, Hash)] pub enum Role { Admin, User, @@ -31,6 +31,9 @@ impl Guard for RoleGuard { let user_auth_cache = ctx.data::().unwrap(); let user_info = user_auth_cache.query_user(&token.0).await?; // 判断用户角色 + if user_info.roles().contains(&Role::Admin) { + return Ok(()); + } if user_info.roles().contains(&self.role) { return Ok(()); } @@ -155,7 +158,6 @@ pub struct UserInfoDto { pub nickname: Option, pub roles: Vec, } - impl UserInfoDto { pub fn id_i32(&self) -> i32 { self.id @@ -164,14 +166,19 @@ impl UserInfoDto { } pub fn roles(&self) -> Vec { - self.roles - .iter() - .filter_map(|role| match role.as_str() { - "04" | "05" => Some(Role::Admin), - "01" | "03" => Some(Role::User), - _ => None, - }) - .collect() + let mut unique_roles = HashSet::new(); + for role in &self.roles { + match role.as_str() { + "04" | "05" => { + unique_roles.insert(Role::Admin); + } + "01" | "03" => { + unique_roles.insert(Role::User); + } + _ => {} + } + } + unique_roles.into_iter().collect() } }