@Test public void testMutateSkipForAppAuthRightAdminApp() { SecurityUtil.setUserContext(SecurityUtil.UserContext.STAFF_CONTEXT); Map<String, Object> body = new HashMap<String, Object>(); body.put("staffUniqueStateId", "staff"); teacher = repo.create("staff", body, "staff"); PathSegment v1 = Mockito.mock(PathSegment.class); when(v1.getPath()).thenReturn("/v1"); PathSegment edorgs = Mockito.mock(PathSegment.class); when(edorgs.getPath()).thenReturn("/educationOrganizations"); when(principal.getEntity()).thenReturn(staff); Map<String, Collection<GrantedAuthority>> edOrgRights = generateSimpleEdOrgRightsMap("theEdOrg", Right.APP_AUTHORIZE); when(principal.getEdOrgRights()).thenReturn(edOrgRights); MutatedContainer mutated = mutator.mutate(Arrays.asList(v1,edorgs), null, principal, ADMIN_APP_ID); System.out.println("The path is : " + mutated.getPath() + ", qparams : " + mutated.getQueryParameters()); Assert.assertEquals("Endpoint should NOT have been rewritten to " + mutated.getPath(), createMutatedContainer(null, ""), mutated); }
public MutatedContainer mutate(List<PathSegment> segments, String queryParameters, SLIPrincipal principal, String clientId) { Entity user = principal.getEntity(); Map<String, String> parameters = MutatorUtil.getParameterMap(queryParameters); for (Pair<String, String> parameterResourcePair : PARAMETER_RESOURCE_PAIRS) { String parameter = parameterResourcePair.getLeft(); String resource = parameterResourcePair.getRight(); if (parameters.containsKey(parameter)) { EntityDefinition definition = definitionStore .lookupByResourceName(resource); if (definition != null) { NeutralQuery query = new NeutralQuery(new NeutralCriteria( parameter, NeutralCriteria.OPERATOR_EQUAL, parameters.get(parameter))); Entity e = repo.findOne(definition.getType(), query); if (e != null) { MutatedContainer newMutated = new MutatedContainer(); String path = String.format("/%s/%s", resource, e.getEntityId()); if (EntityNames.TEACHER.equals(e.getType())) { path = String.format("/teachers/%s", e.getEntityId()); } else if (EntityNames.STAFF.equals(e.getType())) { path = String.format("/staff/%s", e.getEntityId()); } newMutated.setPath(path); newMutated.setQueryParameters(queryParameters); LOG.info("Rewriting URI to {} based on natural keys", newMutated.getPath()); return newMutated; } } } } MutatedContainer generalMutation = doGeneralMutations( stringifyPathSegments(segments), queryParameters, user); if (generalMutation != null) { return generalMutation; } MutatedContainer mutated; if (segments.size() < NUM_SEGMENTS_IN_TWO_PART_REQUEST) { if (shouldSkipMutation(segments, queryParameters, principal, clientId)) { mutated = new MutatedContainer(); mutated.setPath(null); mutated.setQueryParameters(queryParameters); } else { if (segments.size() == 1) { mutated = mutateBaseUri(segments.get(0).getPath(), ResourceNames.HOME, queryParameters, user); } else { mutated = mutateBaseUri(segments.get(0).getPath(), segments.get(1).getPath(), queryParameters, user); } } } else { mutated = mutateUriBasedOnRole(segments, queryParameters, user); } return mutated; }
UriMutator { public MutatedContainer mutate(List<PathSegment> segments, String queryParameters, SLIPrincipal principal, String clientId) { Entity user = principal.getEntity(); Map<String, String> parameters = MutatorUtil.getParameterMap(queryParameters); for (Pair<String, String> parameterResourcePair : PARAMETER_RESOURCE_PAIRS) { String parameter = parameterResourcePair.getLeft(); String resource = parameterResourcePair.getRight(); if (parameters.containsKey(parameter)) { EntityDefinition definition = definitionStore .lookupByResourceName(resource); if (definition != null) { NeutralQuery query = new NeutralQuery(new NeutralCriteria( parameter, NeutralCriteria.OPERATOR_EQUAL, parameters.get(parameter))); Entity e = repo.findOne(definition.getType(), query); if (e != null) { MutatedContainer newMutated = new MutatedContainer(); String path = String.format("/%s/%s", resource, e.getEntityId()); if (EntityNames.TEACHER.equals(e.getType())) { path = String.format("/teachers/%s", e.getEntityId()); } else if (EntityNames.STAFF.equals(e.getType())) { path = String.format("/staff/%s", e.getEntityId()); } newMutated.setPath(path); newMutated.setQueryParameters(queryParameters); LOG.info("Rewriting URI to {} based on natural keys", newMutated.getPath()); return newMutated; } } } } MutatedContainer generalMutation = doGeneralMutations( stringifyPathSegments(segments), queryParameters, user); if (generalMutation != null) { return generalMutation; } MutatedContainer mutated; if (segments.size() < NUM_SEGMENTS_IN_TWO_PART_REQUEST) { if (shouldSkipMutation(segments, queryParameters, principal, clientId)) { mutated = new MutatedContainer(); mutated.setPath(null); mutated.setQueryParameters(queryParameters); } else { if (segments.size() == 1) { mutated = mutateBaseUri(segments.get(0).getPath(), ResourceNames.HOME, queryParameters, user); } else { mutated = mutateBaseUri(segments.get(0).getPath(), segments.get(1).getPath(), queryParameters, user); } } } else { mutated = mutateUriBasedOnRole(segments, queryParameters, user); } return mutated; } }
UriMutator { public MutatedContainer mutate(List<PathSegment> segments, String queryParameters, SLIPrincipal principal, String clientId) { Entity user = principal.getEntity(); Map<String, String> parameters = MutatorUtil.getParameterMap(queryParameters); for (Pair<String, String> parameterResourcePair : PARAMETER_RESOURCE_PAIRS) { String parameter = parameterResourcePair.getLeft(); String resource = parameterResourcePair.getRight(); if (parameters.containsKey(parameter)) { EntityDefinition definition = definitionStore .lookupByResourceName(resource); if (definition != null) { NeutralQuery query = new NeutralQuery(new NeutralCriteria( parameter, NeutralCriteria.OPERATOR_EQUAL, parameters.get(parameter))); Entity e = repo.findOne(definition.getType(), query); if (e != null) { MutatedContainer newMutated = new MutatedContainer(); String path = String.format("/%s/%s", resource, e.getEntityId()); if (EntityNames.TEACHER.equals(e.getType())) { path = String.format("/teachers/%s", e.getEntityId()); } else if (EntityNames.STAFF.equals(e.getType())) { path = String.format("/staff/%s", e.getEntityId()); } newMutated.setPath(path); newMutated.setQueryParameters(queryParameters); LOG.info("Rewriting URI to {} based on natural keys", newMutated.getPath()); return newMutated; } } } } MutatedContainer generalMutation = doGeneralMutations( stringifyPathSegments(segments), queryParameters, user); if (generalMutation != null) { return generalMutation; } MutatedContainer mutated; if (segments.size() < NUM_SEGMENTS_IN_TWO_PART_REQUEST) { if (shouldSkipMutation(segments, queryParameters, principal, clientId)) { mutated = new MutatedContainer(); mutated.setPath(null); mutated.setQueryParameters(queryParameters); } else { if (segments.size() == 1) { mutated = mutateBaseUri(segments.get(0).getPath(), ResourceNames.HOME, queryParameters, user); } else { mutated = mutateBaseUri(segments.get(0).getPath(), segments.get(1).getPath(), queryParameters, user); } } } else { mutated = mutateUriBasedOnRole(segments, queryParameters, user); } return mutated; }  }
UriMutator { public MutatedContainer mutate(List<PathSegment> segments, String queryParameters, SLIPrincipal principal, String clientId) { Entity user = principal.getEntity(); Map<String, String> parameters = MutatorUtil.getParameterMap(queryParameters); for (Pair<String, String> parameterResourcePair : PARAMETER_RESOURCE_PAIRS) { String parameter = parameterResourcePair.getLeft(); String resource = parameterResourcePair.getRight(); if (parameters.containsKey(parameter)) { EntityDefinition definition = definitionStore .lookupByResourceName(resource); if (definition != null) { NeutralQuery query = new NeutralQuery(new NeutralCriteria( parameter, NeutralCriteria.OPERATOR_EQUAL, parameters.get(parameter))); Entity e = repo.findOne(definition.getType(), query); if (e != null) { MutatedContainer newMutated = new MutatedContainer(); String path = String.format("/%s/%s", resource, e.getEntityId()); if (EntityNames.TEACHER.equals(e.getType())) { path = String.format("/teachers/%s", e.getEntityId()); } else if (EntityNames.STAFF.equals(e.getType())) { path = String.format("/staff/%s", e.getEntityId()); } newMutated.setPath(path); newMutated.setQueryParameters(queryParameters); LOG.info("Rewriting URI to {} based on natural keys", newMutated.getPath()); return newMutated; } } } } MutatedContainer generalMutation = doGeneralMutations( stringifyPathSegments(segments), queryParameters, user); if (generalMutation != null) { return generalMutation; } MutatedContainer mutated; if (segments.size() < NUM_SEGMENTS_IN_TWO_PART_REQUEST) { if (shouldSkipMutation(segments, queryParameters, principal, clientId)) { mutated = new MutatedContainer(); mutated.setPath(null); mutated.setQueryParameters(queryParameters); } else { if (segments.size() == 1) { mutated = mutateBaseUri(segments.get(0).getPath(), ResourceNames.HOME, queryParameters, user); } else { mutated = mutateBaseUri(segments.get(0).getPath(), segments.get(1).getPath(), queryParameters, user); } } } else { mutated = mutateUriBasedOnRole(segments, queryParameters, user); } return mutated; }  MutatedContainer mutate(List<PathSegment> segments,
			String queryParameters, SLIPrincipal principal, String clientId); MutatedContainer mutateBaseUri(String version, String resource,
			final String queryParameters, Entity user); }
