Frage

I've just ported my application to Play 2.2 and modified some code to use the latest version of SecureSocial - I've seen case class UserId has been renamed to IdentityId. Well, I'm able to register a new user... and the data is saved to MongoDB successfully:

> db.identities.find()
{ "_id" : ObjectId("5256a6ffe4b000f9ba0b6ce5"), "identityId" : { "userId" : "joey", "providerId" : "userpass" }, "firstName" : "Joey", "lastName" : "Smith", "fullName" : "Joey Smith", "email" : "joey@mydomain.com", "authMethod" : { "method" : "userPassword" }, "passwordInfo" : { "hasher" : "bcrypt", "password" : "$2a$10$vDr02XZ5dAHmJHDexbBnROWejhtnYFufPJtHPr8IT.rqsCSAAu5Ju" } }

... but as soon as I try to login - and of course this implies reading the data from MongoDb, the toy no longer works. I always get a NoSuchElementException due to the fact that the data is not read from the database.

Here below is my full code:

import com.mongodb.casbah.Imports._
import com.novus.salat._
import com.novus.salat.annotations._
import com.novus.salat.dao._
import play.api.Play.current
import play.api.libs.json._
import play.api.libs.functional.syntax._
import securesocial.core._
import se.radley.plugin.salat._
import se.radley.plugin.salat.Binders._
import mongodbContext._

/**
 * Defines an account identity to be used by [[securesocial.core.SecureSocial]]
 * when signing up or signing in. @see securesocial.core.Identity.
 */
case class AccountIdentity(
  identityId: IdentityId,
  firstName: String,
  lastName: String,
  fullName: String,
  email: Option[String],
  avatarUrl: Option[String],
  authMethod: AuthenticationMethod,
  oAuth1Info: Option[OAuth1Info] = None,
  oAuth2Info: Option[OAuth2Info] = None,
  passwordInfo: Option[PasswordInfo] = None
) extends Identity

/**
 * Provides database access and serialization for [[AccountIdentity]] data.
 */
object AccountIdentity extends ((
  IdentityId,
  String,
  String,
  String,
  Option[String],
  Option[String],
  AuthenticationMethod,
  Option[OAuth1Info],
  Option[OAuth2Info],
  Option[PasswordInfo]) => AccountIdentity) with AccountIdentityDAO with SocialUserJson {

  /**
   * Returns an `AccountIdentity` initialized with the specified identity.
   *
   * @param     identity The identity that Initializes the `AccountIdentity`.
   */
  def apply(identity: Identity) : AccountIdentity = {
    AccountIdentity(
      identity.identityId,
      identity.firstName,
      identity.lastName,
      identity.fullName,
      identity.email,
      identity.avatarUrl,
      identity.authMethod,
      identity.oAuth1Info,
      identity.oAuth2Info,
      identity.passwordInfo
    )
  }
}

/**
 * Provides database access for [[AccountIdentity]] data.
 */
trait AccountIdentityDAO extends ModelCompanion[AccountIdentity, IdentityId] {

  private def collection = mongoCollection("identities")
  val dao = new SalatDAO[AccountIdentity, IdentityId](collection) {}

  collection.ensureIndex(DBObject("email" -> 1, "_id.providerId" -> 1), "emailProvider", unique = true)

  /**
   * Finds the [[AccountIdentity]] identified by the specified identity id.
   *
   * @param     identityId The identity id.
   * @return    An `Option` value containing the `AccountIdentity`, or `None`
   *            if there is no `AccountIdentity` identified by `identityId`.
   */
  def find(identityId: IdentityId): Option[AccountIdentity] = {
    dao.findOne(DBObject("_id.userId" -> identityId.userId, "_id.providerId" -> identityId.providerId))
  }

   /**
   * Finds the [[AccountIdentity]] identified by the specified email and provider id.
   *
   * @param     email The user email.
   * @param     providerId The provider id.
   * @return    An `Option` value containing the `AccountIdentity`, or `None` if
   *            there is no `AccountIdentity` associated with `email` and `providerId`.
   */
  def findByEmailAndProvider(email: String, providerId: String): Option[AccountIdentity] = {
    dao.findOne(DBObject("email" -> email, "_id.providerId" -> providerId))
  }
}

