Skip to content

Commit

Permalink
project.organizationName now defaults to the project.organization.nam…
Browse files Browse the repository at this point in the history
…e, and overwrites the field on get() to prevent inconsistencies. Only if there is no organization.name, the original field can be used.
  • Loading branch information
Bdegraaf1234 committed Nov 6, 2023
1 parent 86f4c45 commit 65e839d
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/gatling/scala/ProjectGatlingTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ProjectGatlingTest extends ManagementPortalSimulation {
.exec(http("Create new project")
.post("/api/projects")
.headers(headers_http_authenticated)
.body(StringBody("""{"id":null, "projectName":"PROJECT-${randstring}", "description":"SAMPLE_TEXT", "organizationName":"SAMPLE_TEXT", "location":"SAMPLE_TEXT", "startDate":"2020-01-01T00:00:00.000Z", "projectStatus":null, "endDate":"2020-01-01T00:00:00.000Z", "projectAdmin":null}""")).asJson
.body(StringBody("""{"id":null, "projectName":"PROJECT-${randstring}", "description":"SAMPLE_TEXT", "organization": {"id": 1, "name": "main" }, "location":"SAMPLE_TEXT", "startDate":"2020-01-01T00:00:00.000Z", "projectStatus":null, "endDate":"2020-01-01T00:00:00.000Z", "projectAdmin":null}""")).asJson
.check(status.is(201))
.check(headerRegex("Location", "(.*)").saveAs("new_project_url"))).exitHereIfFailed
.pause(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
open class AuthorizationConfiguration(
class AuthorizationConfiguration(
private val projectRepository: ProjectRepository,
) {
@Bean
open fun authorizationOracle(): AuthorizationOracle = MPAuthorizationOracle(
fun authorizationOracle(): AuthorizationOracle = MPAuthorizationOracle(
object : EntityRelationService {
override suspend fun findOrganizationOfProject(project: String): String? = withContext(Dispatchers.IO) {
projectRepository.findOneWithEagerRelationshipsByName(project)
?.organization?.name
?.organizationName
}
}
)
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/radarbase/management/domain/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,19 @@ class Project : AbstractEntity(), Serializable {
@Column(name = "description", nullable = false)
@NotNull var description: String? = null

@JvmField
// Defaults to organization name, but if that is not set then we can use the organizationName
@Column(name = "jhi_organization")
var organizationName: String? = null
get() {
if (organization?.name != null)
field = organization?.name
return field
}
// needed because the @JVMField annotation cannot be added when a custom getter/setter is set
set(value) {
field = value
}


@JvmField
@ManyToOne(fetch = FetchType.EAGER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ interface ProjectRepository : JpaRepository<Project, Long?>, RevisionRepository<
value = "select distinct project from Project project "
+ "left join fetch project.sourceTypes "
+ "WHERE project.projectName in (:projectNames) "
+ "OR project.organization.name in (:organizationNames)",
+ "OR project.organizationName in (:organizationNames)",
countQuery = "select distinct count(project) from Project project "
+ "WHERE project.projectName in (:projectNames) "
+ "OR project.organization.name in (:organizationNames)"
+ "OR project.organizationName in (:organizationNames)"
)
fun findAllWithEagerRelationshipsInOrganizationsOrProjects(
pageable: Pageable?,
Expand All @@ -40,7 +40,7 @@ interface ProjectRepository : JpaRepository<Project, Long?>, RevisionRepository<

@Query(
"select project from Project project "
+ "WHERE project.organization.name = :organization_name"
+ "WHERE project.organizationName = :organization_name"
)
fun findAllByOrganizationName(
@Param("organization_name") organizationName: String
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/radarbase/management/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import java.util.function.Function
*/
@Service
@Transactional
open class UserService @Autowired constructor(
class UserService @Autowired constructor(
private val userRepository: UserRepository,
private val passwordService: PasswordService,
private val userMapper: UserMapper,
Expand Down Expand Up @@ -205,7 +205,7 @@ open class UserService @Autowired constructor(
RoleAuthority.Scope.ORGANIZATION -> e.organization(role.organization?.name)
RoleAuthority.Scope.PROJECT -> {
if (role.project?.organization != null) {
e.organization(role.project?.organization?.name)
e.organization(role.project?.organizationName)
}
e.project(role.project?.projectName)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ open class UserService @Autowired constructor(
*/
@Transactional
@Throws(NotAuthorizedException::class)
open fun updateUser(userDto: UserDTO): UserDTO? {
fun updateUser(userDto: UserDTO): UserDTO? {
val userOpt = userDto.id?.let { userRepository.findById(it) }
return if (userOpt?.isPresent == true) {
var user = userOpt.get()
Expand Down Expand Up @@ -314,7 +314,7 @@ open class UserService @Autowired constructor(
* @param password the new password
* @param login of the user to change password
*/
open fun changePassword(login: String, password: String) {
fun changePassword(login: String, password: String) {
val user = userRepository.findOneByLogin(login)

if (user != null) {
Expand All @@ -331,7 +331,7 @@ open class UserService @Autowired constructor(
* @return the requested page of users
*/
@Transactional(readOnly = true)
open fun getAllManagedUsers(pageable: Pageable): Page<UserDTO> {
fun getAllManagedUsers(pageable: Pageable): Page<UserDTO> {
log.debug("Request to get all Users")
return userRepository.findAllByLoginNot(pageable, Constants.ANONYMOUS_USER)
.map { user: User? -> userMapper.userToUserDTO(user) }
Expand All @@ -344,12 +344,12 @@ open class UserService @Autowired constructor(
* and is empty otherwise
*/
@Transactional(readOnly = true)
open fun getUserWithAuthoritiesByLogin(login: String): UserDTO? {
fun getUserWithAuthoritiesByLogin(login: String): UserDTO? {
return userMapper.userToUserDTO(userRepository.findOneWithRolesByLogin(login))
}

@get:Transactional(readOnly = true)
open val userWithAuthorities: User?
val userWithAuthorities: User?
/**
* Get the current user.
* @return the currently authenticated user, or null if no user is currently authenticated
Expand Down Expand Up @@ -412,7 +412,7 @@ open class UserService @Autowired constructor(
*/
@Transactional
@Throws(NotAuthorizedException::class)
open fun updateRoles(login: String, roleDtos: Set<RoleDTO>?) {
fun updateRoles(login: String, roleDtos: Set<RoleDTO>?) {
val user = userRepository.findOneByLogin(login)
?: throw NotFoundException(
"User with login $login not found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class ProjectDTO : Serializable {
var humanReadableProjectName: String? = null
@NotNull var description: String? = null
var organization: OrganizationDTO? = null

// Defaults to organization name, but if that is not set then we can use the organizationName
var organizationName: String? = null
get() {
if (organization?.name != null)
field = organization?.name
return field
}


@NotNull var location: String? = null
var startDate: ZonedDateTime? = null
var projectStatus: ProjectStatus? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ abstract class ProjectMapperDecorator : ProjectMapper {
val project = delegate.projectDTOToProject(projectDto)
val projectName = projectDto.humanReadableProjectName
if (!projectName.isNullOrEmpty()) {
project!!.attributes[ProjectDTO.Companion.HUMAN_READABLE_PROJECT_NAME] = projectName
project!!.attributes[ProjectDTO.HUMAN_READABLE_PROJECT_NAME] = projectName
}

val name = projectDto.organization?.name
if (name != null) {
val name = projectDto.organizationName
if (name != null && projectDto.organization != null) {
val org = organizationRepository.findOneByName(name)
?: throw NotFoundException(
"Organization not found with name",
EntityName.Companion.ORGANIZATION,
EntityName.ORGANIZATION,
ErrorConstants.ERR_ORGANIZATION_NAME_NOT_FOUND,
Collections.singletonMap<String, String?>("name", name)
Collections.singletonMap("name", name)
)
project!!.organization = org
}
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/radarbase/management/web/rest/ProjectResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ class ProjectResource(
fun createProject(@RequestBody @Valid projectDto: ProjectDTO?): ResponseEntity<ProjectDTO> {
log.debug("REST request to save Project : {}", projectDto)
val org = projectDto?.organizationName
if (org == null) {
throw BadRequestException(
?: throw BadRequestException(
"Organization must be provided",
ENTITY_NAME, ErrorConstants.ERR_VALIDATION
)
}
authService.checkPermission(
Permission.PROJECT_CREATE,
{ e: EntityDetails -> e.organization(org) })
Expand Down Expand Up @@ -156,7 +154,7 @@ class ProjectResource(
e.organization(newOrgName)
e.project(existingProject?.projectName)
})
val oldOrgName = existingProject?.organization?.name
val oldOrgName = existingProject?.organizationName
if (newOrgName != oldOrgName) {
authService.checkPermission(
Permission.PROJECT_UPDATE,
Expand Down Expand Up @@ -210,7 +208,7 @@ class ProjectResource(
log.debug("REST request to get Project : {}", projectName)
val projectDto = projectService.findOneByName(projectName!!)
authService.checkPermission(Permission.PROJECT_READ, { e: EntityDetails ->
e.organization(projectDto.organization?.name)
e.organization(projectDto.organizationName)
e.project(projectDto.projectName)
})
return ResponseEntity.ok(projectDto)
Expand All @@ -233,7 +231,7 @@ class ProjectResource(
log.debug("REST request to get Project : {}", projectName)
val projectDto = projectService.findOneByName(projectName!!)
authService.checkPermission(Permission.PROJECT_READ, { e: EntityDetails ->
e.organization(projectDto.organization?.name)
e.organization(projectDto.organizationName)
e.project(projectDto.projectName)
})
return projectService.findSourceTypesByProjectId(projectDto.id!!)
Expand All @@ -255,7 +253,7 @@ class ProjectResource(
log.debug("REST request to delete Project : {}", projectName)
val projectDto = projectService.findOneByName(projectName!!)
authService.checkPermission(Permission.PROJECT_DELETE, { e: EntityDetails ->
e.organization(projectDto.organization?.name)
e.organization(projectDto.organizationName)
e.project(projectDto.projectName)
})
return try {
Expand Down Expand Up @@ -284,7 +282,7 @@ class ProjectResource(
log.debug("REST request to get all Roles for project {}", projectName)
val projectDto = projectService.findOneByName(projectName!!)
authService.checkPermission(Permission.ROLE_READ, { e: EntityDetails ->
e.organization(projectDto.organization?.name)
e.organization(projectDto.organizationName)
e.project(projectDto.projectName)
})
return ResponseEntity.ok(roleService.getRolesByProject(projectName))
Expand All @@ -311,7 +309,7 @@ class ProjectResource(
val projectDto = projectService.findOneByName(projectName) //?: throw NoSuchElementException()

authService.checkPermission(Permission.SOURCE_READ, { e: EntityDetails ->
e.organization(projectDto.organization?.name)
e.organization(projectDto.organizationName)
e.project(projectDto.projectName)
})
return if (assigned != null) {
Expand Down Expand Up @@ -375,7 +373,7 @@ class ProjectResource(
// this checks if the project exists
val projectDto = projectName.let { projectService.findOneByName(it) }
authService.checkPermission(Permission.SUBJECT_READ, { e: EntityDetails ->
e.organization(projectDto.organization?.name)
e.organization(projectDto.organizationName)
e.project(projectDto.projectName)
})

Expand Down
Loading

0 comments on commit 65e839d

Please sign in to comment.