PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/server/app/models/User.scala

https://gitlab.com/guodman/webcbv
Scala | 122 lines | 104 code | 18 blank | 0 comment | 11 complexity | 6692e211399d1056ea9023814f7150ae MD5 | raw file
  1. package models
  2. import java.security.MessageDigest
  3. import scala.collection.JavaConverters._
  4. import com.mongodb.casbah.Imports._
  5. import java.util.UUID
  6. class User(val username: String) {
  7. var _id: ObjectId = null
  8. var salt: String = User.newSalt
  9. var passwordHash: String = User.hash("123", salt)
  10. var currentlyReadingPath: String = null
  11. var currentlyReadingName: String = null
  12. var places: Map[String, Int] = Map()
  13. def setPassword(password: String) = {
  14. salt = User.newSalt
  15. passwordHash = User.hash(password, salt)
  16. }
  17. def checkPassword(pass: String): Boolean = {
  18. return User.hash(pass, salt) == passwordHash
  19. }
  20. def savePlace(comic: Comic, place: Int) {
  21. places = places + (comic.path.replaceAll("""[\.]""", "") -> place)
  22. }
  23. def getPlace(comic: Comic): Int = {
  24. return places.getOrElse(comic.path.replaceAll("""[\.]""", ""), 1)
  25. }
  26. def getInProgress: List[Comic] = {
  27. return places.map({ keyVal =>
  28. val key = keyVal._1
  29. val place = keyVal._2
  30. val mapping = SlugMapping.getByKey(key)
  31. if (mapping != null) {
  32. val comic = Comic.getByPath(mapping.path)
  33. if (comic.isValid && place > 0 && place < mapping.pageCount) {
  34. comic
  35. } else {
  36. play.api.Logger.debug("Not reading comic for key " + key)
  37. null
  38. }
  39. } else {
  40. play.api.Logger.debug("No mapping for key " + key)
  41. null
  42. }
  43. }).filter({
  44. _ != null
  45. }).toList
  46. }
  47. def clearPlace(comic: Comic) {
  48. play.api.Logger.debug("clearing reading status for " + comic.path)
  49. places = places - comic.path.replaceAll("""[\.]""", "")
  50. }
  51. def save = {
  52. var builder = MongoDBObject.newBuilder
  53. builder += "_id" -> _id
  54. builder += "username" -> username
  55. builder += "passwordHash" -> passwordHash
  56. builder += "salt" -> salt
  57. builder += "currentlyReadingPath" -> currentlyReadingPath
  58. builder += "currentlyReadingName" -> currentlyReadingName
  59. val dbPlaces: DBObject = places
  60. builder += "places" -> dbPlaces
  61. User.dbCollection += builder.result
  62. }
  63. }
  64. object User {
  65. val dbCollection = Constants.db("user")
  66. def factory(db: DBObject): User = {
  67. val r = new User(db.getAsOrElse[String]("username", ""))
  68. r.passwordHash = db.getAsOrElse[String]("passwordHash", "")
  69. r.salt = db.getAsOrElse[String]("salt", User.newSalt)
  70. r.currentlyReadingPath = db.getAsOrElse[String]("currentlyReadingPath", null)
  71. r.currentlyReadingName = db.getAsOrElse[String]("currentlyReadingName", null)
  72. var places: Map[String, Int] = Map()
  73. db.getAsOrElse[BasicDBObject]("places", new BasicDBObject())
  74. .toMap.asScala
  75. .foreach( { t => places = places + (t._1.asInstanceOf[String] -> t._2.asInstanceOf[Int]) } )
  76. r.places = places
  77. r._id = db.getAs[ObjectId]("_id").get
  78. return r
  79. }
  80. def get(id: ObjectId): User = {
  81. dbCollection.findOneByID(id).map({ dbobj => factory(dbobj) }).getOrElse[User](null)
  82. }
  83. def get(id: Option[String]): Option[User] = {
  84. if (id.isDefined) {
  85. Option(get(new ObjectId(id.get)))
  86. } else {
  87. None
  88. }
  89. }
  90. def getByUsername(username: String): User = {
  91. val r = dbCollection.findOne(MongoDBObject("username" -> ("(?i)" + username).r))
  92. return r.map(factory(_)).headOption.getOrElse[User](null)
  93. }
  94. def all: List[User] = {
  95. dbCollection.find.map({ dbobj => factory(dbobj) }).toList
  96. }
  97. def hash(pass: String, salt: String): String = {
  98. val toHash = pass + salt
  99. val sha1 = MessageDigest.getInstance("SHA1")
  100. sha1.reset()
  101. return new sun.misc.BASE64Encoder().encode(sha1.digest(toHash.getBytes))
  102. }
  103. def newSalt = UUID.randomUUID.toString
  104. }