BrokerServicePermissionFilter extends JsonRpcPermissionsFilterAdapter { @Override public void doAccept(String method, Object... params) throws ForbiddenException { String workspaceId; switch (method) { case BROKER_STATUS_CHANGED_METHOD: case BROKER_RESULT_METHOD: RuntimeIdentityDto runtimeId = ((BrokerStatusChangedEvent) params[0]).getRuntimeId(); if (runtimeId == null || runtimeId.getWorkspaceId() == null) { throw new ForbiddenException("Workspace id must be specified"); } workspaceId = runtimeId.getWorkspaceId(); break; default: throw new ForbiddenException("Unknown method is configured to be filtered."); } Subject currentSubject = EnvironmentContext.getCurrent().getSubject(); if (!currentSubject.hasPermission( WorkspaceDomain.DOMAIN_ID, workspaceId, WorkspaceDomain.RUN)) { throw new ForbiddenException( "User doesn't have the required permissions to the specified workspace"); } }  @Inject void register(RequestHandlerManager requestHandlerManager); @Override void doAccept(String method, Object... params);  }
@Test( dataProvider = "coveredMethods", expectedExceptions = ForbiddenException.class, expectedExceptionsMessageRegExp = "User doesn't have the required permissions to the specified workspace") public void shouldThrowExceptionIfUserDoesNotHaveRunPermission(String method) throws Exception { when(subject.hasPermission(eq(WorkspaceDomain.DOMAIN_ID), eq("ws123"), eq(WorkspaceDomain.RUN))) .thenReturn(false); permissionFilter.doAccept( method, DtoFactory.newDto(BrokerStatusChangedEvent.class) .withRuntimeId(DtoFactory.newDto(RuntimeIdentityDto.class).withWorkspaceId("ws123"))); }
@Test(dataProvider = "coveredMethods") public void shouldDoNothingIfUserHasRunPermissions(String method) throws Exception { when(subject.hasPermission(WorkspaceDomain.DOMAIN_ID, "ws123", WorkspaceDomain.RUN)) .thenReturn(true); permissionFilter.doAccept( method, DtoFactory.newDto(BrokerStatusChangedEvent.class) .withRuntimeId(DtoFactory.newDto(RuntimeIdentityDto.class).withWorkspaceId("ws123"))); }
@Test( expectedExceptions = ForbiddenException.class, expectedExceptionsMessageRegExp = "Unknown method is configured to be filtered\\.") public void shouldThrowExceptionIfUnknownMethodIsInvoking() throws Exception { permissionFilter.doAccept( "unknown", DtoFactory.newDto(BrokerStatusChangedEvent.class) .withRuntimeId(DtoFactory.newDto(RuntimeIdentityDto.class).withWorkspaceId("ws123"))); }