Advertisement
Guest User

Untitled

a guest
May 7th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.24 KB | None | 0 0
  1. package models
  2.  
  3. import java.sql.PreparedStatement
  4.  
  5. import be.objectify.deadbolt.core.models.Subject
  6. import play.api.db.DB
  7. import play.libs.Scala
  8. import anorm._
  9. import anorm.SqlParser._
  10. import play.api.Play.current
  11.  
  12. /**
  13.  * Created by Konrad on 2015-05-03.
  14.  */
  15. case class User(
  16.                  id: Long,
  17.                  userName: String,
  18.                  password: String,
  19.                  description: String,
  20.                  photo: Array[Byte],
  21.                  photoType: String,
  22.                  telNumber: String,
  23.                  webPage: String
  24.                  ) extends Subject
  25. {
  26.   def getRoles: java.util.List[SecurityRole] = {
  27.     Scala.asJava(List(new SecurityRole("foo"),
  28.       new SecurityRole("bar")))
  29.   }
  30.  
  31.   def getPermissions: java.util.List[UserPermission] = {
  32.     Scala.asJava(List(new UserPermission("printers.edit")))
  33.   }
  34.  
  35.   def getIdentifier: String = userName
  36. }
  37.  
  38. object User {
  39.  
  40.  
  41.   val user = {
  42.     get[Long]("id") ~
  43.       get[String]("userName") ~
  44.       get[String]("password") ~
  45.       get[String]("description") ~
  46.       get[Array[Byte]]("photo") ~
  47.       get[String]("photoType") ~
  48.       get[String]("telNumber") ~
  49.       get[String]("webPage") map {
  50.       case id~userName~
  51.         password~description~
  52.         photo~photoType~
  53.         telNumber~webPage =>
  54.         User(id, userName,
  55.           password, description,
  56.           photo, photoType,
  57.           telNumber, webPage)
  58.     }
  59.   }
  60.  
  61.  
  62.   def all(): List[User] = DB.withConnection { implicit c =>
  63.     SQL("select * from _user").as(user *)
  64.   }
  65.  
  66.   def create(userName: String, password: String) = DB.withConnection { implicit c =>
  67.  
  68.     //TODO: hash
  69.  
  70.     SQL("insert into _user (userName, password) values ({userName}, {password})").on(
  71.       'userName -> userName,
  72.       'password -> password
  73.     ).executeUpdate()
  74.   }
  75.  
  76.   implicit object byteArrayToStatement extends ToStatement[Array[Byte]] {
  77.     def set(s: PreparedStatement, i: Int, array: Array[Byte]): Unit = {
  78.       s.setBlob(i, new javax.sql.rowset.serial.SerialBlob(array))
  79.     }
  80.   }
  81.  
  82.   def update(user: User) = DB.withConnection { implicit c =>
  83.     SQL(
  84.       "update _user SET " +
  85.       "description = {description}," +
  86.       "photo = {photo}," +
  87.       "photoType = {photoType}," +
  88.       "telNumber = {telNumber}," +
  89.       "webPage = {webPage} " +
  90.       "WHERE id = {id}"
  91.     ).on(
  92.       'description -> user.description,
  93.       'photo       -> user.photo,
  94.       'photoType   -> user.photoType,
  95.       'telNumber   -> user.telNumber,
  96.       'webPage     -> user.webPage,
  97.       'id          -> user.id
  98.     ).executeUpdate()
  99.   }
  100.  
  101.   def delete(id: Long) = DB.withConnection { implicit c =>
  102.     SQL("delete from _user where id = {id}").on(
  103.       'id -> id
  104.     ).executeUpdate()
  105.   }
  106.  
  107.   def delete(userName: String) = DB.withConnection { implicit c =>
  108.     SQL("delete from _user where userName = {userName}").on(
  109.       'userName -> userName
  110.     ).executeUpdate()
  111.   }
  112.  
  113.   def findByUserNameAndPassword(userName: String, password: String): User ={
  114.       DB.withConnection{ implicit c =>     SQL("select * from _user where userName = {userName} AND password = {password}").on(
  115.         "userName" -> userName,
  116.         "passowrd" -> password
  117.       ).as(user singleOpt).head
  118.     }
  119.   }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement