Skip to content

Commit d4639d8

Browse files
fix: fix ID type in permission (#2006)
1 parent 234ea74 commit d4639d8

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

jupiter/src/storage/note_storage.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::ops::Deref;
22

33
use callisto::notes;
44
use common::errors::MegaError;
5-
use sea_orm::{ActiveModelTrait, ActiveValue::Set, EntityTrait, IntoActiveModel};
5+
use sea_orm::{
6+
ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter,
7+
};
68

79
use crate::storage::base_storage::{BaseStorage, StorageConnector};
810

@@ -25,6 +27,17 @@ impl NoteStorage {
2527
.await?;
2628
Ok(model)
2729
}
30+
31+
pub async fn get_note_by_public_id(
32+
&self,
33+
public_id: &str,
34+
) -> Result<Option<notes::Model>, MegaError> {
35+
let model = notes::Entity::find()
36+
.filter(notes::Column::PublicId.eq(public_id))
37+
.one(self.get_connection())
38+
.await?;
39+
Ok(model)
40+
}
2841
pub async fn save_note(&self, note: notes::Model) -> Result<(), MegaError> {
2942
let a_model = note.into_active_model();
3043
a_model.insert(self.get_connection()).await?;

mono/src/api/api_common/group_permission.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ pub async fn ensure_admin(state: &MonoApiServiceState, user: &LoginUser) -> Resu
2424
))
2525
}
2626

