Skip to content

Instantly share code, notes, and snippets.

@guillaumebort
Created June 6, 2013 09:21
Show Gist options
  • Save guillaumebort/5720350 to your computer and use it in GitHub Desktop.
Save guillaumebort/5720350 to your computer and use it in GitHub Desktop.
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result index(String name) {
return (new Secured(name) {
public Result apply() {
return ok(index.render("Your new application is ready."));
}
}).run();
}
/** -- Secured Action, should be packaged elsewhere -- **/
static abstract class Secured extends Action.Simple {
private final String connectedUser;
public Secured(String connectedUser) {
this.connectedUser = connectedUser;
}
public Result call(Http.Context ctx) throws Throwable {
if("Bob".equals(connectedUser)) {
return apply();
} else {
return forbidden("Please login as an authorized user!");
}
}
public abstract Result apply();
public Result run() {
try {
return call(ctx());
} catch(Throwable e) {
throw new RuntimeException("Caught in Secured", e);
}
}
}
/** -- **/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment