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/visa permission api #690

Merged
merged 7 commits into from
May 4, 2023
92 changes: 92 additions & 0 deletions src/main/java/bio/overture/ego/controller/VisaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.UUID;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -29,17 +31,21 @@ public class VisaController {
/** Dependencies */
private final VisaService visaService;

private final VisaPermissionService visaPermissionService;

private final UserPermissionService userPermissionService;
private final GroupPermissionService groupPermissionService;
private final ApplicationPermissionService applicationPermissionService;

@Autowired
public VisaController(
@NonNull VisaService visaService,
@NotNull VisaPermissionService visaPermissionService,
@NonNull UserPermissionService userPermissionService,
@NonNull GroupPermissionService groupPermissionService,
@NonNull ApplicationPermissionService applicationPermissionService) {
this.visaService = visaService;
this.visaPermissionService = visaPermissionService;
this.groupPermissionService = groupPermissionService;
this.userPermissionService = userPermissionService;
this.applicationPermissionService = applicationPermissionService;
Expand All @@ -61,6 +67,10 @@ public VisaController(
return visaService.getById(id);
}

/*
* This method is used to list all visas
* @return visas List<Visa>
*/
@AdminScoped
@RequestMapping(method = GET, value = "")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "All Visas")})
Expand All @@ -72,6 +82,11 @@ public VisaController(
return new PageDTO<>(visaService.listVisa(pageable));
}

/*
* This method is used to create visa using visa create request
* @param visaRequest VisaRequest
* @return Visa visa
*/
@AdminScoped
@RequestMapping(method = POST, value = "")
@ApiResponses(
Expand All @@ -85,6 +100,12 @@ public VisaController(
return visaService.create(visaRequest);
}

/*
* This method is used to update visa using visa id and update request
* @param visaId UUID
* @param visaRequest VisaRequest
* @return Visa visa
*/
@AdminScoped
@RequestMapping(method = PUT, value = "/{id}")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Update Visa")})
Expand All @@ -96,6 +117,10 @@ public VisaController(
return visaService.partialUpdate(id, visaRequest);
}

/*
* This method is used to delete visa using visa id
* @param visaId UUID
*/
@AdminScoped
@RequestMapping(method = DELETE, value = "/{id}")
@ResponseStatus(value = HttpStatus.OK)
Expand All @@ -105,4 +130,71 @@ public void deleteVisa(
@PathVariable(value = "id", required = true) UUID id) {
visaService.delete(id);
}

/*
* This method is used to fetch visa permissions using visa id
* @param visaId UUID
* @return visaPermissions List<VisaPermissions>
*/
@AdminScoped
@RequestMapping(method = GET, value = "/permissions/visaId/{id}")
@ApiResponses(
value = {@ApiResponse(responseCode = "200", description = "Get VisaPermissions by visaId")})
@JsonView(Views.REST.class)
public @ResponseBody List<VisaPermission> getPermissionsByVisaId(
@Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true)
final String authorization,
@PathVariable(value = "id", required = true) UUID id) {
return visaPermissionService.getPermissionsByVisaId(id);
}

/*
* This method is used to fetch visa permissions using policy id
* @param policyId UUID
* @return visaPermissions List<VisaPermissions>
*/
@AdminScoped
@RequestMapping(method = GET, value = "/permissions/policyId/{id}")
@ApiResponses(
value = {@ApiResponse(responseCode = "200", description = "Get VisaPermissions by policyId")})
@JsonView(Views.REST.class)
public @ResponseBody List<VisaPermission> getPermissionsByPolicyId(
@Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true)
final String authorization,
@PathVariable(value = "id", required = true) UUID id) {
return visaPermissionService.getPermissionsByPolicyId(id);
}

/*
* This method is used to create/update visa permissions
* @param visaPermissionRequest VisaPermissionRequest
* @return visaPermission VisaPermission
*/
@AdminScoped
@RequestMapping(method = POST, value = "/permissions")
@ApiResponses(
value = {@ApiResponse(responseCode = "200", description = "Create or Update VisaPermission")})
@JsonView(Views.REST.class)
public @ResponseBody VisaPermission createOrUpdatePermissions(
@Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true)
final String authorization,
@RequestBody(required = true) VisaPermissionRequest visaPermissionRequest) {
return visaPermissionService.createOrUpdatePermissions(visaPermissionRequest);
}

