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/5075578 to your computer and use it in GitHub Desktop.
Save dsugden/5075578 to your computer and use it in GitHub Desktop.
Roles for DeadBolt2
package models
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
sealed trait USER_ROLES
case object ADMIN extends USER_ROLES
case object CLINIC_ADMIN extends USER_ROLES
case object THERAPIST extends USER_ROLES
object UserRoleImplicits {
trait UserRole extends Role{
def getName:String
}
implicit def stringToRole(s:String):Role = {
new UserRole{override def getName = s}
}
implicit def stringListToRole(list:List[String]):Seq[Role] = {
for(s <- list) yield stringToRole(s)
}
}
case class UserRoles(userId: Long, role: String)
trait UserRoleComponent {
this: Profile =>
import profile.simple._
object UserRolesTable extends Table[UserRoles]("UserRoles") {
def userId = column[Long]("userId", O NotNull)
def role = column[String]("role", O NotNull)
def * = userId ~ role <> (UserRoles, UserRoles.unapply _)
def forInsert = userId ~ role <>
({ (userId, role) => UserRoles(userId, role) },
{ u: UserRoles => Some((u.userId, u.role)) })
def setRoles(userId: Long, roles: List[USER_ROLES])(implicit db: Session) =
for (r <- roles) {
UserRolesTable.forInsert.insert(UserRoles(userId, r.toString()))
}
def getRoles(userId: Long)(implicit db: Session): List[String] =
UserRolesTable.filter(ur => ur.userId === userId).map(ur => ur.role).list
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment