Skip to content

Commit

Permalink
Enum To Describe User Requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Jan 14, 2025
1 parent e3f5490 commit a342c95
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
11 changes: 6 additions & 5 deletions vcell-rest/src/main/java/org/vcell/restq/db/UserRestDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import jakarta.ws.rs.WebApplicationException;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.vcell.auth.JWTUtils;
import org.vcell.restq.Main;
import org.vcell.restq.auth.CustomSecurityIdentityAugmentor;
import org.vcell.restq.handlers.UsersResource;
import org.vcell.util.DataAccessException;
Expand Down Expand Up @@ -51,14 +50,16 @@ public UserRestDB(AgroalConnectionFactory agroalConnectionFactory) throws DataAc
* and error will be thrown about the user being unauthenticated.
* Tokens generated by the old API for guest users will have no effect on this function because the securityIdentity
* is something only our designated Auth0 provider can create.
* @param securityIdentity
* @param defaultGuest
* @throws DataAccessException
*/
public User getUserFromIdentity(SecurityIdentity securityIdentity, boolean defaultGuest) throws DataAccessException {
public enum UserRequirement{
ALLOW_ANONYMOUS,
REQUIRE_USER
}
public User getUserFromIdentity(SecurityIdentity securityIdentity, UserRequirement allowAnonymous) throws DataAccessException {
List<UserIdentity> userIdentities = getUserIdentities(securityIdentity);
if (userIdentities == null || userIdentities.isEmpty()){
if (defaultGuest){
if (allowAnonymous == UserRequirement.ALLOW_ANONYMOUS){
return null;
} else {
throw new WebApplicationException("User is not authenticated.", HTTP.UNAUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.annotation.security.RolesAllowed;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
Expand Down Expand Up @@ -62,7 +61,7 @@ public Response getUsage() throws DataAccessException {
if (securityIdentity.isAnonymous()){
throw new WebApplicationException("not authenticated", 401);
}
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, false);
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
try {
String htmlString = adminRestDB.getUsageSummaryHtml(vcellUser);
StreamingOutput fileStream = output -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.vcell.restq.Main;
import org.vcell.restq.db.BioModelRestDB;
import org.vcell.restq.db.UserRestDB;
import org.vcell.restq.models.BioModel;
Expand Down Expand Up @@ -45,7 +44,7 @@ public BioModelResource(BioModelRestDB bioModelRestDB, UserRestDB userRestDB) {
})
@Produces(MediaType.APPLICATION_JSON)
public BioModel getBioModelInfo(@PathParam("bioModelID") String bioModelID) throws SQLException, DataAccessException, ExpressionException {
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, true);
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.ALLOW_ANONYMOUS);
try {
BioModelRep bioModelRep = bioModelRestDB.getBioModelRep(new KeyValue(bioModelID), vcellUser);
return BioModel.fromBioModelRep(bioModelRep);
Expand Down Expand Up @@ -102,7 +101,7 @@ public void getBioModelDiagram(@PathParam("bioModelID") String bioModelID){
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed("user")
public String uploadBioModel(String bioModelXML) throws DataAccessException, XmlParseException {
User user = userRestDB.getUserFromIdentity(securityIdentity, false);
User user = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
KeyValue keyValue = bioModelRestDB.saveBioModel(user, bioModelXML);
return keyValue.toString();
}
Expand All @@ -111,7 +110,7 @@ public String uploadBioModel(String bioModelXML) throws DataAccessException, Xml
@Path("{bioModelID}")
@Operation(operationId = "deleteBioModel", summary = "Delete the BioModel from VCell's database.")
public void deleteBioModel(@PathParam("bioModelID") String bioModelID) throws DataAccessException {
User user = userRestDB.getUserFromIdentity(securityIdentity, false);;
User user = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);;
bioModelRestDB.deleteBioModel(user, new KeyValue(bioModelID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public FieldDataResource(FieldDataDB fieldDataDB, UserRestDB userRestDB){
@Operation(operationId = "getAllFieldDataIDs", summary = "Get all of the ids used to identify, and retrieve field data.")
public ArrayList<FieldDataReference> getAllFieldDataIDs(){
try {
return fieldDataDB.getAllFieldDataIDs(userRestDB.getUserFromIdentity(securityIdentity, false));
return fieldDataDB.getAllFieldDataIDs(userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER));
} catch (SQLException e) {
throw new WebApplicationException("Can't retrieve field data ID's.", 500);
} catch (DataAccessException e) {
Expand All @@ -67,7 +67,7 @@ public ArrayList<FieldDataReference> getAllFieldDataIDs(){
@Operation(operationId = "getFieldDataShapeFromID", summary = "Get the shape of the field data. That is it's size, origin, extent, and data identifiers.")
public FieldDataShape getFieldDataShapeFromID(@PathParam("fieldDataID") String fieldDataID){
try {
FieldDataFileOperationResults results = fieldDataDB.getFieldDataFromID(userRestDB.getUserFromIdentity(securityIdentity, false), fieldDataID, 0);
FieldDataFileOperationResults results = fieldDataDB.getFieldDataFromID(userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER), fieldDataID, 0);
return new FieldDataShape(results.extent, results.origin, results.iSize, results.dataIdentifierArr,results.times);
} catch (DataAccessException e) {
if (e.getCause() instanceof FileNotFoundException){
Expand Down Expand Up @@ -99,6 +99,7 @@ public FieldDataShape getFieldDataShapeFromID(@PathParam("fieldDataID") String f
@Operation(operationId = "analyzeFieldDataFile", summary = "Analyze the field data from the uploaded file. Filenames must be lowercase alphanumeric, and can contain underscores.")
public AnalyzedResultsFromFieldData analyzeFieldData(@RestForm File file, @RestForm String fileName){
try{
userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
if (!Pattern.matches("^[a-z0-9_]*$", fileName) || fileName.length() > 100 || fileName.isEmpty()){
throw new WebApplicationException("Invalid file name.", 400);
}
Expand All @@ -116,7 +117,7 @@ public AnalyzedResultsFromFieldData analyzeFieldData(@RestForm File file, @RestF
public FieldDataSaveResults createNewFieldDataFromFile(AnalyzedResultsFromFieldData saveFieldData){
FieldDataSaveResults fieldDataSaveResults;
try{
User user = userRestDB.getUserFromIdentity(securityIdentity, false);
User user = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
FieldDataFileOperationResults fileResults = fieldDataDB.saveNewFieldDataFromFile(saveFieldData, user);
fieldDataSaveResults = new FieldDataSaveResults(fileResults.externalDataIdentifier.getName(), fileResults.externalDataIdentifier.getKey().toString());
} catch (ImageException | DataFormatException | DataAccessException e) {
Expand Down Expand Up @@ -145,7 +146,7 @@ public FieldDataSaveResults createNewFieldDataFromFile(AnalyzedResultsFromFieldD
@Operation(operationId = "deleteFieldData", summary = "Delete the selected field data.")
public void deleteFieldData(@PathParam("fieldDataID") String fieldDataID){
try{
fieldDataDB.deleteFieldData(userRestDB.getUserFromIdentity(securityIdentity, false), fieldDataID);
fieldDataDB.deleteFieldData(userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER), fieldDataID);
} catch (DataAccessException e) {
throw new WebApplicationException(e.getMessage(), 500);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PublicationResource(PublicationService publicationService) {
@Operation(operationId = "getPublicationById", summary = "Get publication by ID")
public Publication get_by_id(@PathParam("id") Long publicationID) throws SQLException, DataAccessException {
try {
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, true);
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.ALLOW_ANONYMOUS);
return publicationService.getPublication(new KeyValue(publicationID.toString()), vcellUser);
} catch (DataAccessException | SQLException e) {
Log.error(e);
Expand All @@ -53,7 +53,7 @@ public Publication get_by_id(@PathParam("id") Long publicationID) throws SQLExce
@Operation(operationId = "getPublications", summary = "Get all publications")
public Publication[] get_list() {
try {
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, true);
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.ALLOW_ANONYMOUS);
return publicationService.getPublications(DatabaseServerImpl.OrderBy.year_desc, vcellUser);
} catch (PermissionException ee) {
Log.error(ee);
Expand All @@ -71,7 +71,7 @@ public Publication[] get_list() {
public Long add(Publication publication) throws DataAccessException {
Log.debug(securityIdentity.getPrincipal().getName()+" with roles " + securityIdentity.getRoles() + " is adding publication "+publication.title());
try {
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, false);
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
KeyValue key = publicationService.savePublication(publication, vcellUser);
return Long.parseLong(key.toString());
} catch (PermissionException ee) {
Expand All @@ -87,11 +87,11 @@ public Long add(Publication publication) throws DataAccessException {
@PUT
@Consumes(MediaType.APPLICATION_JSON)
// @RolesAllowed("curator")
@Operation(operationId = "updatePublication", summary = "Create publication")
@Operation(operationId = "updatePublication", summary = "Update publication")
public Publication update(Publication publication) {
Log.debug(securityIdentity.getPrincipal().getName()+" with roles " + securityIdentity.getRoles() + " is adding publication "+publication.title());
try {
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, false);
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
Publication pub = publicationService.updatePublication(publication, vcellUser);
return pub;
} catch (PermissionException ee) {
Expand All @@ -110,7 +110,7 @@ public Publication update(Publication publication) {
@Operation(operationId = "deletePublication", summary = "Delete publication")
public void delete(@PathParam("id") Long publicationID) {
try {
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, false);
User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
int numRecordsDeleted = publicationService.deletePublication(new KeyValue(publicationID.toString()), vcellUser);
if (numRecordsDeleted != 1) {
throw new ObjectNotFoundException("failed to delete publication, record not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SimulationResource(SimulationRestDB simulationRestDB, UserRestDB userRest
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<StatusMessage> startSimulation(@PathParam("simID") String simID) {
try {
User user = userRestDB.getUserFromIdentity(securityIdentity, false);
User user = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
return simulationRestDB.startSimulation(simID, user);
} catch (DataAccessException | SQLException e) {
throw new RuntimeException(e);
Expand All @@ -53,7 +53,7 @@ public ArrayList<StatusMessage> startSimulation(@PathParam("simID") String simID
@Operation(operationId = "stopSimulation", summary = "Stop a simulation.")
public ArrayList<StatusMessage> stopSimulation(@PathParam("simID") String simID) {
try {
User user = userRestDB.getUserFromIdentity(securityIdentity, false);
User user = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
return simulationRestDB.stopSimulation(simID, user);
} catch (DataAccessException | SQLException | VCMessagingException e) {
throw new RuntimeException(e);
Expand All @@ -67,7 +67,7 @@ public ArrayList<StatusMessage> stopSimulation(@PathParam("simID") String simID)
public SimulationStatusPersistentRecord getSimulationStatus(@PathParam("simID") String simID,
@QueryParam("bioModelID") String bioModelID, @QueryParam("mathModelID") String mathModelID){
try {
User user = userRestDB.getUserFromIdentity(securityIdentity, false);
User user = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
return simulationRestDB.getBioModelSimulationStatus(simID, bioModelID, user);
} catch (DataAccessException | SQLException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public AccesTokenRepresentationRecord generateBearerToken() throws DataAccessExc
if(securityIdentity.isAnonymous()){
return new AccesTokenRepresentationRecord(null, 0, 0, null, null);
}
org.vcell.util.document.User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, false);
org.vcell.util.document.User vcellUser = userRestDB.getUserFromIdentity(securityIdentity, UserRestDB.UserRequirement.REQUIRE_USER);
ApiAccessToken apiAccessToken = userRestDB.generateApiAccessToken(userRestDB.getAPIClient().getKey(), vcellUser);
return AccesTokenRepresentationRecord.getRecordFromAccessTokenRepresentation(apiAccessToken);
}
Expand Down

0 comments on commit a342c95

Please sign in to comment.