Commit 4c7fab34 authored by Mathieu's avatar Mathieu
Browse files

Implement user data update

parent d5d652f6
......@@ -20,7 +20,6 @@ import scala.concurrent.Future
import scala.scalajs.js.typedarray.{ArrayBuffer, TypedArrayBuffer}
import scaladget.bootstrapnative.bsn._
import scaladget.tools._
import scalatags.JsDom.all._
......@@ -30,26 +29,37 @@ object AdminPanel {
@JSExportTopLevel("admin")
def admin() = {
// val users: Var[Seq[UserData]] = Var(Seq())
println("admin ...")
lazy val rows: Var[Seq[ExpandableRow]] = Var(Seq())
// val users: Var[Seq[UserData]] = 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))
implicit def userDataSeqToRows(userData: Seq[UserData]): Seq[ExpandableRow] = userData.map { u =>
buildExpandable(u.name, u.email, u.password, u.role, running)
}
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)
lazy val roles = Seq(user, shared.Data.admin)
lazy val roleFilter = (r: Role) => r == shared.Data.admin
lazy val rows: Var[Seq[ExpandableRow]] = Var(Seq())
val roles = Seq(user, shared.Data.admin)
val roleFilter = (r: Role) => r == shared.Data.admin
def save(expandableRow: ExpandableRow, name: TextCell, email: TextCell, password: PasswordCell, role: LabelCell, status: Status): Unit = {
//SET USER
val userRole: Role = role.get
val modifiedUser = UserData(name.get, email.get, password.get, userRole)
Post[AdminApi].updated(modifiedUser).call().foreach {
rows() = _
}
}
def closeAll(except: ExpandableRow) = rows.now.filterNot {
_ == except
}.foreach {
_.subRow.trigger() = false
}
def buildExpandable(userName: String, userEmail: String, userPassword: String, userRole: Role, userStatus: Status, expanded: Boolean = false): ExpandableRow = {
......@@ -116,20 +126,11 @@ object AdminPanel {
}
Post[AdminApi].users().call().foreach { us =>
rows() = us.map{u=>
buildExpandable(u.name, u.email, u.password, u.role, running)
}
println("US " + us)
rows() = us
}
// 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
)
......@@ -142,28 +143,9 @@ object AdminPanel {
}
)
// 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(editablePanel.render)
}
}
......
package org.openmoleconnect.server
import shared.Data.UserData
import DB._
object AdminApiImpl extends shared.AdminApi {
def users() = {
println("in users server")
val u = DBQueries.users
println("users " + u)
u
DB.users
}
}
def updated(userData: UserData): Seq[UserData] = {
DB.uuid(Email(userData.email)).foreach { id =>
update(toUser(id, userData))
}
users
}
//object AdminRequest {
// private val requestPrefix = "shared/AdminApi"
//
// private val requests = Seq(
// s"$requestPrefix/users"
// )
//
// def isAdminRequest(path: String) = requests.contains(path)
//}
\ No newline at end of file
}
\ No newline at end of file
......@@ -60,7 +60,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
def withAdminRights(action: TokenData=> ActionResult): Serializable = {
withAccesToken { tokenData =>
DBQueries.isAdmin(tokenData.email) match {
DB.isAdmin(tokenData.email) match {
case true=> action(tokenData)
case false=> Unauthorized("You seem unauthorized to do this !")
}
......@@ -76,7 +76,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
def connectionAppRedirection = {
withAccesToken { tokenData =>
if (DBQueries.isAdmin(tokenData.email)) {
if (DB.isAdmin(tokenData.email)) {
Ok(adminHtml)
}
else {
......
......@@ -2,10 +2,13 @@ package org.openmoleconnect.server
import java.util
import shared.Data.UserData
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import slick.jdbc.H2Profile.api._
import DBQueries._
import shared._
object DB {
......@@ -24,7 +27,9 @@ object DB {
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.name.value, 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) }
def toUser(uuid: UUID, userData: UserData): User = User(userData.name, Email(userData.email), Password(userData.password), Role(userData.role), uuid)
class Users(tag: Tag) extends Table[(UUID, String, Email, Password, Role)](tag, "USERS") {
def uuid = column[UUID]("UUID", O.PrimaryKey)
......@@ -47,26 +52,7 @@ object DB {
url = s"jdbc:h2:/${Settings.location}/db"
)
def users =
Await.result(
db.run(userTable.result).map { x =>
x.map {
case (uuid, name, email, password, role) => User(name, email, password, role, uuid)
}
}, Duration.Inf
)
// 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(email: Email): Option[UUID] = users.find(_.email == email).map {
_.uuid
}
def uuid(email: Email, password: Password): Option[UUID] = users.find(u => u.email == email && u.password == password).map {
_.uuid
}
// TRANSACTIONS
def runTransaction[E <: Effect](actions: DBIOAction[_, NoStream, E]*) =
Await.result(
db.run(
......@@ -86,11 +72,47 @@ object DB {
}
def addUser(name: String, email: Email, password: Password, role: Role, uuid: UUID): Unit = {
if (!DBQueries.exists(email)) {
if (!exists(email)) {
runTransaction(
userTable += (uuid, name, email, password, role)
)
}
}
def update(user: User) = {
println("user updated " + user.name)
runTransaction(
getQuery(user.email).update(user.uuid, user.name, user.email, user.password, user.role)
)
}
//QUERIES
// 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(email: Email): Option[UUID] = users.find(_.email == email).map {
_.uuid
}
def uuid(email: Email, password: Password): Option[UUID] = users.find(u => u.email == email && u.password == password).map {
_.uuid
}
def get(email: Email) = {
runQuery(
getQuery(email)
).headOption
}
def users = runQuery(
for {
u <- userTable
} yield (u)
)
def exists(email: Email) = get(email).isDefined
def isAdmin(email: Email) = get(email).map {
_.role
} == Some(admin)
}
......@@ -19,22 +19,10 @@ object DBQueries {
), Duration.Inf
).map { case (u, n, e, p, r) => User(n, e, p, r, u) }
def exists(email: Email) = get(email).isDefined
def isAdmin(email: Email) = get(email).map{_.role} == Some(admin)
def get(email: Email) = {
runQuery(
for {
u <- userTable if (u.email === email)
} yield (u)
).headOption
}
def users = runQuery(
// Query statements
def getQuery(email: Email) =
for {
u <- userTable
u <- userTable if (u.email === email)
} yield (u)
)
}
......@@ -5,4 +5,6 @@ import shared.Data.UserData
trait AdminApi {
def users(): Seq[UserData]
def updated(userData: UserData): Seq[UserData]
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ object Data {
val off: Status = "Off"
val error: Status = "Error"
case class UserData(name: String, email: String, password: String, role: Role, uuid: String)
case class UserData(name: String, email: String, password: String, role: Role)
case class UserStatus(uuid: String, status: Status)
......
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