27-
pub fn parse_resource_context(
27+
pub async fn resolve_resource_context(
28+
state: &MonoApiServiceState,
2829
resource_type: &str,
2930
resource_id: &str,
3031
) -> Result<(ResourceTypeEnum, ResourceTypeValue, String), ApiError> {
@@ -33,7 +34,8 @@ pub fn parse_resource_context(
3334
ApiError::bad_request(anyhow!(err))
3435
})?;
3536

36-
let validated_resource_id = validate_resource_id(resource_type_value, resource_id)?;
37+
let validated_resource_id =
38+
resolve_resource_id(state, resource_type_value, resource_id).await?;
3739

3840
Ok((
3941
resource_type_value.into(),
@@ -62,20 +64,36 @@ pub fn build_user_effective_permission_response(
6264
}
6365
}
6466

65-
fn validate_resource_id(
67+
async fn resolve_resource_id(
68+
state: &MonoApiServiceState,
6669
resource_type: ResourceTypeValue,
6770
resource_id: &str,
6871
) -> Result<String, ApiError> {
72+
let normalized_resource_id = resource_id.trim();
73+
if normalized_resource_id.is_empty() {
74+
tracing::warn!("empty resource_id in request path");
75+
return Err(ApiError::bad_request(anyhow!(
76+
"resource_id must not be empty"
77+
)));
78+
}
79+
6980
match resource_type {
7081
ResourceTypeValue::Note => {
71-
let note_id = resource_id.parse::<i64>().map_err(|_| {
72-
tracing::warn!("invalid resource_id format");
73-
ApiError::bad_request(anyhow!(
74-
"Invalid note resource_id: {}, expected i64 note.id",
75-
resource_id
76-
))
77-
})?;
78-
Ok(note_id.to_string())
82+
let note = state
83+
.note_stg()
84+
.get_note_by_public_id(normalized_resource_id)
85+
.await?
86+
.ok_or_else(|| {
87+
tracing::warn!(
88+
resource_id = normalized_resource_id,
89+
"note resource not found"
90+
);
91+
ApiError::not_found(anyhow!(
92+
"Note not found for public_id: {}",
93+
normalized_resource_id
94+
))
95+
})?;
96+
Ok(note.public_id)
7997
}
8098
}
8199
}

mono/src/api/router/group_router.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
api::{
2020
MonoApiServiceState,
2121
api_common::group_permission::{
22-
build_user_effective_permission_response, ensure_admin, parse_resource_context,
22+
build_user_effective_permission_response, ensure_admin, resolve_resource_context,
2323
},
2424
error::ApiError,
2525
oauth::model::LoginUser,
@@ -393,7 +393,7 @@ async fn list_group_members(
393393
(status = 400, description = "Invalid request"),
394394
(status = 401, description = "Unauthorized"),
395395
(status = 403, description = "Forbidden - admin only"),
396-
(status = 404, description = "Group not found"),
396+
(status = 404, description = "Resource or group not found"),
397397
),
398398
tag = GROUP_PERMISSION_TAG
399399
)]
@@ -405,7 +405,7 @@ async fn set_resource_permissions(
405405
) -> Result<Json<CommonResult<Vec<ResourcePermissionResponse>>>, ApiError> {
406406
ensure_admin(&state, &user).await?;
407407
let (resource_type, _, resource_id) =
408-
parse_resource_context(resource_type.as_str(), &resource_id)?;
408+
resolve_resource_context(&state, resource_type.as_str(), &resource_id).await?;
409409

410410
let permissions = req
411411
.permissions
@@ -437,6 +437,7 @@ async fn set_resource_permissions(
437437
(status = 400, description = "Invalid request"),
438438
(status = 401, description = "Unauthorized"),
439439
(status = 403, description = "Forbidden - admin only"),
440+
(status = 404, description = "Resource not found"),
440441
),
441442
tag = GROUP_PERMISSION_TAG
442443
)]
@@ -447,7 +448,7 @@ async fn get_resource_permissions(
447448
) -> Result<Json<CommonResult<Vec<ResourcePermissionResponse>>>, ApiError> {
448449
ensure_admin(&state, &user).await?;
449450
let (resource_type, _, resource_id) =
450-
parse_resource_context(resource_type.as_str(), &resource_id)?;
451+
resolve_resource_context(&state, resource_type.as_str(), &resource_id).await?;
451452

452453
let permissions = state
453454
.monorepo()
@@ -471,7 +472,7 @@ async fn get_resource_permissions(
471472
(status = 400, description = "Invalid request"),
472473
(status = 401, description = "Unauthorized"),
473474
(status = 403, description = "Forbidden - admin only"),
474-
(status = 404, description = "Group not found"),
475+
(status = 404, description = "Resource or group not found"),
475476
),
476477
tag = GROUP_PERMISSION_TAG
477478
)]
@@ -483,7 +484,7 @@ async fn update_resource_permissions(
483484
) -> Result<Json<CommonResult<Vec<ResourcePermissionResponse>>>, ApiError> {
484485
ensure_admin(&state, &user).await?;
485486
let (resource_type, _, resource_id) =
486-
parse_resource_context(resource_type.as_str(), &resource_id)?;
487+
resolve_resource_context(&state, resource_type.as_str(), &resource_id).await?;
487488

488489
let permissions = req
489490
.permissions
@@ -515,6 +516,7 @@ async fn update_resource_permissions(
515516
(status = 400, description = "Invalid request"),
516517
(status = 401, description = "Unauthorized"),
517518
(status = 403, description = "Forbidden - admin only"),
519+
(status = 404, description = "Resource not found"),
518520
),
519521
tag = GROUP_PERMISSION_TAG
520522
)]
@@ -525,7 +527,7 @@ async fn delete_resource_permissions(
525527
) -> Result<Json<CommonResult<DeletePermissionsResponse>>, ApiError> {
526528
ensure_admin(&state, &user).await?;
527529
let (resource_type, resource_type_value, resource_id) =
528-
parse_resource_context(resource_type.as_str(), &resource_id)?;
530+
resolve_resource_context(&state, resource_type.as_str(), &resource_id).await?;
529531

530532
let deleted_count = state
531533
.monorepo()
@@ -583,6 +585,7 @@ async fn get_user_groups(
583585
(status = 400, description = "Invalid request"),
584586
(status = 401, description = "Unauthorized"),
585587
(status = 403, description = "Forbidden - admin only"),
588+
(status = 404, description = "Resource not found"),
586589
),
587590
tag = GROUP_PERMISSION_TAG
588591
)]
@@ -593,7 +596,7 @@ async fn get_user_effective_permission(
593596
) -> Result<Json<CommonResult<UserEffectivePermissionResponse>>, ApiError> {
594597
ensure_admin(&state, &user).await?;
595598
let (resource_type, resource_type_value, resource_id) =
596-
parse_resource_context(resource_type.as_str(), &resource_id)?;
599+
resolve_resource_context(&state, resource_type.as_str(), &resource_id).await?;
597600

598601
let effective = state
599602
.monorepo()

mono/src/api/router/permission_router.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
api::{
1111
MonoApiServiceState,
1212
api_common::group_permission::{
13-
build_user_effective_permission_response, parse_resource_context,
13+
build_user_effective_permission_response, resolve_resource_context,
1414
},
1515
error::ApiError,
1616
oauth::model::LoginUser,
@@ -36,6 +36,7 @@ pub fn routers() -> OpenApiRouter<MonoApiServiceState> {
3636
(status = 200, body = CommonResult<UserEffectivePermissionResponse>),
3737
(status = 400, description = "Invalid resource_type or resource_id"),
3838
(status = 401, description = "Unauthorized"),
39+
(status = 404, description = "Resource not found"),
3940
),
4041
tag = GROUP_PERMISSION_TAG
4142
)]
@@ -47,7 +48,7 @@ async fn get_my_permission(
4748
let actor = user.username;
4849

4950
let (db_resource_type, resource_type_value, normalized_id) =
50-
parse_resource_context(&resource_type, &resource_id)?;
51+
resolve_resource_context(&state, &resource_type, &resource_id).await?;
5152

5253
let effective = state
5354
.monorepo()

0 commit comments

Comments
 (0)