Skip to content

Instantly share code, notes, and snippets.

@dsugden
Last active December 14, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsugden/5075590 to your computer and use it in GitHub Desktop.
Save dsugden/5075590 to your computer and use it in GitHub Desktop.
User for deadblot
package models
//import play.api.db._
import java.sql.Date
import play.Logger
import scala.util.{ Try, Success, Failure }
import play.cache.Cache
import org.mindrot.jbcrypt.BCrypt
import play.api.libs.Crypto
import play.libs.Scala
import be.objectify.deadbolt.core.models.Subject
import be.objectify.deadbolt.core.models.Role
import be.objectify.deadbolt.core.models.Permission
import slick.session.Session
import models.UserRoleImplicits.stringListToRole
case class User(id: Option[Long] = None,
email: String,
password: String,
name: String) extends Subject {
def getRoles: java.util.List[Role] = {
Scala.asJava {
AppDB.database.withSession { implicit session: Session =>
id.fold(List[String]()) { userId =>
AppDB.dal.UserRolesTable.getRoles(userId)
}
}
}
}
def getPermissions: java.util.List[Permission] = {
Scala.asJava(List())
}
def getIdentifier: String = email
}
trait UsersComponent {
this: Profile =>
import profile.simple._
case class UserSelect(id: Long,
email: String)
implicit def userToSelect(user: User): UserSelect = UserSelect(user.id.get, user.email)
object Users extends Table[User]("user") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def email = column[String]("email", O NotNull)
def password = column[String]("password", O NotNull)
def name = column[String]("name", O NotNull)
def * = id.? ~ email ~ password ~ name <> (User, User.unapply _)
def forInsert = email ~ password ~ name <>
({ (email, password, name) => User(None, email, password, name) },
{ u: User => Some((u.email, u.password, u.name)) }) returning id
// try {
// Database.forDataSource(DB.getDataSource()) withSession { implicit db: Session => (Users.ddl).create }
// Logger.debug("Created ClinicTable");
// } catch {
// case e: Exception => Logger.debug(e.getMessage())
// }
def authenticate(email: String, password: String)(implicit db: Session): Try[User] =
Try {
findByEmail(email) match {
case Some(u) => {
if (BCrypt.checkpw(password, u.password)) u else throw new Exception("bad password")
}
case None => throw new Exception("User " + email + " does not exist in this system")
}
}
def findAll(implicit db: Session): List[User] = (for (u <- Users) yield u).list
def findAllForSelection(implicit db: Session): List[UserSelect] = (for (u <- Users) yield u).list map (userToSelect(_))
def findByEmail(email: String)(implicit db: Session): Option[User] =
(for (u <- Users; if u.email === email) yield u).firstOption
def findByEmail(email: Option[String])(implicit db: Session): Option[User] =
email match {
case Some(s) => findByEmail(s)
case _ => None
}
def findById(id: Long)(implicit db: Session): Option[User] =
(for (u <- Users; if u.id === id) yield u).firstOption
def getIdFromEmail(email: String)(implicit db: Session): Option[Long] = {
val query = for (u <- Users; if u.email === email) yield u
try {
val user = query.first
user.id
} catch {
case e: Exception => None
}
}
/**
* email is unique
*/
def create(email: String, password: String, name: String, userRoles:List[USER_ROLES])(implicit db: Session): Try[User] =
Try {
findByEmail(email) match {
case Some(u) => throw new Exception("User " + email + " already exists")
case None => {
val id = Users.forInsert.insert(User(None, email, BCrypt.hashpw(password, BCrypt.gensalt), name))
AppDB.dal.UserRolesTable.setRoles(id, userRoles)
User(Some(id), email, password, name)
}
}
}
// def update(user:User):Either[String,User]
def deleteByEmail(email: String)(implicit db: Session) = {}
// findByEmail(email) match {
// case Some(u) => {
// val bioquery = TeamBioTable where { _.userID === u.id.getOrElse(0L) }
// bioquery delete
// val userQuery = Users where { _.email === email}
// userQuery delete
//
// }
// case None => Unit
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment