本文整理汇总了Scala中com.mohiva.play.silhouette.api.LoginInfo类的典型用法代码示例。如果您正苦于以下问题:Scala LoginInfo类的具体用法?Scala LoginInfo怎么用?Scala LoginInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LoginInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: save
//设置package包名称以及导入依赖的类
package daos.util.impl
import java.util.UUID
import models.slick.TableDefinitions
import com.mohiva.play.silhouette.api.LoginInfo
import models.User
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import slick.dbio.DBIOAction
import javax.inject.Inject
import daos.util.UserDAO
import models.slick.SlickDAO
import play.api.db.slick.DatabaseConfigProvider
import scala.concurrent.Future
def save(user: User) = {
val userID = user.userID.toString
val balance = 0
val userType = user.userType.toInt
val email = user.email
val dbUser = DBUser(userID, balance, userType, email)
val dbLoginInfo = DBLoginInfo(None, user.loginInfo.providerID, user.loginInfo.providerKey)
// We don't have the LoginInfo id so we try to get it first.
// If there is no LoginInfo yet for this user we retrieve the id on insertion.
val loginInfoAction = {
val retrieveLoginInfo = slickLoginInfos.filter(
info => info.providerID === user.loginInfo.providerID &&
info.providerKey === user.loginInfo.providerKey).result.headOption
val insertLoginInfo = slickLoginInfos.returning(slickLoginInfos.map(_.id)).
into((info, id) => info.copy(id = Some(id))) += dbLoginInfo
for {
loginInfoOption <- retrieveLoginInfo
loginInfo <- loginInfoOption.map(DBIO.successful(_)).getOrElse(insertLoginInfo)
} yield loginInfo
}
// combine database actions to be run sequentially
val actions = (for {
_ <- slickUsers.insertOrUpdate(dbUser)
loginInfo <- loginInfoAction
_ <- slickUserLoginInfos += DBUserLoginInfo(dbUser.userID, loginInfo.id.get)
} yield ()).transactionally
// run actions and return user afterwards
db.run(actions).map(_ => user)
}
}
示例2: User
//设置package包名称以及导入依赖的类
package models
import java.util.UUID
import com.mohiva.play.silhouette.api.{ Identity, LoginInfo }
import utils.misc.Glicko
case class User(
userID: UUID,
loginInfo: LoginInfo,
handle: Option[String],
regpass: Option[String],
firstName: Option[String],
lastName: Option[String],
fullName: Option[String],
email: Option[String],
avatarURL: Option[String],
activated: Boolean,
admin: Boolean = false,
rating: Double = Glicko.rating0,
rd: Double = Glicko.rd0,
lastrated: Double = 0.0
) extends Identity {
def name = fullName.orElse {
firstName -> lastName match {
case (Some(f), Some(l)) => Some(f + " " + l)
case (Some(f), None) => Some(f)
case (None, Some(l)) => Some(l)
case _ => None
}
}
def setActivated(a: Boolean) = this.copy(activated = a)
}
示例3: AuthenticatorQueries
//设置package包名称以及导入依赖的类
package models.queries
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import jdub.async.{ Statement, Row, FlatSingleRowQuery }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
object AuthenticatorQueries extends BaseQueries[CookieAuthenticator] {
override protected val tableName = "session_info"
override protected val columns = Seq("id", "provider", "key", "last_used", "expiration", "fingerprint", "created")
override protected val searchColumns = Seq("id::text", "key")
val insert = Insert
val getById = GetById
val removeById = RemoveById
case class FindSessionInfoByLoginInfo(l: LoginInfo) extends FlatSingleRowQuery[CookieAuthenticator] {
override val sql = getSql(Some("provider = ? and key = ?"))
override val values = Seq(l.providerID, l.providerKey)
override def flatMap(row: Row) = Some(fromRow(row))
}
case class UpdateAuthenticator(ca: CookieAuthenticator) extends Statement {
override val sql = updateSql(Seq("provider", "key", "last_used", "expiration", "fingerprint"))
override val values = Seq(
ca.loginInfo.providerID,
ca.loginInfo.providerKey,
ca.lastUsedDateTime.toLocalDateTime,
ca.expirationDateTime.toLocalDateTime,
ca.fingerprint,
ca.id
)
}
override protected def fromRow(row: Row) = {
val id = row.as[String]("id")
val provider = row.as[String]("provider")
val key = row.as[String]("key")
val lastUsed = row.as[LocalDateTime]("last_used").toDateTime
val expiration = row.as[LocalDateTime]("expiration").toDateTime
val idleTimeout = None
val cookieMaxAge = None
val fingerprint = row.asOpt[String]("fingerprint")
CookieAuthenticator(id, LoginInfo(provider, key), lastUsed, expiration, idleTimeout, cookieMaxAge, fingerprint)
}
override protected def toDataSeq(ca: CookieAuthenticator) = Seq(
ca.id,
ca.loginInfo.providerID,
ca.loginInfo.providerKey,
ca.lastUsedDateTime.toLocalDateTime,
ca.expirationDateTime.toLocalDateTime,
ca.fingerprint,
new LocalDateTime()
)
}
示例4: OAuth1InfoQueries
//设置package包名称以及导入依赖的类
package models.queries
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OAuth1Info
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
object OAuth1InfoQueries extends BaseQueries[OAuth1Info] {
override protected val tableName = "oauth1_info"
override protected val columns = Seq("provider", "key", "token", "secret", "created")
override protected val idColumns = Seq("provider", "key")
override protected val searchColumns = Seq("key", "token")
val getById = GetById
val removeById = RemoveById
case class CreateOAuth1Info(l: LoginInfo, o: OAuth1Info) extends Statement {
override val sql = insertSql
override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
}
case class UpdateOAuth1Info(l: LoginInfo, o: OAuth1Info) extends Statement {
override val sql = s"update $tableName set token = ?, secret = ?, created = ? where provider = ? and key = ?"
override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
}
override protected def fromRow(row: Row) = {
val token = row.as[String]("token")
val secret = row.as[String]("secret")
OAuth1Info(token, secret)
}
override protected def toDataSeq(o: OAuth1Info) = Seq(o.token, o.secret, new LocalDateTime())
}
示例5: ProfileQueries
//设置package包名称以及导入依赖的类
package models.queries
import java.util.UUID
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.CommonSocialProfile
import jdub.async.queries.BaseQueries
import jdub.async.{ Query, Row, FlatSingleRowQuery }
import org.joda.time.LocalDateTime
object ProfileQueries extends BaseQueries[CommonSocialProfile] {
override protected val tableName = "user_profiles"
override protected val columns = Seq("provider", "key", "email", "first_name", "last_name", "full_name", "avatar_url", "created")
override protected val searchColumns = Seq("key", "email", "first_name", "last_name", "full_name")
val insert = Insert
val remove = RemoveById
case class FindProfile(provider: String, key: String) extends FlatSingleRowQuery[CommonSocialProfile] {
override val sql = getSql(Some("provider = ? and key = ?"))
override val values = Seq(provider, key)
override def flatMap(row: Row) = Some(fromRow(row))
}
case class FindProfilesByUser(id: UUID) extends Query[List[CommonSocialProfile]] {
override val sql = s"select ${columns.mkString(", ")} from $tableName p " +
"where (p.provider || ':' || p.key) in (select unnest(profiles) from users where users.id = ?)"
override val values = Seq(id)
override def reduce(rows: Iterator[Row]) = rows.map(fromRow).toList
}
override protected def fromRow(row: Row) = {
val loginInfo = LoginInfo(
providerID = row.as[String]("provider"),
providerKey = row.as[String]("key")
)
val firstName = row.asOpt[String]("first_name")
val lastName = row.asOpt[String]("last_name")
val fullName = row.asOpt[String]("full_name")
val email = row.asOpt[String]("email")
val avatarUrl = row.asOpt[String]("avatar_url")
CommonSocialProfile(loginInfo, firstName, lastName, fullName, email, avatarUrl)
}
override protected def toDataSeq(p: CommonSocialProfile) = Seq(
p.loginInfo.providerID, p.loginInfo.providerKey, p.email, p.firstName, p.lastName, p.fullName, p.avatarURL, new LocalDateTime()
)
}
示例6: OpenIdInfoQueries
//设置package包名称以及导入依赖的类
package models.queries
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OpenIDInfo
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
import play.api.libs.json.{ JsValue, Json }
object OpenIdInfoQueries extends BaseQueries[OpenIDInfo] {
override protected val tableName = "openid_info"
override protected val columns = Seq("provider", "key", "id", "attributes", "created")
override protected val idColumns = Seq("provider", "key")
override protected val searchColumns = Seq("key")
val getById = GetById
val removeById = RemoveById
case class CreateOpenIdInfo(l: LoginInfo, o: OpenIDInfo) extends Statement {
override val sql = insertSql
override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
}
case class UpdateOpenIdInfo(l: LoginInfo, o: OpenIDInfo) extends Statement {
override val sql = s"update $tableName set id = ?, attributes = ?, created = ? where provider = ? and key = ?"
val attributes = Json.prettyPrint(Json.toJson(o.attributes))
override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
}
override protected def fromRow(row: Row) = {
val id = row.as[String]("id")
val attributesString = row.as[String]("attributes")
val attributes = Json.parse(attributesString).as[Map[String, JsValue]].map(x => x._1 -> x._2.as[String])
OpenIDInfo(id, attributes)
}
override protected def toDataSeq(o: OpenIDInfo) = {
val attributes = Json.prettyPrint(Json.toJson(o.attributes))
Seq(o.id, attributes, new LocalDateTime())
}
}
示例7: PasswordInfoQueries
//设置package包名称以及导入依赖的类
package models.queries
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.api.util.PasswordInfo
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
object PasswordInfoQueries extends BaseQueries[PasswordInfo] {
override protected val tableName = "password_info"
override protected val columns = Seq("provider", "key", "hasher", "password", "salt", "created")
override protected def idColumns = Seq("provider", "key")
override protected val searchColumns = Seq("key")
val getById = GetById
val removeById = RemoveById
case class CreatePasswordInfo(l: LoginInfo, p: PasswordInfo) extends Statement {
override val sql = insertSql
override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(p)
}
case class UpdatePasswordInfo(l: LoginInfo, p: PasswordInfo) extends Statement {
override val sql = s"update $tableName set hasher = ?, password = ?, salt = ?, created = ? where provider = ? and key = ?"
override val values = toDataSeq(p) ++ Seq(l.providerID, l.providerKey)
}
override protected def fromRow(row: Row) = PasswordInfo(
hasher = row.as[String]("hasher"),
password = row.as[String]("password"),
salt = row.asOpt[String]("salt")
)
override protected def toDataSeq(p: PasswordInfo) = Seq(p.hasher, p.password, p.salt, new LocalDateTime())
}
示例8: OAuth2InfoQueries
//设置package包名称以及导入依赖的类
package models.queries
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OAuth2Info
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
import play.api.libs.json.{ JsValue, Json }
object OAuth2InfoQueries extends BaseQueries[OAuth2Info] {
override protected val tableName = "oauth2_info"
override protected val columns = Seq("provider", "key", "access_token", "token_type", "expires_in", "refresh_token", "params", "created")
override protected val idColumns = Seq("provider", "key")
override protected val searchColumns = Seq("key", "access_token")
val getById = GetById
val removeById = RemoveById
case class CreateOAuth2Info(l: LoginInfo, o: OAuth2Info) extends Statement {
override val sql = insertSql
override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
}
case class UpdateOAuth2Info(l: LoginInfo, o: OAuth2Info) extends Statement {
override val sql = {
s"update $tableName set access_token = ?, token_type = ?, expires_in = ?, refresh_token = ?, params = ?, created = ? where provider = ? and key = ?"
}
override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
}
override protected def fromRow(row: Row) = {
val params = row.asOpt[String]("params").map { p =>
Json.parse(p).as[Map[String, JsValue]].map(x => x._1 -> x._2.as[String])
}
OAuth2Info(
accessToken = row.as[String]("access_token"),
tokenType = row.asOpt[String]("token_type"),
expiresIn = row.asOpt[Int]("expires_in"),
refreshToken = row.asOpt[String]("refresh_token"),
params = params
)
}
override protected def toDataSeq(o: OAuth2Info) = {
val params = o.params.map(p => Json.prettyPrint(Json.toJson(p)))
Seq(o.accessToken, o.tokenType, o.expiresIn, o.refreshToken, params, new LocalDateTime())
}
}
示例9: User
//设置package包名称以及导入依赖的类
package models.user
import java.util.UUID
import com.mohiva.play.silhouette.api.{ Identity, LoginInfo }
import org.joda.time.LocalDateTime
case class User(
id: UUID,
username: Option[String],
profiles: Seq[LoginInfo],
roles: Set[Role] = Set(Role.User),
created: LocalDateTime
) extends Identity {
def isGuest = profiles.isEmpty
def isAdmin = roles.contains(models.user.Role.Admin)
}
示例10: PasswordInfoService
//设置package包名称以及导入依赖的类
package services.user
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.api.util.PasswordInfo
import com.mohiva.play.silhouette.impl.daos.DelegableAuthInfoDAO
import jdub.async.Database
import models.queries.PasswordInfoQueries
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.Future
object PasswordInfoService extends DelegableAuthInfoDAO[PasswordInfo] {
override def find(loginInfo: LoginInfo) = Database.query(PasswordInfoQueries.getById(Seq(loginInfo.providerID, loginInfo.providerKey)))
override def save(loginInfo: LoginInfo, authInfo: PasswordInfo) = {
Database.transaction { conn =>
Database.execute(PasswordInfoQueries.UpdatePasswordInfo(loginInfo, authInfo), Some(conn)).flatMap { rowsAffected =>
if (rowsAffected == 0) {
Database.execute(PasswordInfoQueries.CreatePasswordInfo(loginInfo, authInfo), Some(conn)).map(x => authInfo)
} else {
Future.successful(authInfo)
}
}
}
}
override def add(loginInfo: LoginInfo, authInfo: PasswordInfo): Future[PasswordInfo] = {
Database.execute(PasswordInfoQueries.CreatePasswordInfo(loginInfo, authInfo)).map(x => authInfo)
}
override def update(loginInfo: LoginInfo, authInfo: PasswordInfo): Future[PasswordInfo] = {
Database.execute(PasswordInfoQueries.UpdatePasswordInfo(loginInfo, authInfo)).map(x => authInfo)
}
override def remove(loginInfo: LoginInfo) = {
Database.execute(PasswordInfoQueries.removeById(Seq(loginInfo.providerID, loginInfo.providerKey))).map(x => Unit)
}
}
示例11: OpenIdInfoService
//设置package包名称以及导入依赖的类
package services.user
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.daos.DelegableAuthInfoDAO
import com.mohiva.play.silhouette.impl.providers.OpenIDInfo
import jdub.async.Database
import models.queries.OpenIdInfoQueries
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.Future
object OpenIdInfoService extends DelegableAuthInfoDAO[OpenIDInfo] {
override def find(loginInfo: LoginInfo) = Database.query(OpenIdInfoQueries.getById(Seq(loginInfo.providerID, loginInfo.providerKey)))
override def save(loginInfo: LoginInfo, authInfo: OpenIDInfo) = {
Database.transaction { conn =>
Database.execute(OpenIdInfoQueries.UpdateOpenIdInfo(loginInfo, authInfo), Some(conn)).flatMap { rowsAffected =>
if (rowsAffected == 0) {
Database.execute(OpenIdInfoQueries.CreateOpenIdInfo(loginInfo, authInfo), Some(conn)).map(x => authInfo)
} else {
Future.successful(authInfo)
}
}
}
}
override def add(loginInfo: LoginInfo, authInfo: OpenIDInfo) = {
Database.execute(OpenIdInfoQueries.CreateOpenIdInfo(loginInfo, authInfo)).map(x => authInfo)
}
override def update(loginInfo: LoginInfo, authInfo: OpenIDInfo) = {
Database.execute(OpenIdInfoQueries.UpdateOpenIdInfo(loginInfo, authInfo)).map(x => authInfo)
}
override def remove(loginInfo: LoginInfo) = {
Database.execute(OpenIdInfoQueries.removeById(Seq(loginInfo.providerID, loginInfo.providerKey))).map(x => Unit)
}
}
示例12: OAuth2InfoService
//设置package包名称以及导入依赖的类
package services.user
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.daos.DelegableAuthInfoDAO
import com.mohiva.play.silhouette.impl.providers.OAuth2Info
import jdub.async.Database
import models.queries.OAuth2InfoQueries
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.Future
object OAuth2InfoService extends DelegableAuthInfoDAO[OAuth2Info] {
override def find(loginInfo: LoginInfo) = Database.query(OAuth2InfoQueries.getById(Seq(loginInfo.providerID, loginInfo.providerKey)))
override def save(loginInfo: LoginInfo, authInfo: OAuth2Info) = {
Database.transaction { conn =>
Database.execute(OAuth2InfoQueries.UpdateOAuth2Info(loginInfo, authInfo), Some(conn)).flatMap { rowsAffected =>
if (rowsAffected == 0) {
Database.execute(OAuth2InfoQueries.CreateOAuth2Info(loginInfo, authInfo), Some(conn)).map(x => authInfo)
} else {
Future.successful(authInfo)
}
}
}
}
override def add(loginInfo: LoginInfo, authInfo: OAuth2Info) = {
Database.execute(OAuth2InfoQueries.CreateOAuth2Info(loginInfo, authInfo)).map(x => authInfo)
}
override def update(loginInfo: LoginInfo, authInfo: OAuth2Info) = {
Database.execute(OAuth2InfoQueries.UpdateOAuth2Info(loginInfo, authInfo)).map(x => authInfo)
}
override def remove(loginInfo: LoginInfo) = {
Database.execute(OAuth2InfoQueries.removeById(Seq(loginInfo.providerID, loginInfo.providerKey))).map(x => Unit)
}
}
示例13: OAuth1InfoService
//设置package包名称以及导入依赖的类
package services.user
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.daos.DelegableAuthInfoDAO
import com.mohiva.play.silhouette.impl.providers.OAuth1Info
import jdub.async.Database
import models.queries.OAuth1InfoQueries
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.Future
object OAuth1InfoService extends DelegableAuthInfoDAO[OAuth1Info] {
override def find(loginInfo: LoginInfo) = Database.query(OAuth1InfoQueries.getById(Seq(loginInfo.providerID, loginInfo.providerKey)))
override def save(loginInfo: LoginInfo, authInfo: OAuth1Info) = {
Database.transaction { conn =>
Database.execute(OAuth1InfoQueries.UpdateOAuth1Info(loginInfo, authInfo), Some(conn)).flatMap { rowsAffected =>
if (rowsAffected == 0) {
Database.execute(OAuth1InfoQueries.CreateOAuth1Info(loginInfo, authInfo), Some(conn)).map(x => authInfo)
} else {
Future.successful(authInfo)
}
}
}
}
override def add(loginInfo: LoginInfo, authInfo: OAuth1Info) = {
Database.execute(OAuth1InfoQueries.CreateOAuth1Info(loginInfo, authInfo)).map(x => authInfo)
}
override def update(loginInfo: LoginInfo, authInfo: OAuth1Info) = {
Database.execute(OAuth1InfoQueries.UpdateOAuth1Info(loginInfo, authInfo)).map(x => authInfo)
}
override def remove(loginInfo: LoginInfo) = {
Database.execute(OAuth1InfoQueries.removeById(Seq(loginInfo.providerID, loginInfo.providerKey))).map(x => Unit)
}
}
示例14: PasswordInfoDao
//设置package包名称以及导入依赖的类
package daos
import javax.inject.Inject
import scala.concurrent.{Await, Future}
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.api.util.PasswordInfo
import com.mohiva.play.silhouette.persistence.daos.DelegableAuthInfoDAO
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json._
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json._
import reactivemongo.play.json.collection.JSONCollection
import models.User
import User._
import scala.concurrent.duration.Duration
class PasswordInfoDao @Inject() (
val reactiveMongoApi: ReactiveMongoApi
) extends DelegableAuthInfoDAO[PasswordInfo] {
val users = Await.result(reactiveMongoApi.database.map(_.collection[JSONCollection]("users")), Duration.Inf)
def find(loginInfo:LoginInfo):Future[Option[PasswordInfo]] = for {
user <- users.find(Json.obj(
"profiles.loginInfo" -> loginInfo
)).one[User]
} yield user.flatMap(_.profiles.find(_.loginInfo == loginInfo)).flatMap(_.passwordInfo)
def add(loginInfo:LoginInfo, authInfo:PasswordInfo):Future[PasswordInfo] =
users.update(Json.obj(
"profiles.loginInfo" -> loginInfo
), Json.obj(
"$set" -> Json.obj("profiles.$.passwordInfo" -> authInfo)
)).map(_ => authInfo)
def update(loginInfo:LoginInfo, authInfo:PasswordInfo):Future[PasswordInfo] =
add(loginInfo, authInfo)
def save(loginInfo:LoginInfo, authInfo:PasswordInfo):Future[PasswordInfo] =
add(loginInfo, authInfo)
def remove(loginInfo:LoginInfo):Future[Unit] =
users.update(Json.obj(
"profiles.loginInfo" -> loginInfo
), Json.obj(
"$pull" -> Json.obj(
"profiles" -> Json.obj("loginInfo" -> loginInfo)
)
)).map(_ => ())
}
示例15: OAuth1InfoDao
//设置package包名称以及导入依赖的类
package daos
import javax.inject.Inject
import scala.concurrent.{Await, Future}
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OAuth1Info
import com.mohiva.play.silhouette.persistence.daos.DelegableAuthInfoDAO
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json._
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json._
import reactivemongo.play.json.collection.JSONCollection
import models.User
import User._
import scala.concurrent.duration.Duration
class OAuth1InfoDao @Inject() (
val reactiveMongoApi: ReactiveMongoApi
) extends DelegableAuthInfoDAO[OAuth1Info] {
val users = Await.result(reactiveMongoApi.database.map(_.collection[JSONCollection]("users")), Duration.Inf)
def find(loginInfo:LoginInfo):Future[Option[OAuth1Info]] = for {
user <- users.find(Json.obj(
"profiles.loginInfo" -> loginInfo
)).one[User]
} yield user.flatMap(_.profiles.find(_.loginInfo == loginInfo)).flatMap(_.oauth1Info)
def add(loginInfo:LoginInfo, authInfo:OAuth1Info):Future[OAuth1Info] =
users.update(Json.obj(
"profiles.loginInfo" -> loginInfo
), Json.obj(
"$set" -> Json.obj("profiles.$.oauth1Info" -> authInfo)
)).map(_ => authInfo)
def update(loginInfo:LoginInfo, authInfo:OAuth1Info):Future[OAuth1Info] =
add(loginInfo, authInfo)
def save(loginInfo:LoginInfo, authInfo:OAuth1Info):Future[OAuth1Info] =
add(loginInfo, authInfo)
def remove(loginInfo:LoginInfo):Future[Unit] =
users.update(Json.obj(
"profiles.loginInfo" -> loginInfo
), Json.obj(
"$pull" -> Json.obj(
"profiles" -> Json.obj("loginInfo" -> loginInfo)
)
)).map(_ => ())
}