fix(users): fallback to fresh lineage context if cached role_id no longer matches #8593
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Type of Change
Description
This PR fixes an issue where a stale
role_id
from the cachedlineage_context
was being used to generate the JWT token during sign-in, even if the actual user role was modified (e.g., role deleted and recreated with a differentrole_id
but same lineage).Previously, we only validated if a user role existed for the lineage (
user_id
,tenant_id
,org_id
,merchant_id
,profile_id
), but did not verify that therole_id
matched the current assigned role. This led to JWTs being issued with outdatedrole_id
values.Changes Made
role_id
in the fetched user role.role_id
from cachedlineage_context
does not match the current role fetched from DB (v2 fallback to v1), we now resolve the lineage afresh from the currentuser_role
object.Additional Changes
Motivation and Context
In systems where roles can be modified dynamically (e.g., deleted and recreated), relying on cached
lineage_context.role_id
without validating it introduces a silent inconsistency. This can lead to:This fix ensures that JWTs are always generated using valid and current
role_id
information associated with the user role.How did you test it?
Scenario: Validate fallback when
role_id
has changedInitial Setup
org_admin
of Org A.org_admin
.Initial Sign-In
lineage_context
based on the selected values.Modify Role Assignment
org_admin
of Org A.org_admin
) of User X in Org A.profile_admin
using the same merchant and profile from the storedlineage_context
.profile_admin
role.Re-Sign-In Validation
org_admin
) no longer exists, the cached lineage context will fail validation.Verify Correct Role Assignment
role_id
:/user
API.role_id
should now correctly reflectprofile_admin
.Checklist
cargo +nightly fmt --all
cargo clippy