Skip to content

Commit

Permalink
feat: 添加 InnerCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjing committed Aug 22, 2024
1 parent 2b0eff7 commit 8d6e07f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ultimate-web = { version = "0.1", path = "ultimates/ultimate-web" }
# begin -- memory allocator
tikv-jemallocator = "0.6"
# end -- memory allocator
derive_more = { version = "1.0", features = ["from", "display", "constructor"] }
derive_more = { version = "1.0", features = ["from", "display"] }
toml = "0.8"
config = { version = "0.14", default-features = false, features = [
"toml",
Expand Down
1 change: 1 addition & 0 deletions examples/api-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sea-query.workspace = true
sea-query-binder.workspace = true
modql.workspace = true
enum-iterator.workspace = true
derive-new = "0.6"

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator.workspace = true
6 changes: 3 additions & 3 deletions examples/api-example/src/auth/auth_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use axum::{
extract::FromRequestParts,
http::{request::Parts, StatusCode},
};
use derive_more::derive::Constructor;
use derive_new::new;
use ultimate::{security::pwd::verify_pwd, Result};
use ultimate_web::AppError;

Expand All @@ -15,14 +15,14 @@ use crate::{

use super::{LoginByPwdReq, LoginResp, TokenType};

#[derive(Constructor)]
#[derive(new)]
pub struct AuthServ {
app: AppState,
}

impl AuthServ {
pub async fn login_by_pwd(&self, req: LoginByPwdReq) -> Result<LoginResp> {
let user_serv = UserServ::new(self.app.clone(), self.app.create_super_admin_ctx());
let user_serv = UserServ::new(self.app.create_super_admin_ctx());

let (u, uc) = user_serv.get_fetch_credential(UserFilter::from(&req)).await?;
verify_pwd(&req.pwd, &uc.encrypted_pwd).await?;
Expand Down
4 changes: 2 additions & 2 deletions examples/api-example/src/user/user_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use axum::{
http::{request::Parts, StatusCode},
Json,
};
use derive_more::derive::Constructor;
use derive_new::new;
use ultimate::{DataError, Result};
use ultimate_web::AppError;

Expand All @@ -14,7 +14,7 @@ use super::{
User, UserBmc, UserCredential, UserCredentialBmc, UserFilter, UserForCreate, UserForPage, UserForUpdate, UserPage,
};

#[derive(Constructor)]
#[derive(new)]
pub struct UserServ {
ctx: CtxW,
}
Expand Down
2 changes: 1 addition & 1 deletion ultimates/ultimate-web/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn ok_ulid(id: Ulid) -> AppResult<IdUlidResult> {

#[inline]
#[cfg(feature = "uuid")]
pub fn ok_uuid(id: uuid::Uuid) -> AppResult<IdI64Result> {
pub fn ok_uuid(id: uuid::Uuid) -> AppResult<ultimate::IdUuidResult> {
Ok(ultimate::IdUuidResult::new(id).into())
}

Expand Down
63 changes: 43 additions & 20 deletions ultimates/ultimate/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{ops::Deref, sync::Arc};

use josekit::jwt::JwtPayload;

use ultimate_common::time::{self, DateTime, Duration, Utc, UtcDateTime};

use crate::DataError;

/// 会话上下文。
/// 此处 clone 的成本很低,若后续数据多的话可以使用 Arc 加 Wrapper 模式来降低数据复制的成本
#[derive(Clone, Debug, Default)]
pub struct Ctx {
#[derive(Debug, Default)]
pub struct InnerCtx {
/// 会话用户 ID
uid: i64,

Expand All @@ -27,9 +27,14 @@ pub struct Ctx {
ext_privileges: Vec<i64>,
}

/// 会话上下文。
/// 此处 clone 的成本很低,若后续数据多的话可以使用 Arc 加 Wrapper 模式来降低数据复制的成本
#[derive(Clone, Debug, Default)]
pub struct Ctx(Arc<InnerCtx>);

impl Ctx {
pub fn new(uid: i64, req_time: UtcDateTime, expires_at: UtcDateTime) -> Self {
Self { uid, req_time, expires_at, ..Default::default() }
Self(Arc::new(InnerCtx { uid, req_time, expires_at, ..Default::default() }))
}

pub fn new_root() -> Self {
Expand All @@ -52,37 +57,41 @@ impl Ctx {
&self.req_time
}

pub fn with_expires_at(mut self, expires_at: UtcDateTime) -> Self {
self.expires_at = expires_at;
self
pub fn expires_at(&self) -> &UtcDateTime {
&self.expires_at
}

// pub fn with_expires_at(mut self, expires_at: UtcDateTime) -> Self {
// self.expires_at = expires_at;
// self
// }

pub fn ext_orgs(&self) -> &[i64] {
&self.ext_orgs
}

pub fn with_ext_orgs(mut self, ext_orgs: Vec<i64>) -> Self {
self.ext_orgs = ext_orgs;
self
}
// pub fn with_ext_orgs(mut self, ext_orgs: Vec<i64>) -> Self {
// self.ext_orgs = ext_orgs;
// self
// }

pub fn ext_roles(&self) -> &[i64] {
&self.ext_roles
}

pub fn with_ext_roles(mut self, ext_roles: Vec<i64>) -> Self {
self.ext_roles = ext_roles;
self
}
// pub fn with_ext_roles(mut self, ext_roles: Vec<i64>) -> Self {
// self.0.ext_roles = ext_roles;
// self
// }

pub fn ext_privileges(&self) -> &[i64] {
&self.ext_privileges
}

pub fn with_ext_privileges(mut self, ext_privileges: Vec<i64>) -> Self {
self.ext_privileges = ext_privileges;
self
}
// pub fn with_ext_privileges(mut self, ext_privileges: Vec<i64>) -> Self {
// self.ext_privileges = ext_privileges;
// self
// }

pub fn try_from_jwt_payload(payload: &JwtPayload, req_time: Option<UtcDateTime>) -> Result<Self, DataError> {
let req_time = req_time.unwrap_or_else(time::now_utc);
Expand All @@ -105,6 +114,20 @@ impl Ctx {
}
}

impl Deref for Ctx {
type Target = InnerCtx;

fn deref(&self) -> &Self::Target {
&self.0
}
}

// impl DerefMut for Ctx {
// fn deref_mut(&mut self) -> &mut Self::Target {
// &mut self.0
// }
// }

impl TryFrom<JwtPayload> for Ctx {
type Error = DataError;

Expand Down

0 comments on commit 8d6e07f

Please sign in to comment.