diff --git a/src/main/java/de/samply/app/ProjectManagerConst.java b/src/main/java/de/samply/app/ProjectManagerConst.java index dbb294e..83c73b1 100644 --- a/src/main/java/de/samply/app/ProjectManagerConst.java +++ b/src/main/java/de/samply/app/ProjectManagerConst.java @@ -101,6 +101,7 @@ public class ProjectManagerConst { public final static String FETCH_DATASHIELD_STATUS_ACTION = "FETCH_DATASHIELD_STATUS"; public final static String FETCH_USERS_FOR_AUTOCOMPLETE_ACTION = "FETCH_USERS_FOR_AUTOCOMPLETE"; public final static String FETCH_PROJECT_USERS_ACTION = "FETCH_PROJECT_USERS"; + public final static String FETCH_CURRENT_USER_ACTION = "FETCH_CURRENT_USER"; public final static String EXIST_INVITED_USERS_ACTION = "EXIST_INVITED_USERS"; public final static String FETCH_PROJECT_CONFIGURATIONS_ACTION = "FETCH_PROJECT_CONFIGURATIONS"; public final static String EXISTS_AUTHENTICATION_SCRIPT_ACTION = "EXISTS_AUTHENTICATION_SCRIPT"; @@ -189,6 +190,7 @@ public class ProjectManagerConst { public final static String FETCH_ALL_REGISTERED_BRIDGEHEADS = "/bridgeheads"; public final static String FETCH_USERS_FOR_AUTOCOMPLETE = "/autocomplete-users"; public final static String FETCH_PROJECT_USERS = "/project-users"; + public final static String FETCH_CURRENT_USER = "/current-user"; public final static String EXIST_INVITED_USERS = "/exist-invited-users"; diff --git a/src/main/java/de/samply/app/ProjectManagerController.java b/src/main/java/de/samply/app/ProjectManagerController.java index e9b5e73..ec42932 100644 --- a/src/main/java/de/samply/app/ProjectManagerController.java +++ b/src/main/java/de/samply/app/ProjectManagerController.java @@ -186,7 +186,7 @@ public ResponseEntity fetchProjectsBridgeheads( return convertToResponseEntity(() -> projectBridgeheadService.fetchProjectBridgeheads(projectCode)); } - @RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.PROJECT_MANAGER_ADMIN}) + @RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.BRIDGEHEAD_ADMIN, ProjectRole.PROJECT_MANAGER_ADMIN}) @StateConstraints(projectStates = {ProjectState.DEVELOP}) @ProjectConstraints(projectTypes = {ProjectType.DATASHIELD, ProjectType.RESEARCH_ENVIRONMENT}) @EmailSender(templateType = EmailTemplateType.INVITATION, recipients = {EmailRecipientType.EMAIL_ANNOTATION}) @@ -204,7 +204,7 @@ public ResponseEntity setUserAsDeveloper( this.userService.setProjectBridgheadUserWithRoleAndGenerateTokensIfDataShield(email, projectCode, bridgehead, ProjectRole.DEVELOPER)); } - @RoleConstraints(organisationRoles = {OrganisationRole.PROJECT_MANAGER_ADMIN}) + @RoleConstraints(projectRoles = {ProjectRole.BRIDGEHEAD_ADMIN, ProjectRole.PROJECT_MANAGER_ADMIN}) @StateConstraints(projectStates = {ProjectState.PILOT}) @ProjectConstraints(projectTypes = {ProjectType.DATASHIELD, ProjectType.RESEARCH_ENVIRONMENT}) @EmailSender(templateType = EmailTemplateType.INVITATION, recipients = {EmailRecipientType.EMAIL_ANNOTATION}) @@ -353,7 +353,6 @@ public ResponseEntity fetchProjectRoles( @ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode, @Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD, required = false) String bridgehead ) { - //TODO return convertToResponseEntity(() -> userService.fetchProjectRoles(projectCode, Optional.ofNullable(bridgehead))); } @@ -1159,7 +1158,7 @@ public ResponseEntity fetchAllRegisteredBridgeheads() { return convertToResponseEntity(() -> bridgeheadConfiguration.getRegisteredBridgeheads().stream().map(dtoFactory::convertToBridgehead).collect(Collectors.toSet())); } - @RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.PROJECT_MANAGER_ADMIN}) + @RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.BRIDGEHEAD_ADMIN, ProjectRole.PROJECT_MANAGER_ADMIN}) @StateConstraints(projectStates = {ProjectState.DEVELOP, ProjectState.PILOT, ProjectState.FINAL}) @FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE) @FrontendAction(action = ProjectManagerConst.FETCH_USERS_FOR_AUTOCOMPLETE_ACTION) @@ -1172,20 +1171,31 @@ public ResponseEntity fetchUsersForAutocomplete( return convertToResponseEntity(() -> this.userService.fetchUsersForAutocomplete(projectCode, partialEmail, bridgehead)); } - @RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.PROJECT_MANAGER_ADMIN}) + @RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.BRIDGEHEAD_ADMIN, ProjectRole.PROJECT_MANAGER_ADMIN}) @StateConstraints(projectStates = {ProjectState.DEVELOP, ProjectState.PILOT, ProjectState.FINAL}) @FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE) @FrontendAction(action = ProjectManagerConst.FETCH_PROJECT_USERS_ACTION) @GetMapping(value = ProjectManagerConst.FETCH_PROJECT_USERS) public ResponseEntity fetchProjectUsers( - // Project Code Required for state constraints @ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode, @Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead ) { return convertToResponseEntity(() -> this.userService.fetchProjectUsers(projectCode, bridgehead)); } - @RoleConstraints(projectRoles = {ProjectRole.BRIDGEHEAD_ADMIN}) + @RoleConstraints(projectRoles = {ProjectRole.DEVELOPER, ProjectRole.PILOT, ProjectRole.FINAL}) + @StateConstraints(projectStates = {ProjectState.DEVELOP, ProjectState.PILOT, ProjectState.FINAL}) + @FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE) + @FrontendAction(action = ProjectManagerConst.FETCH_CURRENT_USER_ACTION) + @GetMapping(value = ProjectManagerConst.FETCH_CURRENT_USER) + public ResponseEntity fetchCurrentUser( + @ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode, + @Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead + ) { + return convertOptionalToResponseEntity(() -> this.userService.fetchCurrentUser(projectCode, bridgehead)); + } + + @RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.BRIDGEHEAD_ADMIN, ProjectRole.PROJECT_MANAGER_ADMIN}) @StateConstraints(projectStates = {ProjectState.DEVELOP, ProjectState.PILOT, ProjectState.FINAL}) @FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE) @FrontendAction(action = ProjectManagerConst.EXIST_INVITED_USERS_ACTION) @@ -1207,6 +1217,19 @@ private ResponseEntity convertToResponseEntity(RunnableWithException runnable) { } } + private ResponseEntity convertOptionalToResponseEntity(SupplierWithException> supplier) { + try { + Optional result = supplier.get(); + if (result.isEmpty()) { + return ResponseEntity.notFound().build(); + } else { + return ResponseEntity.ok(objectMapper.writeValueAsString(result.get())); + } + } catch (Exception e) { + return createInternalServerError(e); + } + } + private ResponseEntity convertToResponseEntity(SupplierWithException supplier) { try { T result = supplier.get(); diff --git a/src/main/java/de/samply/db/repository/ProjectBridgeheadUserRepository.java b/src/main/java/de/samply/db/repository/ProjectBridgeheadUserRepository.java index 80658bd..9929531 100644 --- a/src/main/java/de/samply/db/repository/ProjectBridgeheadUserRepository.java +++ b/src/main/java/de/samply/db/repository/ProjectBridgeheadUserRepository.java @@ -48,6 +48,17 @@ public interface ProjectBridgeheadUserRepository extends JpaRepository getDistinctByEmailContainingAndProjectBridgehead_Bridgehead(String email, String bridgehead); + @Query("SELECT pbu FROM ProjectBridgeheadUser pbu WHERE pbu.projectBridgehead = :projectBridgehead AND pbu.email = :email AND (" + + "(pbu.projectBridgehead.project.state = 'DEVELOP' AND pbu.projectRole = 'DEVELOPER') OR " + + "(pbu.projectBridgehead.project.state = 'PILOT' AND pbu.projectRole = 'PILOT') OR " + + "(pbu.projectBridgehead.project.state = 'FINAL' AND pbu.projectRole = 'FINAL')) " + + "ORDER BY pbu.id ASC") + Optional getFirstValidByEmailAndProjectBridgehead(String email, ProjectBridgehead projectBridgehead); + + @Query("SELECT DISTINCT pbu FROM ProjectBridgeheadUser pbu WHERE pbu.projectBridgehead = :projectBridgehead AND pbu.projectRole = :role AND (" + + "(pbu.projectBridgehead.project.state = 'DEVELOP' AND pbu.projectRole = 'DEVELOPER') OR " + + "(pbu.projectBridgehead.project.state = 'PILOT' AND pbu.projectRole = 'PILOT') OR " + + "(pbu.projectBridgehead.project.state = 'FINAL' AND pbu.projectRole = 'FINAL'))") List getDistinctByProjectRoleAndProjectBridgehead(ProjectRole role, ProjectBridgehead projectBridgehead); @Query("SELECT DISTINCT pbu FROM ProjectBridgeheadUser pbu WHERE pbu.projectBridgehead.project.code = :projectCode AND pbu.projectRole = :projectRole") diff --git a/src/main/java/de/samply/user/UserService.java b/src/main/java/de/samply/user/UserService.java index 440456b..26fad57 100644 --- a/src/main/java/de/samply/user/UserService.java +++ b/src/main/java/de/samply/user/UserService.java @@ -8,6 +8,7 @@ import de.samply.notification.OperationType; import de.samply.project.state.UserProjectState; import de.samply.security.SessionUser; +import de.samply.user.roles.OrganisationRole; import de.samply.user.roles.OrganisationRoleToProjectRoleMapper; import de.samply.user.roles.ProjectRole; import de.samply.user.roles.UserProjectRoles; @@ -158,6 +159,11 @@ public Set fetchUsersForAutocomplete(@NotNull String projectCode, @NotNull return allUsers; } + public Optional fetchCurrentUser(@NotNull String projectCode, @NotNull String bridgehead){ + Optional user = this.projectBridgeheadUserRepository.getFirstValidByEmailAndProjectBridgehead(sessionUser.getEmail(), fetchProjectBridgehead(projectCode, bridgehead)); + return (user.isEmpty()) ? Optional.empty() : Optional.ofNullable(dtoFactory.convert(user.get())); + } + public Set fetchProjectUsers(@NotNull String projectCode, @NotNull String bridgehead) throws UserServiceException { ProjectBridgehead projectBridgehead = fetchProjectBridgehead(projectCode, bridgehead); return (switch (projectBridgehead.getProject().getState()) { @@ -176,17 +182,21 @@ public boolean existInvatedUsers(@NotNull String projectCode, @NotNull String br } private ProjectBridgehead fetchProjectBridgehead(String projectCode, String bridgehead) throws UserServiceException { - Optional project = projectRepository.findByCode(projectCode); - if (project.isEmpty()) { - throw new UserServiceException("Project " + projectCode + " not found"); - } - Optional projectBridgehead = projectBridgeheadRepository.findFirstByBridgeheadAndProject(bridgehead, project.get()); + Optional projectBridgehead = projectBridgeheadRepository.findFirstByBridgeheadAndProject(bridgehead, fetchProject(projectCode)); if (projectBridgehead.isEmpty()) { throw new UserServiceException("Project " + projectCode + " for bridgehead " + bridgehead + " not found"); } return projectBridgehead.get(); } + private Project fetchProject(@NotNull String projectCode) throws UserServiceException { + Optional project = projectRepository.findByCode(projectCode); + if (project.isEmpty()) { + throw new UserServiceException("Project " + projectCode + " not found"); + } + return project.get(); + } + public Set fetchProjectRoles(@NotNull String projectCode, Optional bridgehead) throws UserServiceException { Optional userProjectRoles = organisationRoleToProjectRoleMapper.map(projectCode); if (userProjectRoles.isEmpty()) { diff --git a/src/main/java/de/samply/user/UserServiceException.java b/src/main/java/de/samply/user/UserServiceException.java index 45dabba..acabcf8 100644 --- a/src/main/java/de/samply/user/UserServiceException.java +++ b/src/main/java/de/samply/user/UserServiceException.java @@ -1,6 +1,6 @@ package de.samply.user; -public class UserServiceException extends Exception{ +public class UserServiceException extends RuntimeException{ public UserServiceException() { }