Skip to content

Commit 2f31ec4

Browse files
authored
fix: bulk migration user to roles association in case of non primary user (#1157)
## Summary of change (A few sentences about this PR) ## Related issues - Link to issue1 here - Link to issue1 here ## Test Plan (Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos!) ## Documentation changes (If relevant, please create a PR in our [docs repo](https://github.com/supertokens/docs), or create a checklist here highlighting the necessary changes) ## Checklist for important updates - [ ] Changelog has been updated - [ ] If there are any db schema changes, mention those changes clearly - [ ] `coreDriverInterfaceSupported.json` file has been updated (if needed) - [ ] `pluginInterfaceSupported.json` file has been updated (if needed) - [ ] Changes to the version if needed - In `build.gradle` - [ ] If added a new paid feature, edit the `getPaidFeatureStats` function in FeatureFlag.java file - [ ] Had installed and ran the pre-commit hook - [ ] If there are new dependencies that have been added in `build.gradle`, please make sure to add them in `implementationDependencies.json`. - [ ] Update function `getValidFields` in `io/supertokens/config/CoreConfig.java` if new aliases were added for any core config (similar to the `access_token_signing_key_update_interval` config alias). - [ ] Issue this PR against the latest non released version branch. - To know which one it is, run find the latest released tag (`git tag`) in the format `vX.Y.Z`, and then find the latest branch (`git branch --all`) whose `X.Y` is greater than the latest released tag. - If no such branch exists, then create one from the latest released branch. - [ ] If added a foreign key constraint on `app_id_to_user_id` table, make sure to delete from this table when deleting the user as well if `deleteUserIdMappingToo` is false. - [ ] If added a new recipe, then make sure to update the bulk import API to include the new recipe. ## Remaining TODOs for this PR - [ ] Item1 - [ ] Item2
2 parents 1cedc88 + 6b3b882 commit 2f31ec4

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
## [11.0.4]
11+
12+
- Fixes user to roles association in bulk import users when the user is not a primary user
13+
1014
## [11.0.3]
1115

1216
- Fixes BatchUpdateException checks and error handling to prevent bulk import users stuck in `PROCESSING` state

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ compileTestJava { options.encoding = "UTF-8" }
2020
// }
2121
//}
2222

23-
version = "11.0.3"
23+
version = "11.0.4"
2424

2525
repositories {
2626
mavenCentral()

src/main/java/io/supertokens/bulkimport/BulkImport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ private static Map<TenantIdentifier, Map<String, List<String>>> gatherRolesForUs
687687
rolesToUserByTenant.put(tenantIdentifier, new HashMap<>());
688688
}
689689
String userIdToUse = user.externalUserId != null ?
690-
user.externalUserId : user.primaryUserId;
690+
user.externalUserId : user.id;
691691
if(!rolesToUserByTenant.get(tenantIdentifier).containsKey(userIdToUse)){
692692
rolesToUserByTenant.get(tenantIdentifier).put(userIdToUse, new ArrayList<>());
693693
}

src/test/java/io/supertokens/test/bulkimport/apis/ImportUserTest.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,71 @@ public void shouldImportSucceedWithoutAccountLinkingEnabled() throws Exception {
265265
}
266266

267267
FeatureFlagTestContent.getInstance(main).setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES,
268-
new EE_FEATURES[] { EE_FEATURES.MULTI_TENANCY});
268+
new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY});
269+
270+
BulkImportTestUtils.createTenants(process);
271+
272+
String userJsonStr = """
273+
{
274+
"externalUserId":"some-text-you-like",
275+
"userRoles":[
276+
{
277+
"role":"user",
278+
"tenantIds":[
279+
"public",
280+
"t1"
281+
]
282+
}
283+
],
284+
"loginMethods":[
285+
{
286+
"tenantIds":[
287+
"public",
288+
"t1"
289+
],
290+
"isVerified":true,
291+
"isPrimary":false,
292+
"timeJoinedInMSSinceEpoch":1506523117518,
293+
"recipeId":"emailpassword",
294+
"email":"something0@testing.com",
295+
// passwordHash is randomly generated
296+
"passwordHash":"$argon2id$v=19$m=23298,t=5,p=12$+AlWgiuzC2vqlKrde9G0SG$PmpDeTU2e6ORbHwUMi7MOavS0M3sUJlc9rX/o+nnSxt",
297+
"hashingAlgorithm":"argon2"
298+
}
299+
]
300+
}""";
301+
302+
String user2JsonStr = """
303+
{
304+
"userRoles":[
305+
{
306+
"role":"user",
307+
"tenantIds":[
308+
"public",
309+
"t1"
310+
]
311+
}
312+
],
313+
"loginMethods":[
314+
{
315+
"tenantIds":[
316+
"public",
317+
"t1"
318+
],
319+
"isVerified":true,
320+
"isPrimary":false,
321+
"timeJoinedInMSSinceEpoch":1506523117518,
322+
"recipeId":"emailpassword",
323+
"email":"something1@testing.com",
324+
// passwordHash is randomly generated
325+
"passwordHash":"$argon2id$v=19$m=23298,t=5,p=12$+AlWgiuzC2vqlKrde9G0SG$PmpDeTU2e6ORbHwUMi7MOavS0M3sUJlc9rX/o+nnSxt",
326+
"hashingAlgorithm":"argon2"
327+
}
328+
]
329+
}""";
330+
// Create user roles
331+
UserRoles.createNewRoleOrModifyItsPermissions(main, "user", null);
269332

270-
String userJsonStr = "{\"id\":\"random-id-lol\",\"loginMethods\":[{\"tenantIds\":[\"public\"],\"isVerified\":true,\"isPrimary\":false,\"timeJoinedInMSSinceEpoch\":1741077729471,\"recipeId\":\"emailpassword\",\"email\":\"test@sometestmail.com\",\"passwordHash\":\"$2a\",\"hashingAlgorithm\":\"BCRYPT\"}]}";
271333
JsonObject request = new Gson().fromJson(userJsonStr, JsonObject.class);
272334

273335
JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(main, "",
@@ -277,6 +339,14 @@ public void shouldImportSucceedWithoutAccountLinkingEnabled() throws Exception {
277339
assertEquals("OK", response.get("status").getAsString());
278340
assertNotNull(response.get("user"));
279341

342+
request = new Gson().fromJson(user2JsonStr, JsonObject.class);
343+
JsonObject response2 = HttpRequestForTesting.sendJsonPOSTRequest(main, "",
344+
"http://localhost:3567/bulk-import/import",
345+
request, 1000, 1000, null, Utils.getCdiVersionStringLatestForTests(), null);
346+
347+
assertEquals("OK", response2.get("status").getAsString());
348+
assertNotNull(response2.get("user"));
349+
280350
process.kill();
281351
Assert.assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
282352
}

0 commit comments

Comments
 (0)