-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(users): refactor find_by_role_id_in_merchant_scope query for custom roles #6701
base: main
Are you sure you want to change the base?
Conversation
Changed Files
|
crates/router/src/core/user_role.rs
Outdated
.await | ||
.to_not_found_response(UserErrors::InvalidRoleId)?; | ||
let role_info = | ||
roles::RoleInfo::from_role_id_and_org_id(&state, &req.role_id, &user_from_token.org_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be lineage check here.
if matches!(role_info.get_scope(), RoleScope::Organization) | ||
&& user_from_token.role_id != common_utils::consts::ROLE_ID_ORGANIZATION_ADMIN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check this with entity type instead of role_id
.
let role_info = | ||
roles::RoleInfo::from_role_id_and_org_id(&state, role_id, &user_from_token.org_id) | ||
.await | ||
.to_not_found_response(UserErrors::InvalidRoleOperation)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also check if user has access to update this role. This can be done by getting the role using the lineage.
crates/router/src/utils/user_role.rs
Outdated
} | ||
|
||
pub async fn set_role_permissions_in_cache_by_role_id_merchant_id_org_id( | ||
pub async fn set_role_permissions_in_cache_by_role_id_org_id( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename these functions to set_role_info...
instead of set_permissions...
.
@@ -26,6 +26,7 @@ impl Role { | |||
.await | |||
} | |||
|
|||
// TODO:remove once find_by_role_id_in_lineage is stable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// TODO:remove once find_by_role_id_in_lineage is stable | |
// TODO: Remove once find_by_role_id_in_lineage is stable |
match self.version { | ||
enums::UserRoleVersion::V1 if self.entity_type.is_none() => { | ||
match self.role_id.as_str() { | ||
consts::ROLE_ID_ORGANIZATION_ADMIN => { | ||
let org_id = self.org_id.clone()?.get_string_repr().to_string(); | ||
Some((org_id, EntityType::Organization)) | ||
} | ||
_ => { | ||
let merchant_id = self.merchant_id.clone()?.get_string_repr().to_string(); | ||
Some((merchant_id, EntityType::Merchant)) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match self.version { | |
enums::UserRoleVersion::V1 if self.entity_type.is_none() => { | |
match self.role_id.as_str() { | |
consts::ROLE_ID_ORGANIZATION_ADMIN => { | |
let org_id = self.org_id.clone()?.get_string_repr().to_string(); | |
Some((org_id, EntityType::Organization)) | |
} | |
_ => { | |
let merchant_id = self.merchant_id.clone()?.get_string_repr().to_string(); | |
Some((merchant_id, EntityType::Merchant)) | |
} | |
} | |
} | |
match (self.version, self.entity_type, self.role_id) { | |
(enums::UserRoleVersion::V1, None, ...) => ... | |
... => ... |
@@ -489,7 +485,7 @@ pub async fn delete_user_role( | |||
.attach_printable("User deleting himself"); | |||
} | |||
|
|||
let deletion_requestor_role_info = roles::RoleInfo::from_role_id_in_merchant_scope( | |||
let deletion_requestor_role_info = roles::RoleInfo::from_role_id_in_lineage( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lineage
check is not required here.
@@ -527,10 +523,9 @@ pub async fn delete_user_role( | |||
}; | |||
|
|||
if let Some(role_to_be_deleted) = user_role_v2 { | |||
let target_role_info = roles::RoleInfo::from_role_id_in_merchant_scope( | |||
let target_role_info = roles::RoleInfo::from_role_id_and_org_id( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lineage
check should be here instead.
@@ -597,10 +592,9 @@ pub async fn delete_user_role( | |||
}; | |||
|
|||
if let Some(role_to_be_deleted) = user_role_v1 { | |||
let target_role_info = roles::RoleInfo::from_role_id_in_merchant_scope( | |||
let target_role_info = roles::RoleInfo::from_role_id_and_org_id( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well.
let user_from_token_role_info = roles::RoleInfo::from_role_id_and_org_id( | ||
&state, | ||
&user_from_token.role_id, | ||
&user_from_token.org_id, | ||
) | ||
.await | ||
.to_not_found_response(UserErrors::InvalidRoleId)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a function already implemented on UserFromToken
which will give the role_info
.
You can use that instead.
@@ -216,8 +208,16 @@ pub async fn update_role( | |||
.await | |||
.to_not_found_response(UserErrors::InvalidRoleOperation)?; | |||
|
|||
let user_from_token_role_info = roles::RoleInfo::from_role_id_and_org_id( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let user_from_token_role_info = roles::RoleInfo::from_role_id_and_org_id( | |
let user_role_info = roles::RoleInfo::from_role_id_and_org_id( |
@@ -72,31 +72,21 @@ pub async fn set_role_permissions_in_cache_by_user_role( | |||
state: &SessionState, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function name also should be changed.
@@ -105,18 +95,16 @@ pub async fn set_role_permissions_in_cache_by_role_id_merchant_id_org_id( | |||
pub async fn set_role_permissions_in_cache_if_required( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well.
Can you change the title of the PR. |
As this supposed to be intermediate PR, shouldn't you add |
Type of Change
Description
Refactor the find_by_role_id_in_merchant_scope query to make it a generic query that uses only org_id and role_id as parameters.
Additional Changes
Motivation and Context
Closes 6702
How did you test it?
Using dashboard . All the below should work as before
Cases to test :
Checklist
cargo +nightly fmt --all
cargo clippy