为multipartFormData,Play Framework编写具有安全特性的Body Parser

时间:2014-08-20 10:20:15

标签: scala playframework-2.0

我正在尝试在提交表单的同时上传图片,经过一些研究后,我尝试使用mutlipartFormData来完成这项专长。 这是我按照教程后的表单提交函数标题。

def insert = withInsert(parse.multipartFormData) { username => implicit request =>

我使用安全特性来检查用户(使用用户),登录时间(withAuth)和权限(withInsert)

def withUser(f: => String => Request[AnyContent] => Result) = {
        Security.Authenticated(username, onUnauthorized) { user =>
          Action(request => f(user)(request))
        }
    }

def withAuth(f: => String => Request[AnyContent] => Result) = withUser { user => request =>
    var timestamp = request.session.get("timestamp")
    timestamp.map { timestamp =>
        if(System.currentTimeMillis - timestamp.toLong < (3600*1000))
            f(user)(request)
        else
            onUnauthorized(request)
    }
    .getOrElse{
        onUnauthorized(request)
    }
}

def withInsert[A](p: BodyParser[A])(f: String => Request[A] => Result) = withAuth { username => request =>

        val permission = User.checkAuth("Page", username)

        if(permission.page_insert == 1)
            Action(p)(request => f(username)(request))
         else
            onPermissionDenied(request)
    }

def onPermissionDenied(request: RequestHeader) = Results.Redirect(routes.Page.index)

由于insert函数需要一个body解析器,我修改了(withInsert)特性以支持一个body解析器。但是,我在这一行上遇到了编译错误。

Action(p)(request => f(username)(request))

type mismatch; found : play.api.mvc.Action[A] required: play.api.mvc.Result

我很遗憾这里出了什么问题,非常感谢任何帮助。

编辑:

我试图完成教程所做的工作,放弃在安全特性上使用withAuth

def withInsert[A](p: BodyParser[A])(f: String => Request[A] => Result) = { 
        Security.Authenticated(username, onUnauthorized) { user =>
            val permission = User.checkAuth("Page", user)

            if(permission.page_insert == 1)
                Action(p)(request => f(user)(request))
            else
                onPermissionDenied(request)
        }
    }

此代码在同一行导致另一个编译错误,但错误不同

not found: value request

删除权限检查后,不会返回编译错误。

def withInsert[A](p: BodyParser[A])(f: String => Request[A] => Result) = { 
        Security.Authenticated(username, onUnauthorized) { user =>
            Action(p)(request => f(user)(request))
        }
    }

但我需要程序在运行函数之前检查权限,而不仅仅是用户名(当前用户是否已登录)。有没有办法做到这一点? 我需要一个解决方法,以便我可以将权限检查和正文解析器应用于特征。

0 个答案:

没有答案