This commit is contained in:
parent
aae184e891
commit
255301509a
@ -14,7 +14,7 @@ use super::common::{DataOptions, IscsDataOptions};
|
|||||||
use super::release_data::ReleaseDataId;
|
use super::release_data::ReleaseDataId;
|
||||||
use crate::RtssDbLoader;
|
use crate::RtssDbLoader;
|
||||||
|
|
||||||
use crate::user_auth::{Role, RoleGuard, UserInfoDto};
|
use crate::user_auth::{Role, RoleGuard, Token, UserAuthCache};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DraftDataQuery;
|
pub struct DraftDataQuery;
|
||||||
@ -46,7 +46,10 @@ impl DraftDataQuery {
|
|||||||
paging: PageQueryDto,
|
paging: PageQueryDto,
|
||||||
mut query: UserDraftDataFilterDto<IscsDataOptions>,
|
mut query: UserDraftDataFilterDto<IscsDataOptions>,
|
||||||
) -> async_graphql::Result<PageDto<DraftIscsDataDto>> {
|
) -> 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.user_id = user.id_i32();
|
||||||
query.data_type = Some(DataType::Iscs);
|
query.data_type = Some(DataType::Iscs);
|
||||||
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
||||||
@ -84,7 +87,10 @@ impl DraftDataQuery {
|
|||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
name: String,
|
name: String,
|
||||||
) -> async_graphql::Result<bool> {
|
) -> 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 user_id = user.id_i32();
|
||||||
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
||||||
let exist = db_accessor.is_draft_data_exist(user_id, &name).await?;
|
let exist = db_accessor.is_draft_data_exist(user_id, &name).await?;
|
||||||
@ -101,7 +107,10 @@ impl DraftDataMutation {
|
|||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
mut input: CreateDraftDataDto<IscsDataOptions>,
|
mut input: CreateDraftDataDto<IscsDataOptions>,
|
||||||
) -> async_graphql::Result<DraftIscsDataDto> {
|
) -> 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());
|
input = input.with_user_id(user.id_i32());
|
||||||
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
||||||
let draft_data = db_accessor.create_draft_data(input.into()).await?;
|
let draft_data = db_accessor.create_draft_data(input.into()).await?;
|
||||||
@ -180,7 +189,10 @@ impl DraftDataMutation {
|
|||||||
id: i32,
|
id: i32,
|
||||||
name: String,
|
name: String,
|
||||||
) -> async_graphql::Result<DraftDataDto> {
|
) -> 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 user_id = user.id_i32();
|
||||||
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
||||||
let draft_data = db_accessor.save_as_new_draft(id, &name, user_id).await?;
|
let draft_data = db_accessor.save_as_new_draft(id, &name, user_id).await?;
|
||||||
|
@ -17,7 +17,7 @@ use crate::RtssDbLoader;
|
|||||||
use super::common::{DataOptions, IscsDataOptions};
|
use super::common::{DataOptions, IscsDataOptions};
|
||||||
use super::{PageDto, PageQueryDto};
|
use super::{PageDto, PageQueryDto};
|
||||||
|
|
||||||
use crate::user_auth::{Role, RoleGuard, UserInfoDto};
|
use crate::user_auth::{Role, RoleGuard, Token, UserAuthCache};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ReleaseDataQuery;
|
pub struct ReleaseDataQuery;
|
||||||
@ -126,7 +126,10 @@ impl ReleaseDataMutation {
|
|||||||
name: String,
|
name: String,
|
||||||
description: String,
|
description: String,
|
||||||
) -> async_graphql::Result<ReleaseDataWithUsedVersionDto> {
|
) -> 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 user_id = user.id_i32();
|
||||||
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
||||||
let result = db_accessor
|
let result = db_accessor
|
||||||
@ -143,7 +146,10 @@ impl ReleaseDataMutation {
|
|||||||
draft_id: i32,
|
draft_id: i32,
|
||||||
description: String,
|
description: String,
|
||||||
) -> async_graphql::Result<ReleaseDataWithUsedVersionDto> {
|
) -> 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 user_id = user.id_i32();
|
||||||
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
||||||
let result = db_accessor
|
let result = db_accessor
|
||||||
@ -202,7 +208,10 @@ impl ReleaseDataMutation {
|
|||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
version_id: i32,
|
version_id: i32,
|
||||||
) -> async_graphql::Result<DraftDataDto> {
|
) -> 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 user_id = user.id_i32();
|
||||||
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
let db_accessor = ctx.data::<RtssDbAccessor>()?;
|
||||||
let result = db_accessor
|
let result = db_accessor
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::{HashMap, HashSet},
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ use axum::http::HeaderMap;
|
|||||||
use rtss_log::tracing::error;
|
use rtss_log::tracing::error;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Clone, Copy)]
|
#[derive(Eq, PartialEq, Clone, Copy, Hash)]
|
||||||
pub enum Role {
|
pub enum Role {
|
||||||
Admin,
|
Admin,
|
||||||
User,
|
User,
|
||||||
@ -31,6 +31,9 @@ impl Guard for RoleGuard {
|
|||||||
let user_auth_cache = ctx.data::<UserAuthCache>().unwrap();
|
let user_auth_cache = ctx.data::<UserAuthCache>().unwrap();
|
||||||
let user_info = user_auth_cache.query_user(&token.0).await?;
|
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) {
|
if user_info.roles().contains(&self.role) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -155,7 +158,6 @@ pub struct UserInfoDto {
|
|||||||
pub nickname: Option<String>,
|
pub nickname: Option<String>,
|
||||||
pub roles: Vec<String>,
|
pub roles: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserInfoDto {
|
impl UserInfoDto {
|
||||||
pub fn id_i32(&self) -> i32 {
|
pub fn id_i32(&self) -> i32 {
|
||||||
self.id
|
self.id
|
||||||
@ -164,14 +166,19 @@ impl UserInfoDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn roles(&self) -> Vec<Role> {
|
pub fn roles(&self) -> Vec<Role> {
|
||||||
self.roles
|
let mut unique_roles = HashSet::new();
|
||||||
.iter()
|
for role in &self.roles {
|
||||||
.filter_map(|role| match role.as_str() {
|
match role.as_str() {
|
||||||
"04" | "05" => Some(Role::Admin),
|
"04" | "05" => {
|
||||||
"01" | "03" => Some(Role::User),
|
unique_roles.insert(Role::Admin);
|
||||||
_ => None,
|
}
|
||||||
})
|
"01" | "03" => {
|
||||||
.collect()
|
unique_roles.insert(Role::User);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unique_roles.into_iter().collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user