PageRenderTime 37ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/play/app/models/Role.scala

https://github.com/Bochenski/stackcanon
Scala | 77 lines | 63 code | 13 blank | 1 comment | 0 complexity | 557d30d7cc4b1bc5b7e06fc099bc1066 MD5 | raw file
  1. package models
  2. import play._
  3. import play.libs.Codec;
  4. import com.mongodb.casbah.Imports._
  5. import net.liftweb.json._
  6. import net.liftweb.json.JsonAST
  7. import net.liftweb.json.JsonDSL._
  8. import org.h2.command.ddl.CreateRole
  9. class Role(o: DBObject) extends DBInstance("Role", o) {
  10. lazy val oid = o.getAs[ObjectId]("_id")
  11. lazy val name = o.getAs[String]("name")
  12. def getName = {
  13. name match {
  14. case Some(x) => x
  15. case None => ""
  16. }
  17. }
  18. def getIdString = {
  19. oid match {
  20. case Some(x) => x.toString
  21. case None => ""
  22. }
  23. }
  24. }
  25. object Role extends DBBase[Role]("Roles") {
  26. def create(name: String) = {
  27. val builder = MongoDBObject.newBuilder
  28. builder += "name" -> name
  29. val newObj = builder.result().asDBObject
  30. coll += newObj
  31. true
  32. }
  33. def destroy(id: String) = {
  34. remove(MongoDBObject("_id" -> new ObjectId(id)))
  35. }
  36. def getNames : Iterable[String] = {
  37. all map { role => role.name.get}
  38. }
  39. def findByName(name: String) = {
  40. findOneBy("name", name)
  41. }
  42. def init() {
  43. //make sure we have a sysadmin, admin and user roles defined
  44. CreateRoleIfMissing("sysadmin")
  45. CreateRoleIfMissing("admin")
  46. CreateRoleIfMissing("user")
  47. }
  48. def getRoleIdList(roles : List[String] ) = {
  49. val roleIdList = roles.map (role => CreateRoleIfMissing(role) )
  50. roleIdList
  51. }
  52. private def CreateRoleIfMissing(role: String) = {
  53. findByName(role) match {
  54. case None => {
  55. create(role)
  56. var newRole = findByName(role)
  57. newRole.get.getIdString
  58. }
  59. case Some(existingRole) => {
  60. existingRole.getIdString
  61. }
  62. }
  63. }
  64. }