Commit d5d652f6 authored by Mathieu's avatar Mathieu
Browse files

Initiate Admin panel

parent 809fe891
......@@ -3,30 +3,164 @@ package org.openmoleconnect.adminclient
import java.nio.ByteBuffer
import org.scalajs.dom
import scaladget.bootstrapnative.bsn._
import org.scalajs.dom.raw.{Event, HTMLFormElement}
import scalatags.JsDom.all._
import scalatags.JsDom.tags
import scala.scalajs.js.annotation.JSExportTopLevel
import boopickle.Default._
import shared.AdminApi
import autowire._
import rx._
import scaladget.bootstrapnative.Table.StaticSubRow
import scaladget.bootstrapnative._
import scaladget.bootstrapnative.bsn.{glyph_edit2, glyph_save}
import scalatags.JsDom.styles
import shared.Data._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.scalajs.js.typedarray.{ArrayBuffer, TypedArrayBuffer}
import scaladget.bootstrapnative.bsn._
import scaladget.tools._
import scalatags.JsDom.all._
object AdminPanel {
@JSExportTopLevel("admin")
def admin() = {
// val users: Var[Seq[UserData]] = Var(Seq())
println("admin ...")
Post[AdminApi].users().call().foreach {u=>
println("users " +u)
}
lazy val rows: Var[Seq[ExpandableRow]] = Var(Seq())
def save(expandableRow: ExpandableRow, name: TextCell, email: TextCell, password: PasswordCell, role: LabelCell, status: Status) = {
//SET USER
//rows() = rows.now.updated(rows.now.indexOf(expandableRow), buildExpandable(name.get, email.get, password.get, role.get, status, true))
}
def closeAll(except: ExpandableRow) = rows.now.filterNot{_ == except}.foreach{_.subRow.trigger() = false}
lazy val rowFlex = Seq(styles.display.flex, flexDirection.row, justifyContent.spaceAround, alignItems.center)
lazy val columnFlex = Seq(styles.display.flex, flexDirection.column, styles.justifyContent.center)
val roles = Seq(user, shared.Data.admin)
val roleFilter = (r: Role) => r == shared.Data.admin
def buildExpandable(userName: String, userEmail: String, userPassword: String, userRole: Role, userStatus: Status, expanded: Boolean = false): ExpandableRow = {
val aVar = Var(expanded)
def roleStyle(s: Role) =
if (s == shared.Data.admin) label_success
else label_default
val name = TextCell(userName, Some("Name"))
val email = TextCell(userEmail, Some("Email"))
val password = PasswordCell(userPassword, Some("Password"))
val role = LabelCell(userRole, roles, optionStyle = roleStyle, title = Some("Role"))
val rowEdit = Var(false)
val buttonStyle: ModifierSeq = Seq(
fontSize := 22,
color := "#23527c",
opacity := 0.8
)
lazy val aSubRow: StaticSubRow = StaticSubRow({
div(height := 120, rowFlex)(
groupCell.build(margin := 25),
)
}, aVar)
def statusStyle(s: Status) =
if (s == running) label_success
else if (s == off) label_default
else label_danger
lazy val expandableRow: ExpandableRow = ExpandableRow(EditableRow(Seq(
TriggerCell(a(userName, onclick := { () =>
closeAll(expandableRow)
aVar() = !aVar.now
})),
LabelCell(userStatus, Seq(), optionStyle = statusStyle),
)), aSubRow)
lazy val groupCell: GroupCell = GroupCell(
div(rowFlex, width := "100%")(
name.build(padding := 10),
email.build(padding := 10),
password.build(padding := 10),
role.build(padding := 10),
span(
Rx {
if (rowEdit()) glyphSpan(glyph_save +++ buttonStyle +++ toClass("actionIcon"), () => {
rowEdit.update(!rowEdit.now)
save(expandableRow, name, email, password, role, userStatus)
})
else glyphSpan(glyph_edit2 +++ buttonStyle +++ toClass("actionIcon"), () => {
//button("Edit", btn_default, onclick := { () =>
rowEdit.update(!rowEdit.now)
groupCell.switch
})
}
)
), name, email, password, role)
expandableRow
}
Post[AdminApi].users().call().foreach { us =>
rows() = us.map{u=>
buildExpandable(u.name, u.email, u.password, u.role, running)
}
}
// lazy val rows = Var(Seq(
// buildExpandable("Bobi", "bobi@me.com", "mypass", admin, running),
// buildExpandable("Barbara", "barb@gmail.com", "toto", user, off)
// ))
val headerStyle: ModifierSeq = Seq(
height := 40.85
)
val editablePanel = div(
Rx {
div(styles.display.flex, flexDirection.row, styles.justifyContent.center)(
EdiTable(Seq("Name", "Status"), rows()).render(width := "90%")
)
}
)
// val table = div(
// Rx {
// val t = dataTable.
// addHeaders("Email", "Password", "Role")
// users().foreach { u =>
// t.addRow(u.email, u.password, u.role)
// }
// t.render
// }
// ).render
dom.document.body.appendChild(div("Admin").render)
dom.document.body.appendChild(editablePanel.render)
}
......
......@@ -43,6 +43,6 @@ object Application extends App {
server.start()
}
DB.addUser(DB.Email("moo@moo.com"), DB.Password("moo"), DB.simpleUser)
DB.addUser("Moo",DB.Email("moo@moo.com"), DB.Password("moo"), DB.simpleUser)
}
......@@ -8,7 +8,7 @@ val scalatraVersion = "2.6.5"
val jettyVersion = "9.4.19.v20190610"
val json4sVersion = "3.6.7"
val scalatagsVersion = "0.7.0"
val scaladgetVersion = "1.2.7"
val scaladgetVersion = "1.3.0-SNAPSHOT"
val scalajsDomVersion = "0.9.7"
val scalaJWTVersion = "4.0.0"
val rosHttpVersion = "2.2.4"
......@@ -104,8 +104,8 @@ lazy val bootstrap = project.in(file("target/bootstrap")) settings (defaultSetti
dependencyJS: sbt.File,
depsCSS: sbt.File,
targetName: String) = {
IO.copyFile(jsBuild, appTarget / s"webapp/js/${targetName}.js")
IO.copyFile(dependencyJS, appTarget / "webapp/js/${targetName}-deps.js")
IO.copyFile(jsBuild, appTarget / s"webapp/js/$targetName.js")
IO.copyFile(dependencyJS, appTarget / s"webapp/js/$targetName-deps.js")
IO.copyDirectory(depsCSS, appTarget / "webapp/css")
IO.copyDirectory(clientResources, appTarget)
}
......
......@@ -162,7 +162,8 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
else {
DB.uuid(DB.Email(email), DB.Password(password)) match {
case Some(uuid) =>
val host = Host(uuid, K8sService.hostIP(uuid))
//val host = Host(uuid, K8sService.hostIP(uuid))
val host = Host(uuid, None)
buildAndAddCookieToHeader(TokenData.accessToken(host, DB.Email(email)))
buildAndAddCookieToHeader(TokenData.refreshToken(host, DB.Email(email)))
redirect("/")
......
......@@ -22,20 +22,22 @@ object DB {
val admin = Role("admin")
val simpleUser = Role("simpleUser")
case class User(email: Email, password: Password, role: Role = simpleUser, uuid: UUID = UUID(""))
case class User(name: String, email: Email, password: Password, role: Role = simpleUser, uuid: UUID = UUID(""))
implicit def userToUserData(users: Seq[User]): Seq[Data.UserData] = users.map{u=> Data.UserData(u.email.value, u.password.value, u.role.value, u.uuid.value)}
implicit def userToUserData(users: Seq[User]): Seq[Data.UserData] = users.map{u=> Data.UserData(u.name.value, u.email.value, u.password.value, u.role.value, u.uuid.value)}
class Users(tag: Tag) extends Table[(UUID, Email, Password, Role)](tag, "USERS") {
class Users(tag: Tag) extends Table[(UUID, String, Email, Password, Role)](tag, "USERS") {
def uuid = column[UUID]("UUID", O.PrimaryKey)
def name = column[String]("NAME")
def email = column[Email]("EMAIL")
def password = column[Password]("PASSWORD")
def role = column[Role]("ROLE")
def * = (uuid, email, password, role)
def * = (uuid, name, email, password, role)
}
val userTable = TableQuery[Users]
......@@ -49,7 +51,7 @@ object DB {
Await.result(
db.run(userTable.result).map { x =>
x.map {
case (uuid, email, password, role) => User(email, password, role, uuid)
case (uuid, name, email, password, role) => User(name, email, password, role, uuid)
}
}, Duration.Inf
)
......@@ -75,18 +77,18 @@ object DB {
def initDB = {
runTransaction(userTable.schema.createIfNotExists)
if (DB.users.isEmpty) {
DB.addUser(DB.Email("admin@admin.com"), DB.Password("admin"), DB.admin, UUID("foo-123-567-foo"))
DB.addUser("admin", DB.Email("admin@admin.com"), DB.Password("admin"), DB.admin, UUID("foo-123-567-foo"))
}
}
def addUser(email: Email, password: Password, role: Role = simpleUser): Unit = {
addUser(email, password, role, UUID(util.UUID.randomUUID().toString))
def addUser(name: String, email: Email, password: Password, role: Role = simpleUser): Unit = {
addUser(name, email, password, role, UUID(util.UUID.randomUUID().toString))
}
def addUser(email: Email, password: Password, role: Role, uuid: UUID): Unit = {
def addUser(name: String, email: Email, password: Password, role: Role, uuid: UUID): Unit = {
if (!DBQueries.exists(email)) {
runTransaction(
userTable += (uuid, email, password, role)
userTable += (uuid, name, email, password, role)
)
}
}
......
......@@ -10,14 +10,14 @@ import scala.concurrent.ExecutionContext.Implicits.global
import shared._
object DBQueries {
type UserQuery = Query[Users, (UUID, Email, Password, Role), Seq]
type UserQuery = Query[Users, (UUID, String, Email, Password, Role), Seq]
def runQuery(query: UserQuery) =
Await.result(
db.run(
query.result
), Duration.Inf
).map { case (u, e, p, r) => User(e, p, r, u) }
).map { case (u, n, e, p, r) => User(n, e, p, r, u) }
def exists(email: Email) = get(email).isDefined
......
......@@ -4,6 +4,18 @@ object Data {
val connectionRoute = "/connection"
val adminRoutePrefix = "shared/AdminApi"
case class UserData(email: String, password: String, role: String, uuid: String)
type Role = String
val admin: Role = "Admin"
val user: Role = "User"
type Status = String
val running: Status = "Running"
val off: Status = "Off"
val error: Status = "Error"
case class UserData(name: String, email: String, password: String, role: Role, uuid: String)
case class UserStatus(uuid: String, status: Status)
}
\ No newline at end of file
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