Commit 15cb97c9 authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Re-activated all report functionality with proper authentication through KeyCloak.

parent 134d89fc
Pipeline #79278 skipped with stage
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -2,12 +2,12 @@ window.nomadEnv = { ...@@ -2,12 +2,12 @@ window.nomadEnv = {
//apiRoot: "https://labdev-nomad.esc.rzg.mpg.de/dev/nomad/encyclopedia-api/", //apiRoot: "https://labdev-nomad.esc.rzg.mpg.de/dev/nomad/encyclopedia-api/",
//guiRoot: "https://labdev-nomad.esc.rzg.mpg.de/dev/nomad/encyclopedia-api/", //guiRoot: "https://labdev-nomad.esc.rzg.mpg.de/dev/nomad/encyclopedia-api/",
//userCookieDomain: ".esc.rzg.mpg.de", //userCookieDomain: ".esc.rzg.mpg.de",
userCookieDomain: ".nomad-lab.eu", //userCookieDomain: ".nomad-lab.eu",
apiRoot: "https://nomad-lab.eu/prod/rae/api/encyclopedia/", //apiRoot: "https://nomad-lab.eu/prod/rae/api/encyclopedia/",
//guiRoot: "https://nomad-lab.eu/prod/rae/encyclopedia/", //guiRoot: "https://nomad-lab.eu/prod/rae/encyclopedia/",
guiRoot: "http://localhost:3000/gui/", guiRoot: "http://localhost:3000/gui/",
//apiRoot: "/", apiRoot: "http://localhost:3000/",
//userCookieDomain: ".localhost", userCookieDomain: ".localhost",
guestUserToken: 'eyJhbGciOiJIUzI1NiIsImlhdCI6MTUyMzg4MDE1OSwiZXhwIjoxNjgxNTYwMTU5fQ.ey'+ guestUserToken: 'eyJhbGciOiJIUzI1NiIsImlhdCI6MTUyMzg4MDE1OSwiZXhwIjoxNjgxNTYwMTU5fQ.ey'+
'JpZCI6ImVuY2d1aSJ9.MsMWQa3IklH7cQTxRaIRSF9q8D_2LD5Fs2-irpWPTp4', 'JpZCI6ImVuY2d1aSJ9.MsMWQa3IklH7cQTxRaIRSF9q8D_2LD5Fs2-irpWPTp4',
keycloakBase: 'https://nomad-lab.eu/fairdi/keycloak/auth/', keycloakBase: 'https://nomad-lab.eu/fairdi/keycloak/auth/',
......
...@@ -64,8 +64,6 @@ flaggingFormPopup.innerHTML =` ...@@ -64,8 +64,6 @@ flaggingFormPopup.innerHTML =`
</div> </div>
`; `;
// Form elements
let categoryField = flaggingFormPopup.querySelector('#flagging-category'); let categoryField = flaggingFormPopup.querySelector('#flagging-category');
let eStructOption = categoryField.querySelector('option[value="electronicstruct"]'); let eStructOption = categoryField.querySelector('option[value="electronicstruct"]');
let thermalOption = categoryField.querySelector('option[value="thermalprops"]'); let thermalOption = categoryField.querySelector('option[value="thermalprops"]');
...@@ -74,21 +72,16 @@ let closeButton = flaggingFormPopup.querySelector('img'); ...@@ -74,21 +72,16 @@ let closeButton = flaggingFormPopup.querySelector('img');
let validationMsg = flaggingFormPopup.querySelector('#form-validation-msg'); let validationMsg = flaggingFormPopup.querySelector('#form-validation-msg');
let sendButton = flaggingFormPopup.querySelector('button'); let sendButton = flaggingFormPopup.querySelector('button');
let materialIdField = flaggingFormPopup.querySelector('#error-material-id'); let materialIdField = flaggingFormPopup.querySelector('#error-material-id');
let treeLeaf = null;
let overviewEStructCalcs = null; let overviewEStructCalcs = null;
function _setCurrentPage(pageId) {
function _setCurrentPage(pageId){
subcategoryField.innerHTML = ''; subcategoryField.innerHTML = '';
let materialId = DataStore.getMaterialData().material_id; let materialId = DataStore.getMaterialData().material_id;
materialIdField.textContent = materialId; materialIdField.textContent = materialId;
if (pageId === null){ if (pageId === null) {
categoryField.disabled = false; categoryField.disabled = false;
subcategoryField.style.display = 'none'; subcategoryField.style.display = 'none';
} else { } else {
categoryField.disabled = true; categoryField.disabled = true;
subcategoryField.style.display = 'block'; subcategoryField.style.display = 'block';
...@@ -128,12 +121,9 @@ function _setCurrentPage(pageId){ ...@@ -128,12 +121,9 @@ function _setCurrentPage(pageId){
break; break;
} }
} }
}
} // function _setCurrentPage function show(pageStatus) {
function show(pageStatus){
treeLeaf = pageStatus.markedLeaf;
overviewEStructCalcs = pageStatus.eStructCalcs; overviewEStructCalcs = pageStatus.eStructCalcs;
// Show/hide some dropdown list options // Show/hide some dropdown list options
...@@ -152,7 +142,6 @@ function show(pageStatus){ ...@@ -152,7 +142,6 @@ function show(pageStatus){
flaggingPopupBackground.style.visibility = 'visible'; flaggingPopupBackground.style.visibility = 'visible';
} }
function hide(){ function hide(){
flaggingPopupBackground.style.visibility = 'hidden'; flaggingPopupBackground.style.visibility = 'hidden';
flaggingFormPopup.style.visibility = 'hidden'; flaggingFormPopup.style.visibility = 'hidden';
...@@ -163,7 +152,6 @@ function hide(){ ...@@ -163,7 +152,6 @@ function hide(){
validationMsg.innerHTML = ''; validationMsg.innerHTML = '';
} }
function createOption(text, value){ function createOption(text, value){
let opt = document.createElement('option'); let opt = document.createElement('option');
opt.value = (value === undefined ? text : value); opt.value = (value === undefined ? text : value);
...@@ -171,72 +159,67 @@ function createOption(text, value){ ...@@ -171,72 +159,67 @@ function createOption(text, value){
return opt; return opt;
} }
closeButton.addEventListener('click', e => { closeButton.addEventListener('click', e => {
hide(); hide();
}); });
sendButton.addEventListener('click', e => { sendButton.addEventListener('click', e => {
// First double-check that the authentication is still valid.
let categoryChosen = categoryField.options[categoryField.selectedIndex]; let categoryChosen = categoryField.options[categoryField.selectedIndex];
var keycloak = window.keycloak;
if (!keycloak.authenticated) {
validationMsg.innerHTML = "Your authentication has expired. Please login again.";
return;
}
if (!categoryField.disabled && categoryChosen.value === ''){ // Overview case // Check fields
if (!categoryField.disabled && categoryChosen.value === '') { // Overview case
validationMsg.innerHTML = 'The category fields must be set'; validationMsg.innerHTML = 'The category fields must be set';
return;
} else if (categoryField.disabled && subcategoryField.value === '' // Detaisl pages case } else if (categoryField.disabled && subcategoryField.value === '' // Details pages case
&& categoryChosen.value !== util.MAT_VIEW.methodology){ && categoryChosen.value !== util.MAT_VIEW.methodology) {
validationMsg.innerHTML = 'The subcategory fields must be set'; validationMsg.innerHTML = 'The subcategory fields must be set';
return;
} else { }
validationMsg.innerHTML = 'Sending report...';
let textareaText = flaggingFormPopup.querySelector('textarea').value; // Send report
let materialId = DataStore.getMaterialData().material_id; validationMsg.innerHTML = 'Sending report...';
let userdata = util.getUserData(); let textareaText = flaggingFormPopup.querySelector('textarea').value;
let materialId = DataStore.getMaterialData().material_id;
keycloak.loadUserProfile()
let titleText = 'User issue | Material '+materialId; .then(function(profile) {
let descriptionText = '**Server:** '+util.getServerLocation()+ let report = {};
'\\n\\n**User:** '+userdata.username+', '+userdata.email; report.server = util.getServerLocation();
report.username = profile.username;
// Overview page report.email = profile.email;
if ( !categoryField.disabled){ report.first_name = profile.firstName;
descriptionText += '\\n\\n**Category:** Overview / '+categoryChosen.text; report.last_name = profile.lastName;
report.message = textareaText;
if (categoryChosen.value === util.MAT_VIEW.electronicstruct report.category = categoryChosen.text;
&& overviewEStructCalcs !== null)
descriptionText += '\\n\\n**Chosen calculations:** '+ // Overview page
(overviewEStructCalcs.bs === null ? '' : 'BS calculation '+overviewEStructCalcs.bs)+ if (!categoryField.disabled) {
(overviewEStructCalcs.dos === null ? '' : ' DOS calculation '+overviewEStructCalcs.dos); report.representatives = DataStore.getRepresentatives();
} else { // Details pages
} else { // Details pages report.subcategory = subcategoryField.options[subcategoryField.selectedIndex].text;
descriptionText += '\\n\\n**Category:** '+categoryChosen.text;
if (categoryChosen.value !== util.MAT_VIEW.methodology){
descriptionText += '\\n\\n**Subcategory:** '+
subcategoryField.options[subcategoryField.selectedIndex].text+
'\\n\\n**Calculation/group marked on the tree:** '+treeLeaf;
} }
}
descriptionText += '\\n\\n**User text:** '+textareaText; let token = keycloak.token;
let queryJson =`{ util.serverReqPOST(util.getReportURL(materialId), JSON.stringify(report), e => {
"title": "${titleText}", if (e.target.status === 204) {
"description": "${descriptionText}"}`; hide();
console.log('Flagging POST request Json: ',queryJson);//, util.getFlaggingURL()); } else {
validationMsg.innerHTML = "Could not connect to the service. Please check your connection and try again later.";
}
util.serverReqPOST(util.getFlaggingURL(), queryJson, e => { }, token);
console.log('response',e); })
if (e.target.status === 200) hide(); .catch(error => {
console.log(error);
validationMsg.innerHTML = "Could not connect to the service. Please check your connection and try again later.";
}); });
}
}); });
// EXPORTS // EXPORTS
module.exports = { show, hide }; module.exports = { show, hide };
...@@ -132,7 +132,7 @@ function getShortCode(id) { ...@@ -132,7 +132,7 @@ function getShortCode(id) {
function setAuthRequestHeader(userDataP, value){ function setAuthRequestHeader(userDataP, value){
if (value === undefined){// default value if (value === undefined) { // default value
authRequestHeaderValue = AUTH_REQUEST_HEADER_GUEST_USER; authRequestHeaderValue = AUTH_REQUEST_HEADER_GUEST_USER;
userData = null; userData = null;
//console.log('user: ANONYMOUS authRequestHeader: ',authRequestHeaderValue); //console.log('user: ANONYMOUS authRequestHeader: ',authRequestHeaderValue);
...@@ -155,13 +155,16 @@ function serverReq(url, callback){ ...@@ -155,13 +155,16 @@ function serverReq(url, callback){
} }
function serverReqPOST(url, data, callback){ function serverReqPOST(url, data, callback, token){
var oReq = new XMLHttpRequest(); var oReq = new XMLHttpRequest();
oReq.addEventListener('load', callback); oReq.addEventListener('load', callback);
oReq.open('POST', url); oReq.open('POST', url);
oReq.setRequestHeader('Content-Type', 'application/json'); oReq.setRequestHeader('Content-Type', 'application/json');
//console.log('authRequestHeaderValue: ',authRequestHeaderValue); if (token !== null) {
oReq.setRequestHeader('Authorization', authRequestHeaderValue); oReq.setRequestHeader('Authorization', 'Bearer ' + token);
} else {
oReq.setRequestHeader('Authorization', authRequestHeaderValue);
}
oReq.send(data); oReq.send(data);
return oReq; return oReq;
} }
...@@ -212,8 +215,8 @@ function getCalcEnergiesURL(matId,calcId){ ...@@ -212,8 +215,8 @@ function getCalcEnergiesURL(matId,calcId){
return API_BASE_URL+'materials/'+matId+'/calculations/'+calcId+'/energies';//'/materials/calculations';// return API_BASE_URL+'materials/'+matId+'/calculations/'+calcId+'/energies';//'/materials/calculations';//
} }
function getFlaggingURL(){ function getReportURL(matId) {
return API_BASE_URL+'flagme'; return API_BASE_URL+'materials/'+matId+'/reports';
} }
...@@ -456,7 +459,7 @@ module.exports = { ...@@ -456,7 +459,7 @@ module.exports = {
getMaterialStatsURL: getMaterialStatsURL, getMaterialStatsURL: getMaterialStatsURL,
getMaterialXsURL: getMaterialXsURL, getMaterialXsURL: getMaterialXsURL,
getCalcEnergiesURL: getCalcEnergiesURL, getCalcEnergiesURL: getCalcEnergiesURL,
getFlaggingURL, getReportURL,
setBrowserHashPath: setBrowserHashPath, setBrowserHashPath: setBrowserHashPath,
loadLib: loadLib, loadLib: loadLib,
getNumberArray: getNumberArray, getNumberArray: getNumberArray,
......
...@@ -66,6 +66,7 @@ var keycloak = new Keycloak({ ...@@ -66,6 +66,7 @@ var keycloak = new Keycloak({
realm: window.nomadEnv.keycloakRealm, realm: window.nomadEnv.keycloakRealm,
clientId: window.nomadEnv.keycloakClientId clientId: window.nomadEnv.keycloakClientId
}); });
window.keycloak = keycloak;
let loginButton = document.querySelector('#login-button'); let loginButton = document.querySelector('#login-button');
let logoutButton = document.querySelector('#logout-button'); let logoutButton = document.querySelector('#logout-button');
let userName = document.querySelector('#user-name'); let userName = document.querySelector('#user-name');
...@@ -106,7 +107,7 @@ logoutButton.onclick = () => { ...@@ -106,7 +107,7 @@ logoutButton.onclick = () => {
but it has to be set up when the app starts */ but it has to be set up when the app starts */
let flaggingTab = document.getElementById('calc-flagging-tab'); let flaggingTab = document.getElementById('calc-flagging-tab');
flaggingTab.style.top = (window.innerHeight/2)+'px'; flaggingTab.style.top = (window.innerHeight/2)+'px';
flaggingTab.addEventListener('click',e => { flaggingTab.addEventListener('click', e => {
FlaggingFormPopup.show(MaterialModule.getCurrentPageStatus()); FlaggingFormPopup.show(MaterialModule.getCurrentPageStatus());
}); });
......
const path = require('path');
module.exports = { module.exports = {
entry: "./src/main.js", entry: "./src/main.js",
output: { output: {
path: __dirname, path: __dirname,
filename: "bundle.js" filename: "bundle.js"
}, },
module: {} module: {}
}; };
Supports Markdown
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