/security_manager/app/it/gov/daf/securitymanager/service/MongoService.scala

https://github.com/italia/daf · Scala · 177 lines · 90 code · 45 blank · 42 comment · 2 complexity · 0361265c241e3953f5a2439e3ec94a9b MD5 · raw file

  1. package it.gov.daf.securitymanager.service
  2. import java.util.Calendar
  3. import com.mongodb.casbah.commons.MongoDBObject
  4. import com.mongodb.{BasicDBObject, ServerAddress}
  5. import com.mongodb.casbah.{MongoClient, MongoCredential}
  6. import it.gov.daf.securitymanager.service.utilities.ConfigReader
  7. import play.api.Logger
  8. import play.api.libs.json.{JsValue, Json}
  9. import security_manager.yaml.IpaUser
  10. object MongoService {
  11. private val server = new ServerAddress(ConfigReader.getDbHost, ConfigReader.getDbPort)
  12. private val userName = ConfigReader.userName
  13. private val dbName = ConfigReader.database
  14. private val password = ConfigReader.password
  15. private val credentials = MongoCredential.createCredential(userName, dbName, password.toCharArray)
  16. private val USER_COLLECTION_NAME = "PreRegistratedUsers"
  17. private val RESETPWD_COLLECTION_NAME = "ResetPwdRequests"
  18. private val PRE_REGISTRATION_TTL = 60*60*2
  19. private val RESET_PWD_TTL = 60*60*2
  20. /*
  21. def writeUserData(user:IpaUser, token:String): Boolean = {
  22. val mongoClient = MongoClient( server, List(credentials) )
  23. val db = mongoClient(dbName)
  24. val coll = db(USER_COLLECTION_NAME)
  25. if(coll.isEmpty) {
  26. try
  27. coll.underlying.createIndex(new BasicDBObject("createdOn", 1), new BasicDBObject("expireAfterSeconds", PRE_REGISTRATION_TTL))
  28. catch{ case e:Exception => println("Indice già creato") }
  29. }
  30. val document = new BasicDBObject("token", token).append("uid", user.uid).append("userpassword",user.userpassword).append("givenname",user.givenname).append("sn",user.sn).append("mail",user.mail).append("createdOn", Calendar.getInstance().getTime() )
  31. val inserted = coll.insert(document)
  32. mongoClient.close()
  33. inserted.getN > 0
  34. }*/
  35. def writeUserData(user:IpaUser, token:String): Either[String,String] = {
  36. val document = new BasicDBObject("token", token).append("uid", user.uid).append("userpassword",user.userpassword).append("givenname",user.givenname).append("sn",user.sn).append("mail",user.mail).append("createdOn", Calendar.getInstance().getTime() )
  37. writeData( document, USER_COLLECTION_NAME, PRE_REGISTRATION_TTL )
  38. }
  39. def writeResetPwdData(user:IpaUser, token:String): Either[String,String] = {
  40. val document = new BasicDBObject("token", token).append("uid", user.uid).append("mail",user.mail).append("createdOn", Calendar.getInstance().getTime() )
  41. writeData( document, RESETPWD_COLLECTION_NAME, RESET_PWD_TTL )
  42. }
  43. private def writeData( document:BasicDBObject, collectionName:String, ttl:Int)={
  44. val mongoClient = MongoClient( server, List(credentials) )
  45. val db = mongoClient(dbName)
  46. val coll = db(collectionName)
  47. if(coll.isEmpty) {
  48. try
  49. coll.underlying.createIndex(new BasicDBObject("createdOn", 1), new BasicDBObject("expireAfterSeconds", ttl))
  50. catch{ case e:Exception => Logger.logger.error("Indice già creato") }
  51. }
  52. val inserted = coll.insert(document)
  53. mongoClient.close()
  54. Logger.logger.debug( "mongo write result: "+inserted )
  55. if(! inserted.isUpdateOfExisting )
  56. Right("ok")
  57. else
  58. Left("ko")
  59. }
  60. def findUserByToken(token:String): Either[String,JsValue] = {
  61. findData(USER_COLLECTION_NAME,"token",token)
  62. }
  63. def findAndRemoveUserByToken(token:String): Either[String,JsValue] = {
  64. findAndRemoveData(USER_COLLECTION_NAME,"token",token)
  65. }
  66. def findAndRemoveResetPwdByToken(token:String): Either[String,JsValue] = {
  67. findAndRemoveData(RESETPWD_COLLECTION_NAME,"token",token)
  68. }
  69. def findUserByUid(uid:String): Either[String,JsValue] = {
  70. findData(USER_COLLECTION_NAME,"uid",uid)
  71. }
  72. def findResetPwdByMail(mail:String): Either[String,JsValue] = {
  73. findData(RESETPWD_COLLECTION_NAME,"mail",mail)
  74. }
  75. private def findData(collectionName:String, filterAttName:String, filterValue:String): Either[String,JsValue] = {
  76. val mongoClient = MongoClient(server,List(credentials))
  77. val db = mongoClient(dbName)
  78. val coll = db(collectionName)
  79. val query = MongoDBObject(filterAttName -> filterValue)
  80. val result = coll.findOne(query)
  81. mongoClient.close
  82. result match {
  83. case Some(x) => {
  84. val jsonString = com.mongodb.util.JSON.serialize(x)
  85. Right(Json.parse(jsonString))
  86. }
  87. case None => Left(s"Data in $collectionName not found")
  88. }
  89. }
  90. private def findAndRemoveData(collectionName:String, filterAttName:String, filterValue:String): Either[String,JsValue] = {
  91. val mongoClient = MongoClient(server,List(credentials))
  92. val db = mongoClient(dbName)
  93. val coll = db(collectionName)
  94. val query = MongoDBObject(filterAttName -> filterValue)
  95. val result = coll.findAndRemove(query)
  96. mongoClient.close
  97. result match {
  98. case Some(x) => {
  99. val jsonString = com.mongodb.util.JSON.serialize(x)
  100. Right(Json.parse(jsonString))
  101. }
  102. case None => Left("Not found")
  103. }
  104. }
  105. /*
  106. private def readMongoById(collectionName: String, id: String): JsValue = {
  107. val mongoClient = MongoClient(server,List(credentials))
  108. val db = mongoClient(dbName)
  109. //val collection = db.getCollection(collectionName)
  110. val coll = db(collectionName)
  111. //val result2 = collection.findOne(equal(filterAttName, filterValue))
  112. val query = MongoDBObject("_id" -> new ObjectId(id))
  113. val result = coll.findOne(query)
  114. mongoClient.close
  115. val out: JsValue = result match {
  116. case Some(x) => {
  117. val jsonString = com.mongodb.util.JSON.serialize(x)
  118. Json.parse(jsonString)
  119. }
  120. case None => JsString("Not found")
  121. }
  122. out
  123. }*/
  124. }