您可以使用动态标签,这正是您所需要的。您可以在http://deadbolt-2-scala.herokuapp.com/#template-dynamic看到一个示例,它说明了这一点。在这里充实它,
@dynamic(handler, "foo") {
This content may be visible, depending on your luck
}
其中 foo 是您要限制的资源的名称。在您的 DeadboltHandler 中,您需要返回定义动态控件的 DynamicResourceHandler。
class MyDeadboltHandler(dynamicResourceHandler: Option[DynamicResourceHandler] = None) extends DeadboltHandler {
override def getDynamicResourceHandler[A](request: Request[A]): Option[DynamicResourceHandler] = {
if (dynamicResourceHandler.isDefined) dynamicResourceHandler
else Some(new MyDynamicResourceHandler())
}
}
DynamicResourceHandler 可以通过多种方式实现,但外观通常效果很好。为了使示例简单,我们将在这种情况下对值进行硬编码。
class MyDynamicResourceHandler extends DynamicResourceHandler
{
def isAllowed[A](name: String, meta: String, handler: DeadboltHandler, request: Request[A]) = {
MyDynamicResourceHandler.handlers(name).isAllowed(name,
meta,
handler,
request)
}
def checkPermission[A](permissionValue: String, deadboltHandler: DeadboltHandler, request: Request[A]) = {
// todo implement this when demonstrating permissions
false
}
}
object MyDynamicResourceHandler {
val handlers: Map[String, DynamicResourceHandler] = Map(
"foo" -> new DynamicResourceHandler() {
def isAllowed[A](name: String, meta: String, deadboltHandler: DeadboltHandler, request: Request[A]) =
// do something here to determine if the subject has access
def checkPermission[A](permissionValue: String, deadboltHandler: DeadboltHandler, request: Request[A]) = false
}
)
}