Skip to content

Instantly share code, notes, and snippets.

@aesteve
Created December 15, 2015 13:18
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aesteve/7052642a5c3454527faf to your computer and use it in GitHub Desktop.
centralize auth logic
public static authorize(JsonObject authInfo, Handler<User> onAuthorized, Handler<Throwable> onFailure) {
return res -> {
if (res.succeeded()) {
User user = res.result();
user.isAuthorised("newsletter:edit:13", res2 -> {
if (res2.succeeded()) {
boolean hasPermission = res2.result();
if(hasPermission){
onAuthorized.handle(user);
}
else{
onFailure.handle(new AuthorizationException());
}
} else {
onFailure.handle(res2.cause());
}
});
} else {
onFailure.handle(res.cause());
}
}
}
import static AuthorizationLogic.authorize;
authorize(authInfo, user -> {
user.greet();
// do what you want to
}, failure -> {
if (!(failure instanceof AuthorizationException)) {
log.error("Something went wrong");
// notify client (503 ?)
} else {
// context.fail(401);
// insult user
// ban him...
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment