修改接口获取userInfo bug
All checks were successful
build / build-rust (push) Successful in 1m51s

This commit is contained in:
soul-walker 2024-09-23 17:56:10 +08:00
parent aae184e891
commit 255301509a
3 changed files with 48 additions and 20 deletions

View File

@ -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<IscsDataOptions>,
) -> async_graphql::Result<PageDto<DraftIscsDataDto>> {
let user = ctx.data::<UserInfoDto>()?;
let user = ctx
.data::<UserAuthCache>()?
.query_user(&ctx.data::<Token>()?.0)
.await?;
query.user_id = user.id_i32();
query.data_type = Some(DataType::Iscs);
let db_accessor = ctx.data::<RtssDbAccessor>()?;
@ -84,7 +87,10 @@ impl DraftDataQuery {
ctx: &Context<'_>,
name: String,
) -> async_graphql::Result<bool> {
let user = ctx.data::<UserInfoDto>()?;
let user = ctx
.data::<UserAuthCache>()?
.query_user(&ctx.data::<Token>()?.0)
.await?;
let user_id = user.id_i32();
let db_accessor = ctx.data::<RtssDbAccessor>()?;
let exist = db_accessor.is_draft_data_exist(user_id, &name).await?;
@ -101,7 +107,10 @@ impl DraftDataMutation {
ctx: &Context<'_>,
mut input: CreateDraftDataDto<IscsDataOptions>,
) -> async_graphql::Result<DraftIscsDataDto> {
let user = ctx.data::<UserInfoDto>()?;
let user = ctx
.data::<UserAuthCache>()?
.query_user(&ctx.data::<Token>()?.0)
.await?;
input = input.with_user_id(user.id_i32());
let db_accessor = ctx.data::<RtssDbAccessor>()?;
let draft_data = db_accessor.create_draft_data(input.into()).await?;
@ -180,7 +189,10 @@ impl DraftDataMutation {
id: i32,
name: String,
) -> async_graphql::Result<DraftDataDto> {
let user = ctx.data::<UserInfoDto>()?;
let user = ctx
.data::<UserAuthCache>()?
.query_user(&ctx.data::<Token>()?.0)
.await?;
let user_id = user.id_i32();
let db_accessor = ctx.data::<RtssDbAccessor>()?;
let draft_data = db_accessor.save_as_new_draft(id, &name, user_id).await?;

View File

@ -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<ReleaseDataWithUsedVersionDto> {
let user = ctx.data::<UserInfoDto>()?;
let user = ctx
.data::<UserAuthCache>()?
.query_user(&ctx.data::<Token>()?.0)
.await?;
let user_id = user.id_i32();
let db_accessor = ctx.data::<RtssDbAccessor>()?;
let result = db_accessor
@ -143,7 +146,10 @@ impl ReleaseDataMutation {
draft_id: i32,
description: String,
) -> async_graphql::Result<ReleaseDataWithUsedVersionDto> {
let user = ctx.data::<UserInfoDto>()?;
let user = ctx
.data::<UserAuthCache>()?
.query_user(&ctx.data::<Token>()?.0)
.await?;
let user_id = user.id_i32();
let db_accessor = ctx.data::<RtssDbAccessor>()?;
let result = db_accessor
@ -202,7 +208,10 @@ impl ReleaseDataMutation {
ctx: &Context<'_>,
version_id: i32,
) -> async_graphql::Result<DraftDataDto> {
let user = ctx.data::<UserInfoDto>()?;
let user = ctx
.data::<UserAuthCache>()?
.query_user(&ctx.data::<Token>()?.0)
.await?;
let user_id = user.id_i32();
let db_accessor = ctx.data::<RtssDbAccessor>()?;
let result = db_accessor

View File

@ -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::<UserAuthCache>().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<String>,
pub roles: Vec<String>,
}
impl UserInfoDto {
pub fn id_i32(&self) -> i32 {
self.id
@ -164,14 +166,19 @@ impl UserInfoDto {
}
pub fn roles(&self) -> Vec<Role> {
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()
}
}