/*
* This method is used to delete/remove visa permissions
* @param visaPermissionRequest VisaPermissionRequest
*/
@AdminScoped
@RequestMapping(method = DELETE, value = "/permissions/{policyId}/{visaId}")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Remove VisaPermission")})
@JsonView(Views.REST.class)
public @ResponseBody void removePermissions(
@Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true)
final String authorization,
@PathVariable(value = "policyId", required = true) UUID policyId,
@PathVariable(value = "visaId", required = true) UUID visaId) {
visaPermissionService.removePermission(policyId, visaId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package bio.overture.ego.model.dto;

import bio.overture.ego.model.enums.AccessLevel;
import java.util.UUID;
import lombok.*;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class VisaPermissionRequest {

private UUID policyId;

private UUID visaId;

private AccessLevel accessLevel;
}
2 changes: 1 addition & 1 deletion src/main/java/bio/overture/ego/model/entity/Visa.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class Visa implements Identifiable<UUID> {
private String by;

@JsonIgnore
@ManyToMany(mappedBy = "visaId", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToMany(mappedBy = JavaFields.VISA, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@Builder.Default
private Set<VisaPermission> visaPermissions = newHashSet();
}
45 changes: 13 additions & 32 deletions src/main/java/bio/overture/ego/model/entity/VisaPermission.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package bio.overture.ego.model.entity;


import bio.overture.ego.model.enums.AccessLevel;
import bio.overture.ego.model.enums.JavaFields;
import bio.overture.ego.model.enums.SqlFields;
import bio.overture.ego.model.enums.Tables;
import bio.overture.ego.view.Views;
import com.fasterxml.jackson.annotation.JsonView;
import com.vladmihalcea.hibernate.type.basic.PostgreSQLEnumType;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import java.util.Collection;
import java.util.UUID;
import lombok.*;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

@Entity
@Table(name = Tables.ACLVISAPERMISSION)
Expand All @@ -25,32 +18,20 @@
@JsonView(Views.REST.class)
@ToString(callSuper = true)
@FieldNameConstants
@EqualsAndHashCode(
callSuper = true,
of = {"id"})
@NamedEntityGraph(
name = "visa-permission-entity-with-relationships",
attributeNodes = {
@NamedAttributeNode(value = JavaFields.POLICY),
@NamedAttributeNode(value = JavaFields.VISA)
})
public class VisaPermission extends AbstractPermission<Visa> {

@Id
@Column(name = SqlFields.ID, updatable = false, nullable = false)
@GenericGenerator(name = "aclp_uuid", strategy = "org.hibernate.id.UUIDGenerator")
@GeneratedValue(generator = "aclp_uuid")
private UUID id;

@JoinColumn(name = SqlFields.ID, nullable = false)
private UUID entity;

@JoinColumn(name = SqlFields.ID, nullable = false)
private UUID visaId;

@NotNull
@Column(name = SqlFields.MASK, nullable = false)
@Enumerated(EnumType.STRING)
@Type(value = PostgreSQLEnumType.class)
private AccessLevel mask;

@ManyToMany
@JoinTable(
name = "ga4ghvisa",
joinColumns = @JoinColumn(name = "aclp_id", referencedColumnName = "visaId"),
inverseJoinColumns = @JoinColumn(name = "visa_id", referencedColumnName = "id"))
private Collection<Visa> visas;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = SqlFields.VISA_ID, nullable = false)
private Visa visa;

@Override
public Visa getOwner() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/bio/overture/ego/model/enums/JavaFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class JavaFields {
public static final String VALUE = "value";

public static final String BY = "by";

public static final String VISAPERMISSION = "ACLVISAPERMISSION";
public static final String GA4GHVISA = "ga4ghvisa";
public static final String VISA = "visa";
}
8 changes: 1 addition & 7 deletions src/main/java/bio/overture/ego/model/enums/SqlFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ public class SqlFields {
public static final String ERRORREDIRECTURI = "errorredirecturi";
public static final String SOURCE = "source";
public static final String VALUE = "value";

public static final String BY = "by";

public static final String ENTITY = "entity";

public static final String VISAID = "visaId";

public static final String MASK = "mask";
public static final String VISA_ID = "visa_id";
}
2 changes: 0 additions & 2 deletions src/main/java/bio/overture/ego/model/enums/Tables.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class Tables {
public static final String APPLICATION_PERMISSION = "applicationpermission";
public static final String DEFAULTPROVIDERTRIPWIRE = "defaultprovidertripwire";
public static final String INITTRIPWIRE = "inittripwire";

public static final String GA4GHVISA = "ga4ghvisa";

public static final String ACLVISAPERMISSION = "ACLVISAPERMISSION";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package bio.overture.ego.repository;

import static org.springframework.data.jpa.repository.EntityGraph.EntityGraphType.FETCH;

import bio.overture.ego.model.entity.VisaPermission;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.EntityGraph;

public interface VisaPermissionRepository extends NamedRepository<VisaPermission, UUID> {
@Override
@Deprecated
default Optional<VisaPermission> findByName(String name) {
return null;
}

List<VisaPermission> findAll();

@EntityGraph(value = "visa-permission-entity-with-relationships", type = FETCH)
List<VisaPermission> findByVisa_Id(UUID visa_id);

@EntityGraph(value = "visa-permission-entity-with-relationships", type = FETCH)
List<VisaPermission> findByPolicy_Id(UUID policy_id);

@EntityGraph(value = "visa-permission-entity-with-relationships", type = FETCH)
List<VisaPermission> findByPolicyIdAndVisaId(UUID policy_id, UUID visa_id);
}
Loading