Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/test user service #19

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/de/samply/app/ProjectManagerConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";


Expand Down
37 changes: 30 additions & 7 deletions src/main/java/de/samply/app/ProjectManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public ResponseEntity<String> 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})
Expand All @@ -204,7 +204,7 @@ public ResponseEntity<String> 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})
Expand Down Expand Up @@ -353,7 +353,6 @@ public ResponseEntity<String> 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)));
}

Expand Down Expand Up @@ -1159,7 +1158,7 @@ public ResponseEntity<Resource> 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)
Expand All @@ -1172,20 +1171,31 @@ public ResponseEntity<String> 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<String> 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<String> 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)
Expand All @@ -1207,6 +1217,19 @@ private ResponseEntity convertToResponseEntity(RunnableWithException runnable) {
}
}

private <T> ResponseEntity convertOptionalToResponseEntity(SupplierWithException<Optional<T>> supplier) {
try {
Optional<T> 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 <T> ResponseEntity convertToResponseEntity(SupplierWithException<T> supplier) {
try {
T result = supplier.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public interface ProjectBridgeheadUserRepository extends JpaRepository<ProjectBr

List<ProjectBridgeheadUser> 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<ProjectBridgeheadUser> 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<ProjectBridgeheadUser> getDistinctByProjectRoleAndProjectBridgehead(ProjectRole role, ProjectBridgehead projectBridgehead);

@Query("SELECT DISTINCT pbu FROM ProjectBridgeheadUser pbu WHERE pbu.projectBridgehead.project.code = :projectCode AND pbu.projectRole = :projectRole")
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/de/samply/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -158,6 +159,11 @@ public Set<User> fetchUsersForAutocomplete(@NotNull String projectCode, @NotNull
return allUsers;
}

public Optional<User> fetchCurrentUser(@NotNull String projectCode, @NotNull String bridgehead){
Optional<ProjectBridgeheadUser> user = this.projectBridgeheadUserRepository.getFirstValidByEmailAndProjectBridgehead(sessionUser.getEmail(), fetchProjectBridgehead(projectCode, bridgehead));
return (user.isEmpty()) ? Optional.empty() : Optional.ofNullable(dtoFactory.convert(user.get()));
}

public Set<User> fetchProjectUsers(@NotNull String projectCode, @NotNull String bridgehead) throws UserServiceException {
ProjectBridgehead projectBridgehead = fetchProjectBridgehead(projectCode, bridgehead);
return (switch (projectBridgehead.getProject().getState()) {
Expand All @@ -176,17 +182,21 @@ public boolean existInvatedUsers(@NotNull String projectCode, @NotNull String br
}

private ProjectBridgehead fetchProjectBridgehead(String projectCode, String bridgehead) throws UserServiceException {
Optional<Project> project = projectRepository.findByCode(projectCode);
if (project.isEmpty()) {
throw new UserServiceException("Project " + projectCode + " not found");
}
Optional<ProjectBridgehead> projectBridgehead = projectBridgeheadRepository.findFirstByBridgeheadAndProject(bridgehead, project.get());
Optional<ProjectBridgehead> 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> project = projectRepository.findByCode(projectCode);
if (project.isEmpty()) {
throw new UserServiceException("Project " + projectCode + " not found");
}
return project.get();
}

public Set<ProjectRole> fetchProjectRoles(@NotNull String projectCode, Optional<String> bridgehead) throws UserServiceException {
Optional<UserProjectRoles> userProjectRoles = organisationRoleToProjectRoleMapper.map(projectCode);
if (userProjectRoles.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/samply/user/UserServiceException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.samply.user;

public class UserServiceException extends Exception{
public class UserServiceException extends RuntimeException{

public UserServiceException() {
}
Expand Down
Loading