/commons/src/main/scala/io/prediction/commons/settings/mongodb/MongoUsers.scala

https://github.com/SNaaS/PredictionIO · Scala · 98 lines · 78 code · 19 blank · 1 comment · 0 complexity · 3981295d7116e2b8a183950ea76bb02e MD5 · raw file

  1. package io.prediction.commons.settings.mongodb
  2. import io.prediction.commons.MongoUtils
  3. import io.prediction.commons.settings.{ User, Users }
  4. import com.mongodb.casbah.Imports._
  5. import com.mongodb.casbah.WriteConcern
  6. /** MongoDB implementation of Users. */
  7. class MongoUsers(db: MongoDB) extends Users {
  8. private val emptyObj = MongoDBObject()
  9. private val userColl = db("users")
  10. private val seq = new MongoSequences(db)
  11. userColl.setWriteConcern(WriteConcern.JournalSafe)
  12. def authenticate(id: Int, password: String) = {
  13. userColl.findOne(MongoDBObject("_id" -> id, "password" -> password) ++ ("confirm" $exists false)) map { _ => true } getOrElse false
  14. }
  15. def authenticateByEmail(email: String, password: String) = {
  16. userColl.findOne(MongoDBObject("email" -> email, "password" -> password) ++ ("confirm" $exists false)) map { _.as[Int]("_id") }
  17. }
  18. def insert(email: String, password: String, firstname: String, lastname: Option[String], confirm: String) = {
  19. val id = seq.genNext("userid")
  20. val userObj = MongoDBObject(
  21. "_id" -> id,
  22. "email" -> email,
  23. "password" -> password,
  24. "firstname" -> firstname,
  25. "confirm" -> confirm)
  26. val lastnameObj = lastname.map(ln => MongoDBObject("lastname" -> ln)).getOrElse(emptyObj)
  27. userColl.save(userObj ++ lastnameObj)
  28. id
  29. }
  30. def get(id: Int) = {
  31. userColl.findOne(MongoDBObject("_id" -> id)) map { dbObjToUser(_) }
  32. }
  33. def getAll() = new MongoUserIterator(userColl.find())
  34. def getByEmail(email: String) = {
  35. userColl.findOne(MongoDBObject("email" -> email)) map { dbObjToUser(_) }
  36. }
  37. def update(user: User, upsert: Boolean = false) = {
  38. val requiredObj = MongoDBObject(
  39. "_id" -> user.id,
  40. "email" -> user.email,
  41. "password" -> user.password,
  42. "firstname" -> user.firstName)
  43. val lastnameObj = user.lastName map { x => MongoDBObject("lastname" -> x) } getOrElse { MongoUtils.emptyObj }
  44. val confirmObj = user.confirm map { x => MongoDBObject("confirm" -> x) } getOrElse { MongoUtils.emptyObj }
  45. userColl.update(MongoDBObject("_id" -> user.id), requiredObj ++ lastnameObj ++ confirmObj, upsert)
  46. }
  47. def updateEmail(id: Int, email: String) = {
  48. userColl.update(MongoDBObject("_id" -> id), MongoDBObject("$set" -> MongoDBObject("email" -> email)))
  49. }
  50. def updatePassword(userid: Int, password: String) = {
  51. userColl.update(MongoDBObject("_id" -> userid), MongoDBObject("$set" -> MongoDBObject("password" -> password)))
  52. }
  53. def updatePasswordByEmail(email: String, password: String) = {
  54. userColl.update(MongoDBObject("email" -> email), MongoDBObject("$set" -> MongoDBObject("password" -> password)))
  55. }
  56. def confirm(confirm: String) = {
  57. userColl.findAndModify(MongoDBObject("confirm" -> confirm), MongoDBObject("$unset" -> MongoDBObject("confirm" -> 1))) map { dbObjToUser(_) }
  58. }
  59. def emailExists(email: String) = {
  60. userColl.findOne(MongoDBObject("email" -> email)).map(_ => true).getOrElse(false)
  61. }
  62. def idAndEmailExists(userid: Int, email: String) = {
  63. userColl.findOne(MongoDBObject("_id" -> userid, "email" -> email)).map(_ => true).getOrElse(false)
  64. }
  65. private def dbObjToUser(dbObj: DBObject): User = {
  66. User(
  67. id = dbObj.as[Int]("_id"),
  68. firstName = dbObj.as[String]("firstname"),
  69. lastName = dbObj.getAs[String]("lastname"),
  70. email = dbObj.as[String]("email"),
  71. password = dbObj.as[String]("password"),
  72. confirm = dbObj.getAs[String]("confirm")
  73. )
  74. }
  75. class MongoUserIterator(it: MongoCursor) extends Iterator[User] {
  76. def next = dbObjToUser(it.next)
  77. def hasNext = it.hasNext
  78. }
  79. }