UriMutator { public MutatedContainer mutate(List<PathSegment> segments, String queryParameters, SLIPrincipal principal, String clientId) { Entity user = principal.getEntity(); Map<String, String> parameters = MutatorUtil.getParameterMap(queryParameters); for (Pair<String, String> parameterResourcePair : PARAMETER_RESOURCE_PAIRS) { String parameter = parameterResourcePair.getLeft(); String resource = parameterResourcePair.getRight(); if (parameters.containsKey(parameter)) { EntityDefinition definition = definitionStore .lookupByResourceName(resource); if (definition != null) { NeutralQuery query = new NeutralQuery(new NeutralCriteria( parameter, NeutralCriteria.OPERATOR_EQUAL, parameters.get(parameter))); Entity e = repo.findOne(definition.getType(), query); if (e != null) { MutatedContainer newMutated = new MutatedContainer(); String path = String.format("/%s/%s", resource, e.getEntityId()); if (EntityNames.TEACHER.equals(e.getType())) { path = String.format("/teachers/%s", e.getEntityId()); } else if (EntityNames.STAFF.equals(e.getType())) { path = String.format("/staff/%s", e.getEntityId()); } newMutated.setPath(path); newMutated.setQueryParameters(queryParameters); LOG.info("Rewriting URI to {} based on natural keys", newMutated.getPath()); return newMutated; } } } } MutatedContainer generalMutation = doGeneralMutations( stringifyPathSegments(segments), queryParameters, user); if (generalMutation != null) { return generalMutation; } MutatedContainer mutated; if (segments.size() < NUM_SEGMENTS_IN_TWO_PART_REQUEST) { if (shouldSkipMutation(segments, queryParameters, principal, clientId)) { mutated = new MutatedContainer(); mutated.setPath(null); mutated.setQueryParameters(queryParameters); } else { if (segments.size() == 1) { mutated = mutateBaseUri(segments.get(0).getPath(), ResourceNames.HOME, queryParameters, user); } else { mutated = mutateBaseUri(segments.get(0).getPath(), segments.get(1).getPath(), queryParameters, user); } } } else { mutated = mutateUriBasedOnRole(segments, queryParameters, user); } return mutated; }  MutatedContainer mutate(List<PathSegment> segments,
			String queryParameters, SLIPrincipal principal, String clientId); MutatedContainer mutateBaseUri(String version, String resource,
			final String queryParameters, Entity user); static final int NUM_SEGMENTS_IN_TWO_PART_REQUEST; static final int NUM_SEGMENTS_IN_ONE_PART_REQUEST; }