I succeded to make a persistent UserServicePlugin, which seems to work. See the class DbUserService hereunder. This class considers only the "userpass" provider, which is fixed in the code.
But I am not sure that it is the correct way. My doubts are:
in the "save" function I distinguished the cases in which the user already exists in the DB; if it exists -> update, otherwise -> insert
I didn't persist the "tokens" to the DB; I don't understand well the use of the tokens
Any suggestions will be appreciated.
Best regards,
giovanni
package service
import play.api.{Logger, Application}
import securesocial.core.{UserServicePlugin, UserId, SocialUser}
import java.util.UUID
import org.joda.time.DateTime
import securesocial.core.providers.Token
import securesocial.core.AuthenticationMethod
import securesocial.core.PasswordInfo
import play.api.db._
import anorm._
import play.api.Play.current
/**
�* A Database user service in Scala
�*
�*/
class DbUserService(application: Application) extends UserServicePlugin(application) {
�
// �private var users = Map[String, SocialUser]()
� private var tokens = Map[String, Token]()
� /**
� �* find
� �*/
� def find(id: UserId) = {
� � if (Logger.isDebugEnabled) {
� � � Logger.debug("find...")
� � � Logger.debug("id = %s".format(id.id))
� � }
� �
� � DB.withConnection { implicit c =>
� � � val sqlQuery = SQL(
� � � � � """
� � � � � � select * from USER
� � � � � � where id = {id};
� � � � � """).on("id" -> id.id)
� � � // Transform the resulting Stream[Row] to a List[SocialUser]
� � � val users = sqlQuery().map(row =>
� � � � SocialUser(
� � � � � � UserId(row[String]("id"), "userpass"),
� � � � � � row[String]("first_name"),
� � � � � � row[String]("last_name"),
� � � � � � row[String]("first_name") + " " + row[String]("last_name"),
� � � � � � row[Option[String]]("email"),
� � � � � � None,
� � � � � � AuthenticationMethod("userPassword"),
� � � � � � None,
� � � � � � None,
� � � � � � Some(PasswordInfo(row[String]("password"), None))
� � � � )).toList
� � � �
� � � val socialUser = if (users.size == 1) Some(users(0)) else None
� � �
� � � if (Logger.isDebugEnabled) {
� � � � Logger.debug("socialUser = %s".format(socialUser))
� � � }
� � �
� � � socialUser
� � } // end DB
� �
� } // end find
�
� def findByEmail(email: String, providerId: String): Option[SocialUser] = {
� �
� � if (Logger.isDebugEnabled) {
� � � Logger.debug("findByEmail...")
� � � Logger.debug("email = %s".format(email))
� � � Logger.debug("providerId = %s".format(providerId))
� � }
� �
� � DB.withConnection { implicit c =>
� � � val sqlQuery = SQL(
� � � � � """
� � � � � � select * from USER
� � � � � � where email = {email};
� � � � � """).on("email" -> email)
� � � // Transform the resulting Stream[Row] to a List[SocialUser]
� � � val users = sqlQuery().map(row =>
� � � � SocialUser(
� � � � � � UserId(row[String]("id"), "userpass"),
� � � � � � row[String]("first_name"),
� � � � � � row[String]("last_name"),
� � � � � � row[String]("first_name") + " " + row[String]("last_name"),
� � � � � � row[Option[String]]("email"),
� � � � � � None,
� � � � � � AuthenticationMethod("userPassword"),
� � � � � � None,
� � � � � � None,
� � � � � � Some(PasswordInfo(row[String]("password"), None))
� � � � )).toList
� � � �
� � � val socialUser = if (users.size == 1) Some(users(0)) else None
� � �
� � � if (Logger.isDebugEnabled) {
� � � � Logger.debug("socialUser = %s".format(socialUser))
� � � }
� � �
� � � socialUser
� � } // end DB
� �
� } // end findByEmail
�
� def save(user: SocialUser) {
� � if (Logger.isDebugEnabled) {
� � � Logger.debug("save...")
� � � Logger.debug("user = %s".format(user))
� � }
� � DB.withConnection { implicit c =>
� � �
� � � val sqlSelectQuery = SQL(
� � � � � """
� � � � � � select * from USER
� � � � � � where id = {id};
� � � � � """).on("id" -> user.id.id)
� � � val users = sqlSelectQuery().map(row =>
� � � � SocialUser(
� � � � � � UserId(row[String]("id"), "userpass"),
� � � � � � row[String]("first_name"),
� � � � � � row[String]("last_name"),
� � � � � � row[String]("first_name") + " " + row[String]("last_name"),
� � � � � � row[Option[String]]("email"),
� � � � � � None,
� � � � � � AuthenticationMethod("userPassword"),
� � � � � � None,
� � � � � � None,
� � � � � � Some(PasswordInfo(row[String]("password"), None))
� � � � )).toList
� � � �
� � � val socialUser = if (users.size == 1) Some(users(0)) else None
� � �
� � � if (Logger.isDebugEnabled) {
� � � � Logger.debug("socialUser = %s".format(socialUser))
� � � }
� � �
� � � if (socialUser == None) { // user not exists
� � � � // create a new user
� � � � val sqlQuery = SQL(
� � � � � """
� � � � � � insert into USER
� � � � � � � (id, first_name, last_name, email, password)
� � � � � � values
� � � � � � � ({id}, {first_name}, {last_name}, {email}, {password})
� � � � � """).on(
� � � � � � 'id -> user.id.id,
� � � � � � 'first_name -> user.firstName,
� � � � � � 'last_name -> user.lastName,
� � � � � � 'email -> user.email,
� � � � � � 'password -> user.passwordInfo.get.password)
� � � � val result: Int = sqlQuery.executeUpdate()
� � � � if (Logger.isDebugEnabled) {
� � � � � Logger.debug("result = %s".format(result))
� � � � }
� � � } else { // user exists
� � � �
� � � � // update the user
� � � � val sqlQuery = SQL(
� � � � � """
� � � � � � update USER
� � � � � � � set id = {id},
� � � � � � � � � first_name = {first_name},
� � � � � � � � � last_name = {last_name},
� � � � � � � � � email = {email},
� � � � � � � � � password = {password}
� � � � � � � where id = {id}
� � � � � """).on(
� � � � � � 'id -> user.id.id,
� � � � � � 'first_name -> user.firstName,
� � � � � � 'last_name -> user.lastName,
� � � � � � 'email -> user.email,
� � � � � � 'password -> user.passwordInfo.get.password)
� � � � val result: Int = sqlQuery.executeUpdate()
� � � � if (Logger.isDebugEnabled) {
� � � � � Logger.debug("result = %s".format(result))
� � � � }
� � � } // end else
� � } // end DB
� �
� } // end save
�
� def save(token: Token) {
� � tokens += (token.uuid -> token)
� }
� def findToken(token: String): Option[Token] = {
� � tokens.get(token)
� }
� def deleteToken(uuid: String) {
� � tokens -= uuid
� }
� def deleteTokens() {
� � tokens = Map()
� }
� def deleteExpiredTokens() {
� � tokens = tokens.filter(!_._2.isExpired)
� }
�
�
} // end DbUserService
- show quoted text -
answered
Nov 14 2012 at 01:41
Giovanni