generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PI-1569 add onboarding endpoints for Pathfinder
- Loading branch information
1 parent
9ab6b41
commit 6c83f79
Showing
11 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
projects/pathfinder-and-delius/src/dev/resources/schema.ldif
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
dn: dc=moj,dc=com | ||
objectclass: top | ||
objectclass: domain | ||
dc: moj | ||
|
||
dn: ou=Users,dc=moj,dc=com | ||
objectclass: top | ||
objectclass: organizationalUnit | ||
ou: Users | ||
|
||
dn: cn=john-smith,ou=Users,dc=moj,dc=com | ||
objectclass: top | ||
objectclass: inetOrgPerson | ||
cn: john-smith | ||
sn: Smith | ||
givenname: John | ||
mail: [email protected] | ||
telephoneNumber: 07321165373 | ||
|
||
dn: cn=ndRoleCatalogue,ou=Users,dc=moj,dc=com | ||
description: Role Catalogue | ||
objectclass: top | ||
cn: ndRoleCatalogue | ||
|
||
dn: cn=CTRBT001,cn=ndRoleCatalogue,ou=Users,dc=moj,dc=com | ||
description: Pathfinder CT Probation | ||
Sector: public | ||
Level1: FALSE | ||
Level2: FALSE | ||
Level3: FALSE | ||
UIBusinessInteractionCollection: UPBI003 | ||
objectclass: NDRole | ||
objectclass: top | ||
cn: CTRBT001 |
2 changes: 1 addition & 1 deletion
2
...ts/pathfinder-and-delius/src/dev/resources/simulations/__files/hmpps-auth-token-body.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserRoleIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package uk.gov.justice.digital.hmpps | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.jupiter.api.MethodOrderer | ||
import org.junit.jupiter.api.Order | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestMethodOrder | ||
import org.junit.jupiter.api.assertThrows | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.ldap.NameNotFoundException | ||
import org.springframework.ldap.core.LdapTemplate | ||
import org.springframework.ldap.support.LdapNameBuilder | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import uk.gov.justice.digital.hmpps.model.DeliusRole | ||
import uk.gov.justice.digital.hmpps.security.withOAuth2Token | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation::class) | ||
internal class UserRoleIntegrationTest { | ||
@Autowired | ||
lateinit var mockMvc: MockMvc | ||
|
||
@Autowired | ||
lateinit var wireMockServer: WireMockServer | ||
|
||
@Autowired | ||
lateinit var ldapTemplate: LdapTemplate | ||
|
||
@Order(1) | ||
@Test | ||
fun `successfully updates ldap role`() { | ||
mockMvc.perform( | ||
MockMvcRequestBuilders.put("/users/john-smith/roles/pf_std_probation") | ||
.withOAuth2Token(wireMockServer) | ||
).andExpect(status().is2xxSuccessful).andReturn() | ||
|
||
val res = ldapTemplate.lookupContext( | ||
LdapNameBuilder.newInstance("ou=Users") | ||
.add("cn", "john-smith") | ||
.add("cn", DeliusRole.CTRBT001.name) | ||
.build() | ||
) | ||
assertThat(res.dn.toString(), equalTo("cn=CTRBT001,cn=john-smith,ou=Users")) | ||
} | ||
|
||
@Order(2) | ||
@Test | ||
fun `successfully removes ldap role`() { | ||
mockMvc.perform( | ||
MockMvcRequestBuilders.delete("/users/john-smith/roles/pf_std_probation") | ||
.withOAuth2Token(wireMockServer) | ||
).andExpect(status().is2xxSuccessful).andReturn() | ||
|
||
val res = assertThrows<NameNotFoundException> { | ||
ldapTemplate.lookupContext( | ||
LdapNameBuilder.newInstance("ou=Users") | ||
.add("cn", "john-smith") | ||
.add("cn", DeliusRole.CTRBT001.name) | ||
.build() | ||
) | ||
} | ||
assertThat(res.message, equalTo("[LDAP: error code 32 - Unable to perform the search because base entry 'cn=CTRBT001,cn=john-smith,ou=Users,dc=moj,dc=com' does not exist in the server.]")) | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
projects/pathfinder-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
...nder-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/IdentifierType.kt
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...nder-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/UserController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package uk.gov.justice.digital.hmpps.controller | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.security.access.prepost.PreAuthorize | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PutMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.server.ResponseStatusException | ||
import uk.gov.justice.digital.hmpps.model.DeliusRole | ||
import uk.gov.justice.digital.hmpps.service.UserService | ||
|
||
@RestController | ||
@RequestMapping("users") | ||
class UserController(private val userService: UserService) { | ||
@PreAuthorize("hasRole('PROBATION_API__PATHFINDER__USER_ROLES')") | ||
@PutMapping(value = ["/{username}/roles/{roleName}"]) | ||
fun addRole(@PathVariable username: String, @PathVariable roleName: String) = | ||
userService.addRole(username, roleName.deliusRole()) | ||
|
||
@PreAuthorize("hasRole('PROBATION_API__PATHFINDER__USER_ROLES')") | ||
@DeleteMapping(value = ["/{username}/roles/{roleName}"]) | ||
fun removeRole(@PathVariable username: String, @PathVariable roleName: String) = | ||
userService.removeRole(username, roleName.deliusRole()) | ||
} | ||
|
||
private fun String.deliusRole() = | ||
DeliusRole.from(this) ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Role Not Acceptable") |
14 changes: 14 additions & 0 deletions
14
...ts/pathfinder-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/DeliusRole.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package uk.gov.justice.digital.hmpps.model | ||
|
||
enum class DeliusRole(val description: String, val role: String) { | ||
CTRBT001("Pathfinder CT Probation", "PF_STD_PROBATION"), | ||
CTRBT002("Pathfinder CT Approval", "PF_APPROVAL"), | ||
CTRBT003("Pathfinder National Reader", "PF_NATIONAL_READER"), | ||
CTRBT004("Pathfinder HQ User", "PF_HQ"), | ||
CTRBT005("Pathfinder User", "PF_USER"), | ||
CTRBT006("Pathfinder Admin", "PF_ADMIN"); | ||
|
||
companion object { | ||
fun from(role: String): DeliusRole? = entries.firstOrNull { it.role == role.uppercase() } | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...pathfinder-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package uk.gov.justice.digital.hmpps.service | ||
|
||
import org.springframework.ldap.core.LdapTemplate | ||
import org.springframework.ldap.support.LdapNameBuilder | ||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.exception.NotFoundException | ||
import uk.gov.justice.digital.hmpps.model.DeliusRole | ||
import javax.naming.directory.Attributes | ||
import javax.naming.directory.BasicAttribute | ||
import javax.naming.directory.BasicAttributes | ||
|
||
@Service | ||
class UserService(private val ldapTemplate: LdapTemplate) { | ||
private val ldapBase = "ou=Users" | ||
|
||
fun addRole(username: String, role: DeliusRole) { | ||
val roleContext = ldapTemplate.lookupContext(role.context()) | ||
?: throw NotFoundException("NDeliusRole of ${role.name} not found") | ||
val attributes: Attributes = BasicAttributes(true).apply { | ||
put(roleContext.asAttribute("aliasedObjectName")) | ||
put(role.name.asAttribute("cn")) | ||
put(listOf("NDRoleAssociation", "Alias", "top").asAttribute("objectclass")) | ||
} | ||
val userRole = role.context(username) | ||
ldapTemplate.rebind(userRole, null, attributes) | ||
} | ||
|
||
fun removeRole(username: String, role: DeliusRole) = | ||
ldapTemplate.unbind(role.context(username)) | ||
|
||
private fun DeliusRole.context(username: String? = null) = | ||
LdapNameBuilder.newInstance(ldapBase) | ||
.add("cn", username ?: "ndRoleCatalogue") | ||
.add("cn", name) | ||
.build() | ||
|
||
fun Any.asAttribute(key: String) = BasicAttribute(key, this.toString()) | ||
fun List<Any>.asAttribute(key: String): BasicAttribute = | ||
BasicAttribute(key).apply { forEach(this::add) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters