From 28d169ab0ff2c4de6a7521284996b83034794ca5 Mon Sep 17 00:00:00 2001 From: Giorgia Rondinini Date: Wed, 9 Oct 2019 12:17:11 +0200 Subject: [PATCH 1/4] Add functions for assignments of processings and cuts --- client/index.js | 127 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 10 deletions(-) diff --git a/client/index.js b/client/index.js index 3df977f..386356a 100644 --- a/client/index.js +++ b/client/index.js @@ -156,14 +156,70 @@ function completeCut() { /* assigns a processing to a cut*/ function assignProcessing() { - // TODO: - showNotYetImplemented("result_area"); + assignAToB("assign_cut_processing", "cuts", "processings", "cut", "processing", + cut => cut._cutCodiceIntaglio, cut => (cut._cutCodiceIntaglio + " -- " + cut._cutDataRichiesta + " -- " + cut._cutCfRichiedente), + processing => processing._processingCodiceLavorazione, processing => processing._processingCodiceLavorazione); } /* shows which processings are assigned to which cuts */ function showCutsAssignments() { - // TODO: - showNotYetImplemented("result_area"); + clearPage(); + var form = document.getElementById("filters_form"); + form.classList.remove("hidden"); + fetchData("cuts", response => { + if (response.ok) { + response.json().then(jsonResponse => { + if (checkIfJsonIsError(jsonResponse) && jsonResponse["response"]["code"] != "200") { + var error = document.createElement("P"); + error.innerHTML = jsonResponse["response"]["message"]; + error.classList.add("error"); + form.appendChild(error); + } else if (!checkIfJsonIsError(jsonResponse)) { + var list = document.createElement("select"); + list.name = "cut"; + list.id = "cut_select"; + var elem = document.createElement("option"); + elem.value = "none"; + elem.innerHTML = ""; + list.appendChild(elem); + form.appendChild(list); + for (const index in jsonResponse) { + var cut = jsonResponse[index]; + elem = document.createElement("option"); + elem.value = cut._cutCodiceIntaglio + elem.innerHTML = cut._cutCodiceIntaglio + " -- " + cut._cutDataRichiesta + " -- " + cut._cutCfRichiedente; + list.appendChild(elem); + } + var resultTable = createTable("result_table", "headers", ["code", "Codice lavorazione"]); + document.getElementById("result_area").appendChild(resultTable); + var setter = (jsonResponse, table) => { + var body = table.getElementsByTagName("tbody")[0]; + showClearElem(body.id); + for (const index in jsonResponse) { + var listElem = document.createElement("tr"); + var composition = jsonResponse[index]; + listElem.classList.add("result_elem"); + var codeCell = document.createElement("td"); + codeCell.innerHTML = composition._compositionCodiceLavorazione; + listElem.append(codeCell); + body.appendChild(listElem); + } + }; + var changer = () => { + if (list.value == "none") { + setTableFromData([], resultTable, setter); + } else { + useChosenData("filters_form", "assignments_cut", data => { + setTableFromData(data, resultTable, setter); + }); + } + }; + changer(); + list.onchange = () => changer(); + } + }); + } + }); } /* shows all the cuts in the database*/ @@ -378,20 +434,71 @@ function completePrint() { /* assigns a filament to a print */ function assignFilament() { - // TODO: - showNotYetImplemented("result_area"); - /* assignAToB("assign_print_filament", "prints", "filaments", "print", "filament", print => print._printCodiceStampa, print => (print._printCodiceStampa + " -- " + print._printDataRichiesta + " -- " + print._printCfRichiedente), filament => filament._filamentCodiceFilamento, filament => (filament._filamentMarca + " " + filament._filamentColore)); - */ } /* shows which filaments are assigned to which prints */ function showPrintsAssignments() { - // TODO: - showNotYetImplemented("result_area"); + clearPage(); + var form = document.getElementById("filters_form"); + form.classList.remove("hidden"); + fetchData("prints", response => { + if (response.ok) { + response.json().then(jsonResponse => { + if (checkIfJsonIsError(jsonResponse) && jsonResponse["response"]["code"] != "200") { + var error = document.createElement("P"); + error.innerHTML = jsonResponse["response"]["message"]; + error.classList.add("error"); + form.appendChild(error); + } else if (!checkIfJsonIsError(jsonResponse)) { + var list = document.createElement("select"); + list.name = "print"; + list.id = "print_select"; + var elem = document.createElement("option"); + elem.value = "none"; + elem.innerHTML = ""; + list.appendChild(elem); + form.appendChild(list); + for (const index in jsonResponse) { + var print = jsonResponse[index]; + elem = document.createElement("option"); + elem.value = print._printCodiceStampa + elem.innerHTML = print._printCodiceStampa + " -- " + print._printDataRichiesta + " -- " + print._printCfRichiedente; + list.appendChild(elem); + } + var resultTable = createTable("result_table", "headers", ["code", "Codice filamento"]); + document.getElementById("result_area").appendChild(resultTable); + var setter = (jsonResponse, table) => { + var body = table.getElementsByTagName("tbody")[0]; + showClearElem(body.id); + for (const index in jsonResponse) { + var listElem = document.createElement("tr"); + var use = jsonResponse[index]; + listElem.classList.add("result_elem"); + var codeCell = document.createElement("td"); + codeCell.innerHTML = use._useCodiceFilamento; + listElem.append(codeCell); + body.appendChild(listElem); + } + }; + var changer = () => { + if (list.value == "none") { + setTableFromData([], resultTable, setter); + } else { + useChosenData("filters_form", "assignments_print", data => { + setTableFromData(data, resultTable, setter); + }); + } + }; + changer(); + list.onchange = () => changer(); + } + }); + } + }); } /* assigns a printer to a print */ From 20990223ab6f27a24dff09afdfecd98a339f34c1 Mon Sep 17 00:00:00 2001 From: Giorgia Rondinini Date: Tue, 22 Oct 2019 09:44:34 +0200 Subject: [PATCH 2/4] Fix select tag behaviour --- client/common.js | 8 ++++++++ client/index.css | 4 +++- client/index.html | 4 ++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/client/common.js b/client/common.js index 62febc6..1e18d14 100644 --- a/client/common.js +++ b/client/common.js @@ -45,6 +45,10 @@ function clearPage() { hideableElements[i].removeChild(hideableElements[i].firstChild); } } + var groupHideableElements = document.getElementsByClassName("group_hideable"); + for (var i = 0; i < groupHideableElements.length; i++) { + groupHideableElements[i].classList.add("hidden"); + } } /* makes visible the element with elemId, as if it was created anew */ @@ -55,6 +59,10 @@ function showClearElem(elemId) { elem.removeChild(elem.firstChild); } elem.classList.remove("hidden"); + while (elem.parentElement) { + elem.parentElement.classList.remove("hidden"); + elem = elem.parentElement; + } } } diff --git a/client/index.css b/client/index.css index 1933519..2417ba2 100644 --- a/client/index.css +++ b/client/index.css @@ -282,13 +282,15 @@ td { select { font-family: 'Asap', sans-serif; font-size: 16px; + width: 100%; color: #142114; padding: 2px; border-radius: 3px; border-color: #c2f56e; - -moz-appearance: none; + cursor: pointer; display: inline-block; margin: 10px 10px 10px 0px; + box-shadow: 2px 2px 2px 2px rgba(126, 177, 44, 0.12), 0 2px 3px 0 rgba(126, 177, 44, 0.24); } input[type="checkbox"].hidden { display: none; diff --git a/client/index.html b/client/index.html index eff95e5..14cb2a6 100644 --- a/client/index.html +++ b/client/index.html @@ -67,10 +67,10 @@
-
+ -
+ From e1ccbecc41ca35b1393ddf81ebc64288b3388aaa Mon Sep 17 00:00:00 2001 From: Giorgia Rondinini Date: Sun, 3 Nov 2019 18:31:05 +0100 Subject: [PATCH 3/4] Change to how the database password is managed, update changelog and readme --- ChangeLog.md | 18 ++++++++++++++++++ README.md | 8 +++----- app/Main.hs | 18 ++++++++++++++++-- package.yaml | 1 + src/Server.hs | 11 +++++------ 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 266afbe..fc57a6a 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,21 @@ # Changelog for haskellDB ## Unreleased changes + +## [V0.1] - 2019/10/09 +### Added +- First working version of the software! + +## [V0.2] - 2019/10/22 +### Added +- Functions for tracking use of processings and filaments + +### Fixed +- Dropdown menu behaviour in the client interface + +## [V0.3] - 2019/11/03 +### Added +- This CHANGELOG + +### Changed +- There is no more need for storing passwords in plain text! Check the README for more info! diff --git a/README.md b/README.md index 011e213..c22f1df 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ It's a lovely mix of Haskell, HTML, Javascript and CSS. dbLocation="localhost" dbPort=5432 dbUser="your_db_user" - dbPswd="your_db_user_password" port=8080 apiName="MSManager" ``` @@ -32,20 +31,19 @@ It's a lovely mix of Haskell, HTML, Javascript and CSS. The following steps are needed to create the first admin user. -- In the GHCI prompt use `connectWithInfo "localhost" 5432 "your_db_user" "your_db_user_password" "name_of_your_db" >>= insertUser "username" "password"`, changing the function arguments with the ones yuo need and wish to use. +- In the GHCI prompt use `connectWithInfo "localhost" 5432 "your_db_user" "your_db_user_password" "name_of_your_db" >>= insertUser "username" "password"`, changing the function arguments with the ones you need and wish to use. - Open pgAdmin (or whatever tool you use to execute queries in the database) and execute the query `UPDATE utenti SET admin = true WHERE username = 'username'`, changing username with the one chosen in the previous step. You have now created your first admin user! -- Return to the GHCI prompt, use `main` +- Return to the GHCI prompt, use `:main PASSWORD`, where PASSWORD is the password of the PostgreSQL user you used in the "server.cfg" file + - If you wish to execute the server outside of the GHCI prompt (and that would be great!) use Stack to build and install the application, and call it from the command line with PASSWORD as the first argument - Open your browser, and connect to `localhost:8080` - The web interface was tested on Firefox, but it should work on other browsers too (please, avoid Internet Explorer) - That's it. Log in, and everything should work! ## A little disclaimer - This system doesn't aim to be safe: at the moment, I don't have the knowledge to make it safe. So, it isn't. Don't use as if it were. In particular, the main problems are: - - the database password is saved in a plain-text file. I think it's pretty clear why that's bad - - knowing that, notice that the users' data is at risk, too - there is no safe connection to the server (no https), so your passwords are sent without encryption - This is not easy to install, and some operations are doable only through SQL queries: again, I have neither the knowledge nor the time to resolve this - The user interface is probably badly written, and not accessible: I'm still learning, and I will (probably) correct it in the future diff --git a/app/Main.hs b/app/Main.hs index a7f1eb4..8477c45 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,12 +2,26 @@ module Main where +import Data.Semigroup ((<>)) +import Options.Applicative +import Query import Schema import Server -import Query configFile :: FilePath configFile = "server.cfg" +passwordParser :: Parser String +passwordParser = argument str (metavar "PASSWORD") + +pswdInfo :: ParserInfo String +pswdInfo = + info (passwordParser <**> helper) + ( fullDesc + <> progDesc "Starts the server application using the PASSWORD for connecting to the database" + ) + main :: IO () -main = parseConfig configFile >>= runServer +main = do + pswd <- execParser pswdInfo + parseConfig configFile >>= (flip runServer pswd) diff --git a/package.yaml b/package.yaml index 10d0d46..d009a6f 100644 --- a/package.yaml +++ b/package.yaml @@ -30,6 +30,7 @@ dependencies: - hvect - memory - mtl +- optparse-applicative - postgresql-libpq - scientific - Spock >= 0.11 diff --git a/src/Server.hs b/src/Server.hs index a8feb36..a3dfd08 100644 --- a/src/Server.hs +++ b/src/Server.hs @@ -42,7 +42,6 @@ data ApiCfg acfg_db_location :: Text, acfg_db_port :: Integer, acfg_db_user :: Text, - acfg_db_pswd :: Text, acfg_port :: Int, acfg_name :: Text } @@ -72,10 +71,9 @@ parseConfig cfgFile = do dbLocation <- C.require cfg "dbLocation" dbPort <- C.require cfg "dbPort" dbUser <- C.require cfg "dbUser" - dbPassword <- C.require cfg "dbPswd" port <- C.require cfg "port" name <- C.require cfg "apiName" - return (ApiCfg db dbLocation dbPort dbUser dbPassword port name) + return (ApiCfg db dbLocation dbPort dbUser port name) -- |Function used to get the connection used to interrogate the database getFabLabConnection @@ -210,13 +208,14 @@ missingParameter :: MonadIO m => ActionCtxT ctx m b missingParameter = messageJson 422 "Parametro mancante" -- server functions -runServer :: ApiCfg -> IO () -runServer cfg = +-- |Runs the server using the given configuration and the password for the database +runServer :: ApiCfg -> String -> IO () +runServer cfg pswd = let ioConn = getFabLabConnection (unpack $ acfg_db_location cfg) (acfg_db_port cfg) (unpack $ acfg_db_user cfg) - (unpack $ acfg_db_pswd cfg) + pswd (unpack $ acfg_db cfg) in do spockCfg <- defaultSpockCfg Nothing (getPoolOrConn ioConn) () From 92492f9368a3e768c7d89d7183ca4b6de45024d2 Mon Sep 17 00:00:00 2001 From: Giorgia Rondinini Date: Wed, 20 Nov 2019 10:37:13 +0100 Subject: [PATCH 4/4] Fix cut operator assignment --- src/Server.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Server.hs b/src/Server.hs index a3dfd08..5cd2f16 100644 --- a/src/Server.hs +++ b/src/Server.hs @@ -455,7 +455,7 @@ app = do else missingParameter post "assign_cut_operator" $ do maybeCf <- param "operator" - maybeCode <- param "print" + maybeCode <- param "cut" if testParameters [maybeCode, maybeCf] then executeModifyQueryAndSendResult