Skip to content

Commit

Permalink
[DERCBOT-1071] Prevent duplication of namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
assouktim authored and vsct-jburet committed Jun 28, 2024
1 parent 5ea08ec commit 6f2f3fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nlp/front/service/src/main/kotlin/AdminTockUserListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object AdminTockUserListener : TockUserListener {

// if existing: take it
do {
selected = existingNamespaces.find { it.namespace == namespace }?.copy(current = true)
selected = existingNamespaces.find { it.namespace.equals(namespace, ignoreCase = true) }?.copy(current = true)
if (selected == null && (joinNamespace || namespaceDAO.getUsers(namespace).isEmpty())) {
selected = UserNamespace(user.user, namespace, true, true)
} else {
Expand Down
19 changes: 16 additions & 3 deletions nlp/front/storage-mongo/src/main/kotlin/UserNamespaceMongoDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ import ai.tock.nlp.front.shared.user.UserNamespace_.Companion.Login
import ai.tock.nlp.front.shared.user.UserNamespace_.Companion.Namespace
import ai.tock.nlp.front.shared.user.UserNamespace_.Companion.Owner
import com.mongodb.client.MongoCollection
import com.mongodb.client.model.Filters
import com.mongodb.client.model.ReplaceOptions
import org.bson.conversions.Bson
import org.litote.kmongo.and
import org.litote.kmongo.ensureIndex
import org.litote.kmongo.ensureUniqueIndex
import org.litote.kmongo.eq
import org.litote.kmongo.getCollection
import org.litote.kmongo.setValue
import java.util.regex.Pattern

object UserNamespaceMongoDAO : UserNamespaceDAO {

Expand All @@ -43,7 +46,8 @@ object UserNamespaceMongoDAO : UserNamespaceDAO {

override fun getNamespaces(user: String): List<UserNamespace> = col.find(Login eq user).toList()

override fun getUsers(namespace: String): List<UserNamespace> = col.find(Namespace eq namespace).toList()
override fun getUsers(namespace: String): List<UserNamespace> =
col.find(getCaseInsensitiveBsonFilter(Namespace.name, namespace)).toList()

override fun saveNamespace(namespace: UserNamespace) {
col.replaceOne(
Expand All @@ -68,6 +72,15 @@ object UserNamespaceMongoDAO : UserNamespaceDAO {
override fun isNamespaceOwner(user: String, namespace: String): Boolean =
col.countDocuments(and(Login eq user, Namespace eq namespace, Owner eq true)) == 1L

override fun isExistingNamespace(namespace: String): Boolean =
col.countDocuments(Namespace eq namespace) != 0L
override fun isExistingNamespace(namespace: String): Boolean {
return col.countDocuments(getCaseInsensitiveBsonFilter(Namespace.name, namespace)) != 0L
}

/**
* obtain the case-insensitive bson filter
*/
private fun getCaseInsensitiveBsonFilter(field: String, input: String): Bson {
val escapedNamespace = Pattern.quote(input)
return Filters.regex(Namespace.name, "^$escapedNamespace$", "i")
}
}

0 comments on commit 6f2f3fb

Please sign in to comment.