Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package models
- import java.sql.PreparedStatement
- import be.objectify.deadbolt.core.models.Subject
- import play.api.db.DB
- import play.libs.Scala
- import anorm._
- import anorm.SqlParser._
- import play.api.Play.current
- /**
- * Created by Konrad on 2015-05-03.
- */
- case class User(
- id: Long,
- userName: String,
- password: String,
- description: String,
- photo: Array[Byte],
- photoType: String,
- telNumber: String,
- webPage: String
- ) extends Subject
- {
- def getRoles: java.util.List[SecurityRole] = {
- Scala.asJava(List(new SecurityRole("foo"),
- new SecurityRole("bar")))
- }
- def getPermissions: java.util.List[UserPermission] = {
- Scala.asJava(List(new UserPermission("printers.edit")))
- }
- def getIdentifier: String = userName
- }
- object User {
- val user = {
- get[Long]("id") ~
- get[String]("userName") ~
- get[String]("password") ~
- get[String]("description") ~
- get[Array[Byte]]("photo") ~
- get[String]("photoType") ~
- get[String]("telNumber") ~
- get[String]("webPage") map {
- case id~userName~
- password~description~
- photo~photoType~
- telNumber~webPage =>
- User(id, userName,
- password, description,
- photo, photoType,
- telNumber, webPage)
- }
- }
- def all(): List[User] = DB.withConnection { implicit c =>
- SQL("select * from _user").as(user *)
- }
- def create(userName: String, password: String) = DB.withConnection { implicit c =>
- //TODO: hash
- SQL("insert into _user (userName, password) values ({userName}, {password})").on(
- 'userName -> userName,
- 'password -> password
- ).executeUpdate()
- }
- implicit object byteArrayToStatement extends ToStatement[Array[Byte]] {
- def set(s: PreparedStatement, i: Int, array: Array[Byte]): Unit = {
- s.setBlob(i, new javax.sql.rowset.serial.SerialBlob(array))
- }
- }
- def update(user: User) = DB.withConnection { implicit c =>
- SQL(
- "update _user SET " +
- "description = {description}," +
- "photo = {photo}," +
- "photoType = {photoType}," +
- "telNumber = {telNumber}," +
- "webPage = {webPage} " +
- "WHERE id = {id}"
- ).on(
- 'description -> user.description,
- 'photo -> user.photo,
- 'photoType -> user.photoType,
- 'telNumber -> user.telNumber,
- 'webPage -> user.webPage,
- 'id -> user.id
- ).executeUpdate()
- }
- def delete(id: Long) = DB.withConnection { implicit c =>
- SQL("delete from _user where id = {id}").on(
- 'id -> id
- ).executeUpdate()
- }
- def delete(userName: String) = DB.withConnection { implicit c =>
- SQL("delete from _user where userName = {userName}").on(
- 'userName -> userName
- ).executeUpdate()
- }
- def findByUserNameAndPassword(userName: String, password: String): User ={
- DB.withConnection{ implicit c => SQL("select * from _user where userName = {userName} AND password = {password}").on(
- "userName" -> userName,
- "passowrd" -> password
- ).as(user singleOpt).head
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement