Commit efdd86a5 authored by Mathieu's avatar Mathieu
Browse files

Implements addUser function

parent 9720d50e
......@@ -36,12 +36,15 @@ object Application extends App {
config.launchMode match {
case HelpMode println(usage)
case ServerMode =>
if (!Settings.location.exists)
Settings.location.mkdirs()
DB.initDB()
DB.initDB
val server = new ConnectServer(secret = config.tokenSecret)
server.start()
}
// DB.addUser(DB.Login("moo"), DB.Password("moo"), "moo@me.com")
println(DB.users)
}
......@@ -127,7 +127,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
//Build cookie with JWT token if login/password are valid and redirect to the openmole manager url
else {
DB.uuid(DB.User(DB.Login(login), DB.Password(password))) match {
DB.uuid(DB.Login(login), DB.Password(password)) match {
case Some(uuid) =>
val host = Host(uuid, K8sService.hostIP(uuid))
buildAndAddCookieToHeader(TokenData.accessToken(host, DB.Login(login)))
......
package org.openmoleconnect.server
import java.util
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import slick.jdbc.H2Profile.api._
object DB {
......@@ -9,43 +12,52 @@ object DB {
case class UUID(value: String) extends MappedTo[String]
case class Login(value: String)
case class Login(value: String) extends MappedTo[String]
case class Password(value: String) extends MappedTo[String]
case class User(login: Login, password: Password, uuid: UUID = UUID(""))
case class User(login: Login, password: Password, email: String, uuid: UUID = UUID(""))
class Users(tag: Tag) extends Table[(UUID, String, Password)](tag, "USERS") {
class Users(tag: Tag) extends Table[(UUID, Login, Password, String)](tag, "USERS") {
def uuid = column[UUID]("UUID", O.PrimaryKey)
def login = column[String]("LOGIN")
def login = column[Login]("LOGIN")
def password = column[Password]("PASSWORD")
def * = (uuid, login, password)
def email = column[String]("EMAIL")
def * = (uuid, login, password, email)
}
val userTable = TableQuery[Users]
val db: Database = Database.forDriver(
driver = new org.h2.Driver,
url = s"jdbc:h2:/${Settings.location}/db"
)
def users =
Await.result(
db.run(userTable.result).map { x =>
x.map {
case (uuid, login, password, email) => User(login, password, email, uuid)
}
}, Duration.Inf
)
//Seq(User(Login("foo"), Password("foo"), UUID("foo-123-567-foo")), User(Login("bar"), Password("bar"), UUID("bar-123-567-bar")))
private val users = Seq(User(Login("foo"), Password("foo"), UUID("foo-123-567-foo")), User(Login("bar"), Password("bar"), UUID("bar-123-567-bar")))
def uuid(login: Login): Option[UUID] = users.find(_.login == login).map {
_.uuid
}
def uuid(user: User): Option[UUID] = users.find(u => u.login == user.login && u.password == user.password).map {
def uuid(login: Login, password: Password): Option[UUID] = users.find(u => u.login == login && u.password == password).map {
_.uuid
}
lazy val db: Database = Database.forDriver(
driver = new org.h2.Driver,
url = s"jdbc:h2:/${Settings.location}/db"
)
def runTransaction[E <: Effect](actions: DBIOAction[_, NoStream, E]*) =
Await.result(
db.run(
......@@ -53,8 +65,14 @@ object DB {
actions: _*
).transactionally), Duration.Inf)
def initDB() = {
def initDB = {
runTransaction(userTable.schema.createIfNotExists)
}
def addUser(login: Login, password: Password, email: String) =
if (!users.contains{u: User=> u.email == email}) {
runTransaction(
userTable += (UUID(util.UUID.randomUUID().toString), login, password, email)
)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment