Skip to content

Commit

Permalink
[MODCONSKC-19] - Implement sharing of authorization roles
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Aug 13, 2024
1 parent 797e81d commit 184e78f
Show file tree
Hide file tree
Showing 17 changed files with 877 additions and 11 deletions.
50 changes: 48 additions & 2 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
{
"id": "inventory",
"version": "13.0"
},
{
"id": "roles",
"version": "1.1"
},
{
"id": "policies",
"version": "1.1"
}
],
"optional": [
Expand Down Expand Up @@ -360,6 +368,26 @@
"consortia.sharing-policies.item.delete"
],
"modulePermissions": []
},
{
"methods": [
"POST"
],
"pathPattern": "/consortia/{consortiumId}/sharing/roles",
"permissionsRequired": [
"consortia.sharing-roles.item.post"
],
"modulePermissions": []
},
{
"methods": [
"DELETE"
],
"pathPattern": "/consortia/{consortiumId}/sharing/roles/{rolesId}",
"permissionsRequired": [
"consortia.sharing-roles.item.delete"
],
"modulePermissions": []
}
]
},
Expand Down Expand Up @@ -433,7 +461,9 @@
"consortia.sharing-settings.item.post",
"consortia.sharing-settings.item.delete",
"consortia.sharing-policies.item.post",
"consortia.sharing-policies.item.delete"
"consortia.sharing-policies.item.delete",
"consortia.sharing-roles.item.post",
"consortia.sharing-roles.item.delete"
]
},
{
Expand Down Expand Up @@ -572,6 +602,16 @@
"permissionName": "consortia.sharing-policies.item.delete",
"displayName": "delete sharing policy",
"description": "Delete sharing policy"
},
{
"permissionName": "consortia.sharing-roles.item.post",
"displayName": "post sharing role",
"description": "Create sharing roles"
},
{
"permissionName": "consortia.sharing-roles.item.delete",
"displayName": "delete sharing role",
"description": "Delete sharing role"
}
],
"metadata": {
Expand Down Expand Up @@ -667,7 +707,13 @@
"user-settings.custom-fields.item.post",
"user-settings.custom-fields.collection.get",
"ui-users.editperms",
"capabilities.collection.get"
"capabilities.collection.get",
"roles.item.post",
"roles.item.put",
"roles.item.delete",
"policies.item.post",
"policies.item.put",
"policies.item.delete"
]
}
},
Expand Down
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
<sharing_instances.yaml.file>${project.basedir}/src/main/resources/swagger.api/sharing_instances.yaml</sharing_instances.yaml.file>
<sharing_settings.yaml.file>${project.basedir}/src/main/resources/swagger.api/sharing_settings.yaml</sharing_settings.yaml.file>
<sharing_policies.yaml.file>${project.basedir}/src/main/resources/swagger.api/sharing_policies.yaml</sharing_policies.yaml.file>
<sharing_roles.yaml.file>${project.basedir}/src/main/resources/swagger.api/sharing_roles.yaml</sharing_roles.yaml.file>


<!-- runtime dependencies -->
<folio-spring-base.version>8.1.2</folio-spring-base.version>
Expand Down Expand Up @@ -559,6 +561,32 @@
</configOptions>
</configuration>
</execution>
<execution>
<id>sharing roles</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${sharing_roles.yaml.file}</inputSpec>
<output>${project.build.directory}/generated-sources</output>
<generatorName>spring</generatorName>
<modelPackage>${project.groupId}.consortia.domain.dto</modelPackage>
<apiPackage>${project.groupId}.consortia.rest.resource</apiPackage>
<generateApis>true</generateApis>
<generateApiTests>true</generateApiTests>
<generateApiDocumentation>true</generateApiDocumentation>
<generateModels>true</generateModels>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>true</generateSupportingFiles>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<generateModelDocumentation>true</generateModelDocumentation>
<configOptions>
<dateLibrary>java</dateLibrary>
<interfaceOnly>true</interfaceOnly>
<useSpringBoot3>true</useSpringBoot3>
</configOptions>
</configuration>
</execution>
<execution>
<id>publish_coordinator</id>
<goals>
Expand Down
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));
}
}
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);
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ protected PublicationRequest createPublicationRequest(SharingPolicyRequest shari
publicationRequest.setMethod(httpMethod);
String url = sharingPolicyRequest.getUrl();
if (httpMethod.equals(HttpMethod.PUT.toString()) || httpMethod.equals(HttpMethod.DELETE.toString())) {
url += "/" + sharingPolicyRequest.getPolicyId();
url += "/" + getConfigId(sharingPolicyRequest);
}
publicationRequest.setUrl(url);
publicationRequest.setPayload(sharingPolicyRequest.getPayload());
publicationRequest.setPayload(getPayload(sharingPolicyRequest));
publicationRequest.setTenants(new HashSet<>());
return publicationRequest;
}
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/org/folio/consortia/service/impl/SharingRoleService.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ protected PublicationRequest createPublicationRequest(SharingSettingRequest shar
publicationRequest.setMethod(httpMethod);
String url = sharingSettingRequest.getUrl();
if (httpMethod.equals(HttpMethod.PUT.toString()) || httpMethod.equals(HttpMethod.DELETE.toString())) {
url += "/" + sharingSettingRequest.getSettingId();
url += "/" + getConfigId(sharingSettingRequest);
}
publicationRequest.setUrl(url);
publicationRequest.setPayload(sharingSettingRequest.getPayload());
publicationRequest.setPayload(getPayload(sharingSettingRequest));
publicationRequest.setTenants(new HashSet<>());
return publicationRequest;
}
Expand Down
Loading

0 comments on commit 184e78f

Please sign in to comment.