Skip to content

Commit

Permalink
Handle starred+other permisson on same project #823
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Jun 30, 2023
1 parent d00a093 commit ae57fbc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
21 changes: 14 additions & 7 deletions grails-app/services/au/org/ala/ecodata/ParatooService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,23 @@ class ParatooService {
// If the permission has been set as a favourite then delegate to the Hub permission
// so that "readOnly" access for a hub is supported, and we don't return projects that a user
// has only marked as starred without having hub level permissions
Map projectAccessLevel = permissions?.collectEntries { UserPermission permission ->
Map projectAccessLevels = [:]
permissions?.each { UserPermission permission ->
String projectId = permission.entityId
// Don't override an existing permission with a starred permission
if (permission.accessLevel == AccessLevel.starred) {
permission = permissionService.findParentPermission(permission)
if (!projectAccessLevels[projectId]) {
permission = permissionService.findParentPermission(permission)
projectAccessLevels[projectId] = permission?.accessLevel
}
}
else {
// Update the map of projectId to accessLevel
projectAccessLevels[projectId] = permission.accessLevel
}
// Return a Map of projectId to accessLevel
[(projectId):permission?.accessLevel]
}

List projects = Project.findAllByProjectIdInListAndStatus(new ArrayList(projectAccessLevel.keySet()), Status.ACTIVE)
List projects = Project.findAllByProjectIdInListAndStatus(new ArrayList(projectAccessLevels.keySet()), Status.ACTIVE)

// Filter projects that aren't in a program configured to support paratoo or don't have permission
projects = projects.findAll { Project project ->
Expand All @@ -89,12 +96,12 @@ class ParatooService {

Program program = Program.findByProgramId(project.programId)
Map config = program.getInheritedConfig()
config?.get(PROGRAM_CONFIG_PARATOO_ITEM) && projectAccessLevel[project.projectId]
config?.get(PROGRAM_CONFIG_PARATOO_ITEM) && projectAccessLevels[project.projectId]
}

List paratooProjects = projects.collect { Project project ->
List<Site> sites = siteService.sitesForProject(project.projectId)
AccessLevel accessLevel = projectAccessLevel[project.projectId]
AccessLevel accessLevel = projectAccessLevels[project.projectId]
mapProject(project, accessLevel, sites)
}
paratooProjects
Expand Down
15 changes: 15 additions & 0 deletions src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ class ParatooServiceSpec extends Specification implements ServiceUnitTest<Parato
projects.size() == 1
}

void "If the user has starred a project and also has a role, the role will be used"() {

setup:
UserPermission userPermission = new UserPermission(userId:userId, entityId:'p1', entityType: 'au.org.ala.ecodata.Project', accessLevel:AccessLevel.starred)
userPermission.save(flush:true, failOnError:true)

when:
List<ParatooProject> projects = service.userProjects(userId)

then:
projects.size() == 1
projects[0].accessLevel == AccessLevel.admin

}

void "The service can create a data set from a submitted collection"() {
setup:
ParatooProtocolId protocol = new ParatooProtocolId(id:1, version: 1)
Expand Down

0 comments on commit ae57fbc

Please sign in to comment.