feat(users): profile level custom role write (#7877)

This commit is contained in:
Riddhiagrawal001
2025-04-24 15:16:02 +05:30
committed by GitHub
parent b461087567
commit 54e910582f
7 changed files with 38 additions and 24 deletions

View File

@ -78,13 +78,6 @@ pub async fn create_role(
return Err(report!(UserErrors::InvalidRoleOperation))
.attach_printable("User trying to create org level custom role");
}
// TODO: Remove in PR custom-role-write-pr
if matches!(role_entity_type, EntityType::Profile) {
return Err(report!(UserErrors::InvalidRoleOperation))
.attach_printable("User trying to create profile level custom role");
}
let requestor_entity_from_role_scope = EntityType::from(req.role_scope);
if requestor_entity_from_role_scope < role_entity_type {
@ -120,13 +113,15 @@ pub async fn create_role(
.await?;
let (org_id, merchant_id, profile_id) = match role_entity_type {
EntityType::Organization | EntityType::Tenant => {
(user_from_token.org_id, user_from_token.merchant_id, None)
}
EntityType::Merchant => (user_from_token.org_id, user_from_token.merchant_id, None),
EntityType::Organization | EntityType::Tenant => (user_from_token.org_id, None, None),
EntityType::Merchant => (
user_from_token.org_id,
Some(user_from_token.merchant_id),
None,
),
EntityType::Profile => (
user_from_token.org_id,
user_from_token.merchant_id,
Some(user_from_token.merchant_id),
Some(user_from_token.profile_id),
),
};

View File

@ -265,7 +265,13 @@ impl RoleInterface for MockDb {
&& (role.tenant_id == *tenant_id)
&& role.org_id == *org_id
&& ((role.scope == RoleScope::Organization)
|| (role.merchant_id == *merchant_id && role.scope == RoleScope::Merchant)
|| (role
.merchant_id
.as_ref()
.is_some_and(|merchant_id_from_role| {
merchant_id_from_role == merchant_id
&& role.scope == RoleScope::Merchant
}))
|| (role
.profile_id
.as_ref()
@ -369,10 +375,10 @@ impl RoleInterface for MockDb {
let roles_list: Vec<_> = roles
.iter()
.filter(|role| {
let matches_merchant = match merchant_id {
Some(merchant_id) => role.merchant_id == *merchant_id,
None => true,
};
let matches_merchant = merchant_id
.zip(role.merchant_id.as_ref())
.map(|(merchant_id, role_merchant_id)| merchant_id == role_merchant_id)
.unwrap_or(true);
matches_merchant
&& role.org_id == *org_id
@ -420,17 +426,26 @@ impl RoleInterface for MockDb {
vec![EntityType::Merchant]
};
let matches_merchant = role
.merchant_id
.as_ref()
.is_some_and(|merchant_id_from_role| merchant_id_from_role == merchant_id);
role.tenant_id == tenant_id
&& role.org_id == org_id
&& (role.scope == RoleScope::Organization
|| role.merchant_id == *merchant_id)
&& (role.scope == RoleScope::Organization || matches_merchant)
&& entity_in_vec.contains(&role.entity_type)
}
storage::ListRolesByEntityPayload::Profile(merchant_id, profile_id) => {
let entity_in_vec = [EntityType::Profile];
let matches_merchant =
role.merchant_id == *merchant_id && role.scope == RoleScope::Merchant;
role.merchant_id
.as_ref()
.is_some_and(|merchant_id_from_role| {
merchant_id_from_role == merchant_id
&& role.scope == RoleScope::Merchant
});
let matches_profile =
role.profile_id