Created
February 15, 2012 13:48
-
-
Save danielberndt/1835804 to your computer and use it in GitHub Desktop.
Controller and model class for Action Composition Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import models.User; | |
import models.User.Role; | |
import play.mvc.Action; | |
import play.mvc.Controller; | |
import play.mvc.Http; | |
import play.mvc.Result; | |
import play.mvc.With; | |
import controllers.Auth.MinRole; | |
@MinRole(Role.User) | |
public class Auth extends Controller { | |
@With(RolesAction.class) | |
@Target({ElementType.TYPE, ElementType.METHOD}) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface MinRole { | |
Role value() default Role.Admin; | |
} | |
public static class RolesAction extends Action<MinRole> { | |
public Result call(Http.Context ctx) throws Throwable { | |
User user = User.find.ref(1L); | |
System.out.println("User: "+user); | |
if (user==null || !user.role.greaterEqual(configuration.value())) { | |
return unauthorized("no access for you!"); | |
} | |
return delegate.call(ctx); | |
} | |
} | |
public static Result index() { | |
return ok(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import play.db.ebean.Model; | |
@Entity | |
public class User extends Model { | |
private static final long serialVersionUID = 1L; | |
public enum Role { | |
User, Tutor, Admin; | |
public boolean greaterEqual(Role other) { | |
return compareTo(other)>=0; | |
} | |
} | |
@Id | |
public Long id; | |
public String email; | |
public String name; | |
public String password; | |
public Role role; | |
public static Finder<Long,User> find = new Finder<Long,User>(Long.class, User.class); | |
public User(String name, String email) { | |
this.name = name; | |
this.email = email; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment