From 70162a37f6858a0d3e66eb0dba670048c760b090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Thu, 20 Jun 2024 17:01:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=87=87=E7=94=A8Arc?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E9=AB=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/src/entities/account.rs | 16 ++++++++++++++++ library/src/cache/account_cache.rs | 2 +- library/src/middleware/req_ctx.rs | 2 +- service/src/account.rs | 8 ++++---- service/src/sys_account.rs | 6 +++--- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/domain/src/entities/account.rs b/domain/src/entities/account.rs index b150a55..ba49974 100644 --- a/domain/src/entities/account.rs +++ b/domain/src/entities/account.rs @@ -10,6 +10,22 @@ pub enum Role { User, } +impl Role { + pub fn is_admin(&self) -> bool { + match self { + Role::Admin => true, + Role::User => false, + } + } + + pub fn is_user(&self) -> bool { + match self { + Role::Admin => false, + Role::User => true, + } + } +} + impl Default for Role { fn default() -> Self { Role::User diff --git a/library/src/cache/account_cache.rs b/library/src/cache/account_cache.rs index 2f15fce..20d9f63 100644 --- a/library/src/cache/account_cache.rs +++ b/library/src/cache/account_cache.rs @@ -15,7 +15,7 @@ pub struct CacheAccount { } lazy_static! { - pub static ref LOGIN_CACHE: Cache = { + pub static ref LOGIN_CACHE: Cache> = { CacheBuilder::new(20480) .name("login_cache") .eviction_policy(EvictionPolicy::lru()) diff --git a/library/src/middleware/req_ctx.rs b/library/src/middleware/req_ctx.rs index 561ceba..f282db6 100644 --- a/library/src/middleware/req_ctx.rs +++ b/library/src/middleware/req_ctx.rs @@ -52,7 +52,7 @@ pub async fn authenticate_ctx(mut req: Request, next: Next) -> Response { req.extensions_mut().insert( Context { account: account.account.clone(), - token: account.token + token: account.token.clone() }); next.run(req).await }, diff --git a/service/src/account.rs b/service/src/account.rs index 66d626a..ce2bcee 100644 --- a/service/src/account.rs +++ b/service/src/account.rs @@ -59,10 +59,10 @@ pub async fn authenticate_google( LOGIN_CACHE .insert( account.id.to_owned(), - CacheAccount { + Arc::new(CacheAccount { account: Arc::new(account.to_owned()), token: Arc::new(token.to_owned()), - }, + }), ) .await; @@ -98,10 +98,10 @@ pub async fn refresh_token( LOGIN_CACHE .insert( account.id.to_owned(), - CacheAccount { + Arc::new(CacheAccount { account, token: Arc::new(refresh_token.token.to_owned()), - }, + }), ) .await; diff --git a/service/src/sys_account.rs b/service/src/sys_account.rs index 922cbef..a6b15ca 100644 --- a/service/src/sys_account.rs +++ b/service/src/sys_account.rs @@ -39,7 +39,7 @@ pub async fn authenticate_with_password( ACCOUNT_DISABLED ))); } - if account.role != Role::Admin { + if !account.role.is_admin() { tracing::error!("账户不是管理员,无权限"); return Err(ResErr::perm(message!( context.get_lang_id(), @@ -53,10 +53,10 @@ pub async fn authenticate_with_password( LOGIN_CACHE .insert( account.id.to_owned(), - CacheAccount { + Arc::new(CacheAccount { account: Arc::new(account.to_owned()), token: Arc::new(token.to_owned()), - }, + }), ) .await;