Skip to content

Commit

Permalink
Add the @isApproved RestApiMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
epuzanov committed May 21, 2024
1 parent 68d6f2d commit 752c474
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public interface ApprovalService {

boolean isApprovable(Substance s);

ApprovalResult approve(Substance s) throws ApprovalException;

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ protected void defaultApprovalValidation(Substance s, String usernameOfApprover)
}
}

/**
* Check if the given Substance is approvable by the user invoking this method.
* @param s the Substance to approve.
* @return a true if the user invoking this method is able to approve given Substance.
*/
@Override
public boolean isApprovable(Substance s) {
String userName = GsrsSecurityUtils.getCurrentUsername().orElse(null);
if (userName == null) {
return false;
}
try {
defaultApprovalValidation(s, userName);
extraApprovalValidation(s, userName);
return true;
} catch (ApprovalException ex) {
return false;
}
}

/**
* Try to approve the given Substance. The user invoking this method
* must have Approver Role.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,17 @@ public void saveTempStructure(Structure s) {

}

@GetGsrsRestApiMapping(value={"({id})/@isApprovable", "/{id}/@isApprovable" })
public ResponseEntity isApprovableGetMethod(@PathVariable("id") String substanceUUIDOrName, @RequestParam Map<String, String> queryParameters) throws Exception {
Optional<Substance> substance = getEntityService().getEntityBySomeIdentifier(substanceUUIDOrName);

if(!substance.isPresent()){
return getGsrsControllerConfiguration().handleNotFound(queryParameters);
}
boolean approvable = approvalService.isApprovable(substance.get());
return new ResponseEntity<>(String.valueOf(approvable), HttpStatus.OK);
}

@Transactional
@GetGsrsRestApiMapping(value={"({id})/@approve", "/{id}/@approve" })
@hasApproverRole
Expand Down

0 comments on commit 752c474

Please sign in to comment.