-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MODCONSKC-19] - Implement sharing of authorization roles
- Loading branch information
Showing
17 changed files
with
877 additions
and
11 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
39 changes: 39 additions & 0 deletions
39
src/main/java/org/folio/consortia/controller/SharingRoleController.java
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,39 @@ | ||
package org.folio.consortia.controller; | ||
|
||
import static org.springframework.http.HttpStatus.CREATED; | ||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
import java.util.UUID; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.folio.consortia.domain.dto.SharingRoleDeleteResponse; | ||
import org.folio.consortia.domain.dto.SharingRoleRequest; | ||
import org.folio.consortia.domain.dto.SharingRoleResponse; | ||
import org.folio.consortia.rest.resource.RolesApi; | ||
import org.folio.consortia.service.impl.SharingRoleService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/consortia/{consortiumId}/sharing") | ||
@RequiredArgsConstructor | ||
public class SharingRoleController implements RolesApi { | ||
|
||
private final SharingRoleService sharingRoleService; | ||
|
||
@Override | ||
public ResponseEntity<SharingRoleResponse> startSharingRole(UUID consortiumId, SharingRoleRequest sharingRoleRequest) { | ||
return ResponseEntity | ||
.status(CREATED) | ||
.body(sharingRoleService.start(consortiumId, sharingRoleRequest)); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<SharingRoleDeleteResponse> deleteSharingRole(UUID consortiumId, UUID roleId, | ||
SharingRoleRequest sharingRoleRequest) { | ||
return ResponseEntity | ||
.status(OK) | ||
.body(sharingRoleService.delete(consortiumId, roleId, sharingRoleRequest)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/folio/consortia/domain/entity/SharingRoleEntity.java
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,41 @@ | ||
package org.folio.consortia.domain.entity; | ||
|
||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.folio.consortia.domain.entity.base.AuditableEntity; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@RequiredArgsConstructor | ||
@Entity | ||
@Table(name = "sharing_role") | ||
public class SharingRoleEntity extends AuditableEntity { | ||
@Id | ||
private UUID id; | ||
private UUID roleId; | ||
private String tenantId; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
SharingRoleEntity that = (SharingRoleEntity) o; | ||
return Objects.equals(id, that.id) | ||
&& Objects.equals(roleId, that.roleId) | ||
&& Objects.equals(tenantId, that.tenantId); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, roleId, tenantId); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/folio/consortia/repository/SharingRoleRepository.java
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,21 @@ | ||
package org.folio.consortia.repository; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import org.folio.consortia.domain.entity.SharingRoleEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface SharingRoleRepository extends JpaRepository<SharingRoleEntity, UUID> { | ||
|
||
@Query("SELECT sr.tenantId FROM SharingRoleEntity sr WHERE sr.roleId = ?1") | ||
Set<String> findTenantsByRoleId(UUID roleId); | ||
|
||
boolean existsByRoleId(UUID roleId); | ||
|
||
@Modifying | ||
@Query("DELETE FROM SharingRoleEntity sr WHERE sr.roleId = ?1") | ||
void deleteByRoleId(UUID roleId); | ||
} |
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
119 changes: 119 additions & 0 deletions
119
src/main/java/org/folio/consortia/service/impl/SharingRoleService.java
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,119 @@ | ||
package org.folio.consortia.service.impl; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.commons.lang3.ObjectUtils; | ||
import org.folio.consortia.domain.dto.PublicationRequest; | ||
import org.folio.consortia.domain.dto.SharingRoleDeleteResponse; | ||
import org.folio.consortia.domain.dto.SharingRoleRequest; | ||
import org.folio.consortia.domain.dto.SharingRoleResponse; | ||
import org.folio.consortia.domain.entity.SharingRoleEntity; | ||
import org.folio.consortia.exception.ResourceNotFoundException; | ||
import org.folio.consortia.repository.SharingRoleRepository; | ||
import org.folio.consortia.service.BaseSharingService; | ||
import org.folio.consortia.service.ConsortiumService; | ||
import org.folio.consortia.service.PublicationService; | ||
import org.folio.consortia.service.TenantService; | ||
import org.folio.spring.FolioExecutionContext; | ||
import org.folio.spring.service.SystemUserScopedExecutionService; | ||
import org.springframework.core.task.TaskExecutor; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Log4j2 | ||
public class SharingRoleService extends BaseSharingService<SharingRoleRequest, SharingRoleResponse, SharingRoleDeleteResponse, SharingRoleEntity> { | ||
|
||
private final SharingRoleRepository sharingRoleRepository; | ||
|
||
public SharingRoleService(TenantService tenantService, | ||
ConsortiumService consortiumService, | ||
SystemUserScopedExecutionService systemUserScopedExecutionService, | ||
PublicationService publicationService, FolioExecutionContext folioExecutionContext, | ||
ObjectMapper objectMapper, TaskExecutor asyncTaskExecutor, | ||
SharingRoleRepository sharingRoleRepository) { | ||
super(tenantService, consortiumService, systemUserScopedExecutionService, publicationService, folioExecutionContext, objectMapper, asyncTaskExecutor); | ||
this.sharingRoleRepository = sharingRoleRepository; | ||
} | ||
|
||
@Override | ||
protected UUID getConfigId(SharingRoleRequest sharingRoleRequest) { | ||
return sharingRoleRequest.getRoleId(); | ||
} | ||
|
||
@Override | ||
protected Object getPayload(SharingRoleRequest sharingRoleRequest) { | ||
return sharingRoleRequest.getPayload(); | ||
} | ||
|
||
@Override | ||
protected void validateSharingConfigRequestOrThrow(UUID roleId, SharingRoleRequest sharingRoleRequest) { | ||
if (ObjectUtils.notEqual(getConfigId(sharingRoleRequest), roleId)) { | ||
throw new IllegalArgumentException("Mismatch id in path to roleId in request body"); | ||
} | ||
if (Objects.isNull(getPayload(sharingRoleRequest))) { | ||
throw new IllegalArgumentException("Payload must not be null"); | ||
} | ||
if (!sharingRoleRepository.existsByRoleId(roleId)) { | ||
throw new ResourceNotFoundException("roleId", String.valueOf(roleId)); | ||
} | ||
} | ||
|
||
@Override | ||
protected Set<String> findTenantsByConfigId(UUID roleId) { | ||
return sharingRoleRepository.findTenantsByRoleId(roleId); | ||
} | ||
|
||
@Override | ||
protected void saveSharingConfig(List<SharingRoleEntity> sharingRoleEntityList) { | ||
sharingRoleRepository.saveAll(sharingRoleEntityList); | ||
} | ||
|
||
@Override | ||
protected void deleteSharingConfig(UUID roleId) { | ||
sharingRoleRepository.deleteByRoleId(roleId); | ||
} | ||
|
||
@Override | ||
protected PublicationRequest createPublicationRequest(SharingRoleRequest sharingRoleRequest, String httpMethod) { | ||
PublicationRequest publicationRequest = new PublicationRequest(); | ||
publicationRequest.setMethod(httpMethod); | ||
String url = sharingRoleRequest.getUrl(); | ||
if (httpMethod.equals(HttpMethod.PUT.toString()) || httpMethod.equals(HttpMethod.DELETE.toString())) { | ||
url += "/" + getConfigId(sharingRoleRequest); | ||
} | ||
publicationRequest.setUrl(url); | ||
publicationRequest.setPayload(getPayload(sharingRoleRequest)); | ||
publicationRequest.setTenants(new HashSet<>()); | ||
return publicationRequest; | ||
} | ||
|
||
@Override | ||
protected SharingRoleEntity createSharingConfigEntityFromRequest(SharingRoleRequest sharingRoleRequest, String tenantId) { | ||
SharingRoleEntity sharingRoleEntity = new SharingRoleEntity(); | ||
sharingRoleEntity.setId(UUID.randomUUID()); | ||
sharingRoleEntity.setRoleId(sharingRoleEntity.getRoleId()); | ||
sharingRoleEntity.setTenantId(tenantId); | ||
return sharingRoleEntity; | ||
} | ||
|
||
@Override | ||
protected SharingRoleResponse createSharingConfigResponse(UUID createRolesPcId, UUID updateRolesPcId) { | ||
return new SharingRoleResponse() | ||
.createRolesPCId(createRolesPcId) | ||
.updateRolesPCId(updateRolesPcId); | ||
} | ||
|
||
@Override | ||
protected SharingRoleDeleteResponse createSharingConfigResponse(UUID publishRequestId) { | ||
return new SharingRoleDeleteResponse() | ||
.pcId(publishRequestId); | ||
} | ||
} |
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
Oops, something went wrong.