Skip to content

Commit 0317969

Browse files
authored
Merge pull request #2406 from digma-ai/change-user-id-order
Change user id order
2 parents 68603e4 + ea87782 commit 0317969

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ tasks {
316316

317317
)
318318

319-
//to simulate external user
319+
//to simulate external user. don't change the value it's used in UniqueGeneratedUserId
320320
// environment(
321-
// "devenv" to "notdigma"
321+
// "devenv" to "externalUserSimulator"
322322
// )
323323

324324
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/common/UniqueGeneratedUserId.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ object UniqueGeneratedUserId {
1313

1414
init {
1515
if (System.getenv("devenv") == "digma") {
16-
userId = CommonUtils.getLocalHostname()
16+
val userName = System.getProperty("user.name") ?: "user"
17+
userId = "$userName@${CommonUtils.getLocalHostname()}"
1718
isDevUser = true
19+
} else if (System.getenv("devenv") == "externalUserSimulator") {
20+
//this is just to overcome my own deleted external user which can not be recreated in posthog
21+
if (service<PersistenceService>().getUserId() == null) {
22+
service<PersistenceService>().setUserId(generateUniqueUserId("digma"))
23+
}
24+
userId = service<PersistenceService>().getUserId()!!
25+
isDevUser = false
1826
} else {
1927
if (service<PersistenceService>().getUserId() == null) {
2028
service<PersistenceService>().setUserId(generateUniqueUserId())
@@ -27,7 +35,7 @@ object UniqueGeneratedUserId {
2735

2836

2937
//this method is outside the class so that it can be tested,UniqueGeneratedUserId can not be instantiated in regular unit tests
30-
fun generateUniqueUserId(): String {
38+
fun generateUniqueUserId(salt: String = ""): String {
3139
try {
3240
val userName = System.getProperty("user.name")
3341
val userHome = System.getProperty("user.home")
@@ -36,7 +44,7 @@ fun generateUniqueUserId(): String {
3644
//MUST BE SORTED
3745
val ni = NetworkInterface.networkInterfaces().toList().mapNotNull { it.hardwareAddress }.map { macAddressToString(it) }.sorted()
3846
.joinToString("-")
39-
val baseString = userName + userHome + osName + osArch + ni
47+
val baseString = salt + userName + userHome + osName + osArch + ni
4048
return DigestUtils.sha1Hex(baseString)
4149
} catch (e: Throwable) {
4250
return DigestUtils.sha1Hex(UUID.randomUUID().toString())

ide-common/src/test/kotlin/org/digma/intellij/plugin/common/UserIdTests.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ class UserIdTests {
1414
}
1515
}
1616

17+
@Test
18+
fun testUniquenessWithSalt() {
19+
val myId = generateUniqueUserId("digma")
20+
repeat(10) {
21+
val id = generateUniqueUserId("digma")
22+
assertEquals(myId, id)
23+
}
24+
}
25+
1726
}

0 commit comments

Comments
 (0)