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

Skip permission checks for service level tokens #23

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,7 @@ public void canConvertToOrder(List<ConvertToOrderDto> list) {
* Checks if current user has permission to manage a requisition template.
*/
public void canManageRequisitionTemplate() {
OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext()
.getAuthentication();

if (!authentication.isClientOnly()) {
checkPermission(REQUISITION_TEMPLATES_MANAGE, null, null, null);
}
checkPermission(REQUISITION_TEMPLATES_MANAGE, null, null, null);
}

public void canEditReportTemplates() {
Expand Down Expand Up @@ -222,6 +217,11 @@ private void checkPermission(String rightName, UUID program, UUID facility, UUID
}

private Boolean hasPermission(String rightName, UUID program, UUID facility, UUID warehouse) {
OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext()
.getAuthentication();
if (authentication.isClientOnly()) {
return true;
}
UserDto user = authenticationHelper.getCurrentUser();
RightDto right = authenticationHelper.getRight(rightName);
ResultDto<Boolean> result = userReferenceDataService.hasRight(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ public void setUp() {
when(requisition.getProgramId()).thenReturn(programId);
when(requisition.getFacilityId()).thenReturn(facilityId);
when(requisition.getSupplyingFacilityId()).thenReturn(facilityId);
when(requisition.getStatus()).thenReturn(RequisitionStatus.SUBMITTED);

when(securityContext.getAuthentication()).thenReturn(userClient);
when(authenticationHelper.getCurrentUser()).thenReturn(user);

when(authenticationHelper.getRight(REQUISITION_CREATE)).thenReturn(requisitionCreateRight);
Expand Down Expand Up @@ -348,7 +350,6 @@ public void cannotConvertToOrder() throws Exception {

@Test
public void canManageRequisitionTemplate() throws Exception {
when(securityContext.getAuthentication()).thenReturn(userClient);
hasRight(manageRequisitionTemplateRightId, true);

permissionService.canManageRequisitionTemplate();
Expand All @@ -359,16 +360,29 @@ public void canManageRequisitionTemplate() throws Exception {

@Test
public void cannotManageRequisitionTemplate() throws Exception {
when(securityContext.getAuthentication()).thenReturn(userClient);
expectException(REQUISITION_TEMPLATES_MANAGE);

permissionService.canManageRequisitionTemplate();
}

@Test
public void shouldAllowTrustedClientsManageRequisitionTemplate() {
public void serviceLevelTokensShouldHaveAllThePermissions() {
when(securityContext.getAuthentication()).thenReturn(trustedClient);

// Requisition permissions
permissionService.canViewRequisition(requisitionId);
permissionService.canInitOrAuthorizeRequisition(programId, facilityId);
permissionService.canInitRequisition(programId, facilityId);
permissionService.canApproveRequisition(requisitionId);
permissionService.canAuthorizeRequisition(requisitionId);
permissionService.canDeleteRequisition(requisitionId);
permissionService.canSubmitRequisition(requisitionId);
permissionService.canUpdateRequisition(requisitionId);
permissionService.canConvertToOrder(convertToOrderDtos);

// Report permissions
permissionService.canViewReports();
permissionService.canEditReportTemplates();
permissionService.canManageRequisitionTemplate();
}

Expand Down