/**
 * Provides functionality for serializing/deserializing [[AccountIdentity]]
 * data to/from JSON.
 */
trait SocialUserJson {

  import IdentityId._
  import AuthenticationMethod._
  import OAuth1Info._
  import OAuth2Info._
  import PasswordInfo._

  /**
   * An [[AccountIdentity]] serialized to JSON.
   */
  implicit val identityJsonWrite = new Writes[AccountIdentity] {
    def writes(identity: AccountIdentity): JsValue = {
      Json.obj(
        "identityId" -> identity.identityId,
        "firstName" -> identity.firstName,
        "lastName" -> identity.lastName,
        "fullName" -> identity.fullName,
        "email" -> identity.email,
        "avatarUrl" -> identity.avatarUrl,
        "authMethod" -> identity.authMethod,
        "oAuth1Info" -> identity.oAuth1Info,
        "oAuth2Info" -> identity.oAuth2Info,
        "passwordInfo" -> identity.passwordInfo
      )
    }
  }

  /**
   * An [[AccountIdentity]] deserialized from JSON.
   */
  implicit val identityJsonRead = (
    (__ \ 'identityId).read[IdentityId] ~
    (__ \ 'firstName).read[String] ~
    (__ \ 'lasttName).read[String] ~
    (__ \ 'fullName).read[String] ~
    (__ \ 'email).readNullable[String] ~
    (__ \ 'avatarUrl).readNullable[String] ~
    (__ \ 'authMethod).read[AuthenticationMethod] ~
    (__ \ 'oAuth1Info).readNullable[OAuth1Info] ~
    (__ \ 'oAuth2Info).readNullable[OAuth2Info] ~
    (__ \ 'passwordInfo).readNullable[PasswordInfo]
  )(AccountIdentity)
}

As you can see, I'm using a composite key... so perhaps I'm doing something wrong in methods find and findByEmailAndProvider.

EDIT1: ... and of course I also implemented serialization/deserialization for IdentityId:

import play.api.libs.json._
import play.api.libs.functional.syntax._

/**
 * Provides functionality for serializing/deserializing [[securesocial.core.IdentityId]]
 * data to/from JSON.
 */
object IdentityId {

  /**
   * An `IdentityId` serialized to JSON.
   */
  implicit val identityIdJsonWrite = new Writes[securesocial.core.IdentityId] {
    def writes(identityId: securesocial.core.IdentityId): JsValue = {
      Json.obj(
        "userId" -> identityId.userId,
        "providerId" -> identityId.providerId
      )
    }
  }

  /**
   * An `IdentityId` deserialized from JSON.
   */
  implicit val identityIdJsonRead = (
    (__ \ 'userId).read[String] ~
    (__ \ 'providerId).read[String]
  )(securesocial.core.IdentityId.apply _)
}
War es hilfreich?

Lösung

Found... I just needed to remap identityId to _id:

import com.novus.salat._
import com.mongodb.casbah.Imports._

import play.api.Play
import play.api.Play.current

/**
 * Defines a custom Salat context for MongoDB.
 */
package object mongodbContext {

  /**
   * The custom context that globally overrides the Salat defaults.
   */
  implicit val context = {
    val context = new Context {
      val name = "global"
      override val typeHintStrategy = StringTypeHintStrategy(when = TypeHintFrequency.WhenNecessary, typeHint = "_t")
    }

    context.registerGlobalKeyOverride(remapThis = "identityId", toThisInstead = "_id")
    context.registerClassLoader(Play.classloader)
    context
  }
}

... and eventually the data in the database is correct:

> db.identities.find()
{ "_id" : { "userId" : "joey", "providerId" : "userpass" }, "firstName" : "Joey", "lastName" : "Smith", "fullName" : "Joey Smith", "email" : "joey@mydomain.com", "authMethod" : { "method" : "userPassword" }, "passwordInfo" : { "hasher" : "bcrypt", "password" : "$2a$10$lWxiZz3XJcj3ckOOTTgleOo3tBy2zZvO.LfX9jeUtatHENcTHQ.hS" } }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top