OperationChainLimiter implements GraphHook { @Override public void preExecute(final OperationChain<?> opChain, final Context context) { if (null != opChain) { Integer chainScore = scorer.getChainScore(opChain, context.getUser()); Integer maxAuthScore = scorer.getMaxUserAuthScore(context.getUser().getOpAuths()); if (chainScore > maxAuthScore) { throw new UnauthorisedException("The maximum score limit for user: " + context.getUser().toString() + " is " + maxAuthScore + ".\n" + "The requested operation chain exceeded this score limit."); } } }  @Override void preExecute(final OperationChain<?> opChain, final Context context); @Override T postExecute(final T result, final OperationChain<?> opChain, final Context context); @Override T onFailure(final T result, final OperationChain<?> opChain, final Context context, final Exception e); @JsonIgnore Map<Class<? extends Operation>, Integer> getOpScores(); @JsonIgnore void setOpScores(final Map<Class<? extends Operation>, Integer> opScores); @JsonGetter("opScores") Map<String, Integer> getOpScoresAsStrings(); @JsonSetter("opScores") void setOpScoresFromStrings(final Map<String, Integer> opScores); Map<String, Integer> getAuthScores(); void setAuthScores(final Map<String, Integer> authScores); Map<Class<? extends Operation>, ScoreResolver> getScoreResolvers(); void setScoreResolvers(final Map<Class<? extends Operation>, ScoreResolver> resolvers);  }
@Test public void shouldAcceptOperationChainWhenUserHasAuthScoreGreaterThanChainScore() { final OperationChainLimiter hook = fromJson(OP_CHAIN_LIMITER_PATH); final OperationChain opChain = new OperationChain.Builder() .first(new GetElements()) .build(); final User user = new User.Builder() .opAuths("User") .build(); hook.preExecute(opChain, new Context(user)); }
@Test public void shouldAcceptOperationChainWhenUserHasAuthScoreEqualToChainScore() { final OperationChainLimiter hook = fromJson(OP_CHAIN_LIMITER_PATH); final OperationChain opChain = new OperationChain.Builder() .first(new GetAdjacentIds()) .then(new GetElements()) .then(new GenerateObjects<>()) .build(); final User user = new User.Builder() .opAuths("User") .build(); hook.preExecute(opChain, new Context(user)); }
@Test public void shouldRejectOperationChainWhenUserHasAuthScoreLessThanChainScore() { final OperationChainLimiter hook = fromJson(OP_CHAIN_LIMITER_PATH); final OperationChain opChain = new OperationChain.Builder() .first(new GetAdjacentIds()) .then(new GetAdjacentIds()) .then(new GetAdjacentIds()) .then(new GetElements()) .then(new GenerateObjects<>()) .build(); final User user = new User.Builder() .opAuths("User") .build(); try { hook.preExecute(opChain, new Context(user)); fail("Exception expected"); } catch (final UnauthorisedException e) { assertNotNull(e.getMessage()); } }
@Test public void shouldAcceptOperationChainWhenUserHasMaxAuthScoreGreaterThanChainScore() { final OperationChainLimiter hook = fromJson(OP_CHAIN_LIMITER_PATH); final OperationChain opChain = new OperationChain.Builder() .first(new GetAdjacentIds()) .then(new GetAdjacentIds()) .then(new GetElements()) .then(new GenerateObjects<>()) .build(); final User user = new User.Builder() .opAuths("SuperUser", "User") .build(); hook.preExecute(opChain, new Context(user)); }
@Test public void shouldRejectOperationChainWhenUserHasMaxAuthScoreLessThanChainScore() { final OperationChainLimiter hook = fromJson(OP_CHAIN_LIMITER_PATH); final OperationChain opChain = new OperationChain.Builder() .first(new GetAllElements()) .then(new GetElements()) .then(new GenerateObjects<>()) .build(); final User user = new User.Builder() .opAuths("SuperUser", "User") .build(); try { hook.preExecute(opChain, new Context(user)); fail("Exception expected"); } catch (final UnauthorisedException e) { assertNotNull(e.getMessage()); } }
@Test public void shouldRejectOperationChainWhenUserHasNoAuthWithAConfiguredScore() { final OperationChainLimiter hook = fromJson(OP_CHAIN_LIMITER_PATH); final OperationChain opChain = new OperationChain.Builder() .first(new GetElements()) .build(); final User user = new User.Builder() .opAuths("NoScore") .build(); try { hook.preExecute(opChain, new Context(user)); fail("Exception expected"); } catch (final UnauthorisedException e) { assertNotNull(e.getMessage()); } }