アプリケーションを に移植Play 2.2
し、最新バージョンの を使用するようにいくつかのコードを変更しSecureSocial
ました - ケース クラスのUserId
名前が に変更されたことを確認しましたIdentityId
。さて、私は新しいユーザーを登録することができました...データはMongoDBに正常に保存されました:
> 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" } }
...しかし、ログインしようとするとすぐに-もちろん、これはMongoDbからデータを読み取ることを意味し、おもちゃは機能しなくなります。NoSuchElementException
データがデータベースから読み取られないという事実のために、私は常に取得します。
以下は私の完全なコードです:
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)
}
ご覧のとおり、私は複合キーを使用しています...おそらく、メソッドfind
とfindByEmailAndProvider
.
EDIT1:
...そしてもちろん、次のシリアライゼーション/デシリアライゼーションも実装しました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 _)
}