Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Riddhiagrawal001
Copy link
Contributor

@Riddhiagrawal001 Riddhiagrawal001 commented Nov 29, 2024

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

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

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

Closes 6702

How did you test it?

Using dashboard . All the below should work as before
Cases to test :

  • Create a custom role from dashboard
  • Invite someone for that custom role
  • signin via that custom-role-user
  • List roles and list roles at entity level

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

Copy link

semanticdiff-com bot commented Nov 29, 2024

.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)
Copy link
Contributor

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.

Comment on lines 207 to 208
if matches!(role_info.get_scope(), RoleScope::Organization)
&& user_from_token.role_id != common_utils::consts::ROLE_ID_ORGANIZATION_ADMIN
Copy link
Contributor

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.

Comment on lines 202 to 205
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)?;
Copy link
Contributor

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.

}

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(
Copy link
Contributor

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....

@hyperswitch-bot hyperswitch-bot bot added the M-database-changes Metadata: This PR involves database schema changes label Dec 2, 2024
@@ -26,6 +26,7 @@ impl Role {
.await
}

// TODO:remove once find_by_role_id_in_lineage is stable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO:remove once find_by_role_id_in_lineage is stable
// TODO: Remove once find_by_role_id_in_lineage is stable

Comment on lines +32 to 44
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))
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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(
Copy link
Contributor

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(
Copy link
Contributor

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well.

Comment on lines +211 to +217
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)?;
Copy link
Contributor

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
Copy link
Contributor

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well.

@ThisIsMani
Copy link
Contributor

Can you change the title of the PR.

@ThisIsMani
Copy link
Contributor

As this supposed to be intermediate PR, shouldn't you add Profile in RoleScope?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
M-database-changes Metadata: This PR involves database schema changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor(users): refactor find_by_role_id_in_merchant_scope query for custom roles
2 participants