PageRenderTime 2428ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/play/app/models/User.scala

https://github.com/Bochenski/stackcanon
Scala | 246 lines | 212 code | 31 blank | 3 comment | 6 complexity | ec2381b6d5ac11aaeed322e45126eba4 MD5 | raw file
  1. package models
  2. import scala.xml._
  3. import play._
  4. import data.validation.Valid
  5. import mvc.Scope.Flash
  6. import play.libs.Codec;
  7. import com.mongodb.casbah.Imports._
  8. import net.liftweb.json._
  9. import net.liftweb.json.JsonAST
  10. import net.liftweb.json.JsonDSL._
  11. class User(o: DBObject) extends DBInstance("User", o) {
  12. lazy val oid = o.getAs[ObjectId]("_id")
  13. lazy val username = o.getAs[String]("username")
  14. lazy val first_name = o.getAs[String]("first_name")
  15. lazy val surname = o.getAs[String]("surname")
  16. lazy val password = o.getAs[String]("password")
  17. lazy val google_open_id = o.getAs[String]("google_open_id")
  18. lazy val facebook_id = o.getAs[String]("facebook_id")
  19. lazy val _roles = o.getAs[BasicDBList]("roles")
  20. def isInRole(role: String) = {
  21. //first find the role in the Roles Table
  22. models.Role.findByName(role) match {
  23. case Some(dbRole) => {
  24. _roles match {
  25. case Some(roles) => {
  26. val roleStrings = roles.toList collect { case s: String => s}
  27. val result = roleStrings.contains(dbRole.getIdString)
  28. Logger.info(username + " is in role " + role + "? :" + result.toString)
  29. result
  30. }
  31. case _ => {
  32. Logger.info(username + " is in no roles")
  33. false
  34. }
  35. }
  36. }
  37. case None => {
  38. Logger.error("role" + role + " not found in Roles List")
  39. false
  40. }
  41. }
  42. }
  43. def getUserRoles = {
  44. _roles match {
  45. case Some(roles) => {
  46. val roleList = (roles.toList map (role => {
  47. Logger.info("role " + role.toString)
  48. models.Role.findById(new ObjectId(role.toString))
  49. }))
  50. roleList
  51. }
  52. case None => null
  53. }
  54. }
  55. def getUserRoleIdStrings() :List[String] = {
  56. _roles match {
  57. case Some(roles) => {
  58. val roleList = (roles.toList map (role => role.toString))
  59. roleList
  60. }
  61. case None => {
  62. val empty = List("")
  63. empty
  64. }
  65. }
  66. }
  67. def getId() = {
  68. oid match {
  69. case Some(id) => id
  70. case None => null
  71. }
  72. }
  73. def getIdString() = {
  74. oid match {
  75. case Some(id) => id.toString
  76. case None => ""
  77. }
  78. }
  79. def getFirstName = {
  80. first_name match {
  81. case Some(name) => name
  82. case None => ""
  83. }
  84. }
  85. def getLastName = {
  86. surname match {
  87. case Some(lastName) => lastName
  88. case None => ""
  89. }
  90. }
  91. def getUsername = {
  92. username match {
  93. case Some(thing) => thing
  94. case None => ""
  95. }
  96. }
  97. }
  98. object User extends DBBase[User]("Users") {
  99. override def allXML = <Users>{super.allXML}</Users>
  100. def login(username: String, password: String) = findOneBy(MongoDBObject("username" -> username.toLowerCase, "password" -> Codec.hexMD5(password)))
  101. def findByUsername(username: String) = findOneBy("username", username.toLowerCase)
  102. def findByGoogleOpenID(id: String) = findOneBy("google_open_id", id)
  103. def findByFacebookID(id: String) = findOneBy("facebook_id", id)
  104. def create(username: String, first_name: String, surname: String, password: String, google_open_id: String, facebook_id: String, roles: List[String]) = {
  105. Logger.info("in model create user")
  106. //check whether the user exists
  107. val lowerUser = username.toLowerCase
  108. val user = findByUsername(lowerUser)
  109. user match {
  110. case Some(_) => false
  111. case None => {
  112. val builder = MongoDBObject.newBuilder
  113. builder += "username" -> lowerUser
  114. builder += "first_name" -> first_name
  115. builder += "surname" -> surname
  116. builder += "password" -> Codec.hexMD5(password)
  117. if (!roles.contains("sysadmin")) {
  118. builder += "roles" -> checkForFirstUser(roles)
  119. } else {
  120. builder += "roles" -> models.Role.getRoleIdList(roles)
  121. }
  122. builder += "google_open_id" -> google_open_id
  123. builder += "facebook_id" -> facebook_id
  124. val newObj = builder.result().asDBObject
  125. coll += newObj
  126. Logger.info("Created username %s", lowerUser)
  127. true
  128. }
  129. }
  130. }
  131. def update(username: String, first_name: String, surname: String, password: String, password_confirm: String) :Boolean = {
  132. models.User.findByUsername(username) match {
  133. case Some(user) => {
  134. update(MongoDBObject("_id" -> user.getId), $set("first_name" -> first_name, "surname" -> surname))
  135. true
  136. }
  137. case None => {
  138. Logger.error("no user found to update with username " + username)
  139. false
  140. }
  141. }
  142. }
  143. def getUsersInRole(role: String) = {
  144. findManyByMatchingArrayContent("roles", MongoDBObject(role -> 1))
  145. }
  146. def addUserToRole(userId: String, roleId: String) {
  147. findById(new ObjectId(userId)) match {
  148. case Some(user) => {
  149. models.Role.findById(new ObjectId(roleId)) match {
  150. case Some(role) => {
  151. if(!(user.isInRole(role.getName))){
  152. update(MongoDBObject("_id" -> new ObjectId(userId)), $set("roles" -> (user._roles.get :+ role.getIdString)))
  153. }
  154. }
  155. case None => {
  156. Logger.error("role not found to add to user")
  157. }
  158. }
  159. }
  160. case None => {
  161. Logger.error("user not found to add role to")
  162. }
  163. }
  164. }
  165. def removeUserFromRole(userId: String, roleId: String) {
  166. findById(new ObjectId(userId)) match {
  167. case Some(user) => {
  168. models.Role.findById(new ObjectId(roleId)) match {
  169. case Some(role) => {
  170. Logger.info(user._roles.get.toString)
  171. val newRoles = user._roles.get.diff(List(role.getIdString))
  172. Logger.info(newRoles.toString)
  173. update(MongoDBObject("_id" -> new ObjectId(userId)), $set("roles" -> newRoles))
  174. }
  175. case None => {
  176. Logger.error("role not found to remove from user")
  177. }
  178. }
  179. }
  180. case none => {
  181. Logger.error("user not found to remove role from")
  182. }
  183. }
  184. }
  185. def create(username: String, first_name: String, surname: String, password: String, roles: List[String]): Boolean =
  186. create(username, first_name, surname, password, "", "", roles)
  187. private var _hasUsers = false
  188. private def checkForFirstUser(roles: List[String]) : List[String] = {
  189. //called as each user is created, is this our first user, if so, make them a super user
  190. if (!_hasUsers) {
  191. if (coll.count == 0) {
  192. Logger.info("making this user a sys admin")
  193. _hasUsers = true
  194. return models.Role.getRoleIdList(roles) ++ models.Role.getRoleIdList(List("sysadmin"))
  195. }
  196. }
  197. models.Role.getRoleIdList(roles)
  198. }
  199. def associateWithGoogleOpenID(user: User, id: String) {
  200. User.addField(user, "google_open_id", id)
  201. update(user)
  202. }
  203. def dissasociateWithGoogleOpenID(user: User) {
  204. User.addField(user, "google_open_id", "")
  205. update(user)
  206. }
  207. def associateWithFacebookID(user: User, id: String) {
  208. User.addField(user, "facebook_id", id)
  209. update(user)
  210. }
  211. def dissacociateWithFacebookID(user: User) {
  212. User.addField(user, "facebook_id", "")
  213. update(user)
  214. }
  215. }