Search code examples
playframework-2.0securesocial

Play Forms: Mapping the request.user from a Secured Action?


I'd like to be able to get at the user when I'm mapping a request to an object using Play Forms. To make things more fiddly, I'm also using the SecureSocial framework, so request.user is a securesocial.core.SocialUser.

The code below is problematic because the mapped doc object is an immutable case class so I can't reassign lender. I'd prefer to keep the immutability if possible.

I'm a Scala noob so not sure how to proceed.

def create = SecuredAction() { implicit request =>
    models.Lendable.form.bindFromRequest.fold(
        errors => {
            BadRequest(errors.errorsAsJson)
        },
        doc => AsyncResult {
            // TODO: set the lender without mutating a val
            doc.lender = models.User.fromSocialUser(request.user)

            collection.insert(doc.copy()).map(_ => Ok)
        })
}

Would appreciate your help.

Thanks


Solution

  • you could so something like:

    doc => AsyncResult {
         val toInsert = doc.copy(lender = models.User.fromSecureUser(request.user))
         collection.insert(toInsert).map(_ => Ok)
    }