Commit e8044f03 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

login panel and user details panel

parent 5be718b2
...@@ -149,18 +149,30 @@ ServerConnector.sendDeleteRequest = function(url, params) { ...@@ -149,18 +149,30 @@ ServerConnector.sendDeleteRequest = function(url, params) {
ServerConnector.getToken = function() { ServerConnector.getToken = function() {
var self = this; var self = this;
return new Promise(function(resolve) { return new Promise(function(resolve, reject) {
if (self._token === undefined) { var token = self.getSessionData(null).getToken();
self.setToken(document.getElementById("authenticationForm:authenticationToken").value); if (token === undefined) {
self.login().then(function(token){
resolve(token);
}).catch(reject);
} else {
if (self.getSessionData().getProject()===null) {
self.isValidToken(token).then(function(isOk){
if (isOk) {
resolve(token);
} else {
self.login().then(function(token){
resolve(token);
}).catch(reject);
}
}).catch(reject);
} else {
resolve(token);
}
} }
resolve(self._token);
}); });
}; };
ServerConnector.setToken = function(token) {
this._token = token;
};
ServerConnector.getApiBaseUrl = function() { ServerConnector.getApiBaseUrl = function() {
return this.getServerBaseUrl()+"/api/"; return this.getServerBaseUrl()+"/api/";
}; };
...@@ -216,6 +228,12 @@ ServerConnector.getProjectUrl = function(projectId, token) { ...@@ -216,6 +228,12 @@ ServerConnector.getProjectUrl = function(projectId, token) {
}); });
}; };
ServerConnector.loginUrl = function() {
return this.getApiUrl({type:"user",
method: "login"
});
};
ServerConnector.getSuggestedQueryListUrl = function(params) { ServerConnector.getSuggestedQueryListUrl = function(params) {
return this.getApiUrl({type:"project", return this.getApiUrl({type:"project",
method: "getSuggestedQueryList", method: "getSuggestedQueryList",
...@@ -543,6 +561,17 @@ ServerConnector.getUserUrl = function(params) { ...@@ -543,6 +561,17 @@ ServerConnector.getUserUrl = function(params) {
}); });
}; };
ServerConnector.isValidTokenUrl = function(params) {
var token = params.token;
return this.getApiUrl({type:"user",
method:"tokenStatus",
params: {
token: token,
},
});
};
ServerConnector.getConfigurationParam = function(paramId) { ServerConnector.getConfigurationParam = function(paramId) {
var self = this; var self = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
...@@ -781,6 +810,9 @@ ServerConnector.getSessionData = function(project) { ...@@ -781,6 +810,9 @@ ServerConnector.getSessionData = function(project) {
if (this._sessionData===undefined) { if (this._sessionData===undefined) {
this._sessionData = new SessionData(project); this._sessionData = new SessionData(project);
} }
if (project!==undefined && this._sessionData.getProject()===null) {
this._sessionData.setProject(project)
}
return this._sessionData; return this._sessionData;
}; };
...@@ -806,6 +838,49 @@ ServerConnector.getClosestElementsByCoordinates = function(params) { ...@@ -806,6 +838,49 @@ ServerConnector.getClosestElementsByCoordinates = function(params) {
}); });
}; };
ServerConnector.login = function(login, password) {
var self = this;
return new Promise(function(resolve, reject) {
var params = {};
if (login!==undefined) {
params.login = login;
params.password = password;
}
return self.sendPostRequest(self.loginUrl(),params).then(function(content) {
var obj=JSON.parse(content);
var token = obj.id;
if (token===undefined) {
reject(obj.error);
} else {
self.getSessionData().setToken(token)
resolve(token);
}
}).catch(function(exception){
reject(exception);
});
});
};
ServerConnector.isValidToken = function(token) {
var self = this;
return new Promise(function(resolve, reject) {
return self.readFile(self.isValidTokenUrl({token:token})).then(function(content) {
var obj=JSON.parse(content);
resolve(obj.error===undefined);
}).catch(function(exception){
resolve(false);
});
});
};
ServerConnector.logout = function() {
var self = this;
return new Promise(function(resolve, reject) {
self.getSessionData().setToken(undefined)
resolve();
});
};
ServerConnector.getElementsByQuery = function(params) { ServerConnector.getElementsByQuery = function(params) {
var self = this; var self = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
......
...@@ -4,6 +4,8 @@ var SessionObjectType = require('./SessionObjectType'); ...@@ -4,6 +4,8 @@ var SessionObjectType = require('./SessionObjectType');
var Cookies = require('js-cookie'); var Cookies = require('js-cookie');
var logger = require('./logger');
function SessionData(project) { function SessionData(project) {
if (project === undefined) { if (project === undefined) {
throw new Error("Project must be passed as an argument"); throw new Error("Project must be passed as an argument");
...@@ -15,6 +17,10 @@ SessionData.prototype.setProject = function(project) { ...@@ -15,6 +17,10 @@ SessionData.prototype.setProject = function(project) {
this._project = project; this._project = project;
}; };
SessionData.prototype.getProject = function() {
return this._project;
};
SessionData.prototype.getProjectId = function() { SessionData.prototype.getProjectId = function() {
return this._project.getProjectId(); return this._project.getProjectId();
}; };
...@@ -139,6 +145,21 @@ SessionData.prototype.getZoomLevel = function(model) { ...@@ -139,6 +145,21 @@ SessionData.prototype.getZoomLevel = function(model) {
return value; return value;
}; };
SessionData.prototype.setToken = function(token) {
var key = SessionObjectType.TOKEN;
if (token === undefined) {
Cookies.remove(key);
} else {
Cookies.set(key, token);
}
};
SessionData.prototype.getToken = function(model) {
var key = SessionObjectType.TOKEN
var value = Cookies.get(key);
return value;
};
SessionData.prototype.setCenter = function(model, value) { SessionData.prototype.setCenter = function(model, value) {
var key = this.getKey(SessionObjectType.CENTER, [ model.getId() ]); var key = this.getKey(SessionObjectType.CENTER, [ model.getId() ]);
Cookies.set(key, value.x + "," + value.y); Cookies.set(key, value.x + "," + value.y);
......
...@@ -11,6 +11,8 @@ var SessionObjectType = { ...@@ -11,6 +11,8 @@ var SessionObjectType = {
CENTER : "CENTER", CENTER : "CENTER",
ZOOM_LEVEL : "ZOOM_LEVEL", ZOOM_LEVEL : "ZOOM_LEVEL",
TOKEN: "TOKEN",
}; };
module.exports = SessionObjectType; module.exports = SessionObjectType;
...@@ -13,10 +13,6 @@ function OverlayPanel(params) { ...@@ -13,10 +13,6 @@ function OverlayPanel(params) {
var self = this; var self = this;
self.setPanelName(params.panelName);
self.setElement(params.element);
self.setMap(params.customMap);
if (self.getGeneralOverlaysElement() === undefined) { if (self.getGeneralOverlaysElement() === undefined) {
throw new Error("Couldn't find element: generalOverlays"); throw new Error("Couldn't find element: generalOverlays");
} }
......
"use strict";
/* exported logger */
var Panel = require('./Panel');
var GuiConnector = require('../GuiConnector');
var logger = require('../logger');
function UserPanel(params) {
params.panelName = "user";
Panel.call(this, params);
var self = this;
ServerConnector.getLoggedUser().then(function(user) {
if (user.getLogin() === "anonymous") {
self.showLoginPage();
} else {
self.showUserProfilePage(user);
}
});
self.getLoginButton().onclick = function(){
var login = self.getLoginText().value;
var password = self.getPasswordText().value;
ServerConnector.login(login, password).then(function(token){
window.location.reload(false);
}).catch(function(problem){
GuiConnector.alert(problem);
});
};
self.getLogoutButton().onclick = function(){
ServerConnector.logout().then(function(token){
window.location.reload(false);
}).catch(function(problem){
GuiConnector.alert(problem);
});
};
}
UserPanel.prototype = Object.create(Panel.prototype);
UserPanel.prototype.constructor = UserPanel;
UserPanel.prototype.getUserLoginTab = function() {
return this.getElementByName(this.getElement(), "userLoginTab");
};
UserPanel.prototype.getPasswordText= function() {
return this.getElementByName(this.getElement(), "passwordText");
};
UserPanel.prototype.getLoginText = function() {
return this.getElementByName(this.getElement(), "loginText");
};
UserPanel.prototype.getUserDataTab = function() {
return this.getElementByName(this.getElement(), "userDataTab");
};
UserPanel.prototype.getLoginValue = function() {
return this.getElementByName(this.getElement(), "loginValue");
};
UserPanel.prototype.getLogoutButton = function() {
return this.getElementByName(this.getElement(), "logoutButton");
};
UserPanel.prototype.getLoginButton = function() {
return this.getElementByName(this.getElement(), "loginButton");
};
UserPanel.prototype.getNameValue = function() {
return this.getElementByName(this.getElement(), "nameValue");
};
UserPanel.prototype.getSurnameValue = function() {
return this.getElementByName(this.getElement(), "surnameValue");
};
UserPanel.prototype.getEmailValue = function() {
return this.getElementByName(this.getElement(), "emailValue");
};
UserPanel.prototype.showLoginPage = function() {
var self = this;
self.getUserLoginTab().style.display = "block";
self.getUserDataTab().style.display = "none";
}
UserPanel.prototype.showUserProfilePage = function(user) {
var self = this;
self.getUserLoginTab().style.display = "none";
self.getUserDataTab().style.display = "block";
self.getLoginValue().innerHTML = user.getLogin();
self.getNameValue().innerHTML = user.getName();
self.getSurnameValue().innerHTML = user.getSurname();
self.getEmailValue().innerHTML = user.getEmail();
}
module.exports = UserPanel;
...@@ -15,6 +15,7 @@ var MiRnaPanel = require('./gui/MiRnaPanel'); ...@@ -15,6 +15,7 @@ var MiRnaPanel = require('./gui/MiRnaPanel');
var OverlayPanel = require('./gui/OverlayPanel'); var OverlayPanel = require('./gui/OverlayPanel');
var SearchDbOverlay = require('./map/overlay/SearchDbOverlay'); var SearchDbOverlay = require('./map/overlay/SearchDbOverlay');
var SearchPanel = require('./gui/SearchPanel'); var SearchPanel = require('./gui/SearchPanel');
var UserPanel = require('./gui/UserPanel');
var OriginalGuiConnector = require('./GuiConnector'); var OriginalGuiConnector = require('./GuiConnector');
var OriginalServerConnector = require('./ServerConnector'); var OriginalServerConnector = require('./ServerConnector');
...@@ -175,6 +176,11 @@ function create(params) { ...@@ -175,6 +176,11 @@ function create(params) {
customMap : result customMap : result
}); });
new UserPanel({
element : document.getElementById("userTab"),
customMap : result
});
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
restoreSearchQuery(result).then(function(){ restoreSearchQuery(result).then(function(){
......
...@@ -110,4 +110,11 @@ describe('ServerConnector', function() { ...@@ -110,4 +110,11 @@ describe('ServerConnector', function() {
}); });
}); });
it('getToken without login', function() {
ServerConnector.getSessionData().setToken(undefined);
return ServerConnector.getToken().then(function(token){
assert.ok(token);
});
});
}); });
"use strict";
var Helper = require('../helper');
require("../mocha-config.js");
var UserPanel = require('../../../main/js/gui/UserPanel');
var chai = require('chai');
var assert = chai.assert;
var logger = require('../logger');
describe('UserPanel', function() {
var helper;
before(function() {
helper = new Helper();
});
it('contructor', function() {
var div = helper.createUserTab();
var map = helper.createCustomMap();
new UserPanel({
element : div,
customMap : map
});
assert.equal(logger.getWarnings().length, 0);
});
it('show profile', function() {
var div = helper.createUserTab();
var map = helper.createCustomMap();
var panel = new UserPanel({
element : div,
customMap : map
});
var user = helper.createUser();
panel.showUserProfilePage(user);
});
});
...@@ -20,6 +20,7 @@ var AbstractDbOverlay = require("../../main/js/map/overlay/AbstractDbOverlay"); ...@@ -20,6 +20,7 @@ var AbstractDbOverlay = require("../../main/js/map/overlay/AbstractDbOverlay");
var Project = require("../../main/js/map/data/Project"); var Project = require("../../main/js/map/data/Project");
var Reaction = require("../../main/js/map/data/Reaction"); var Reaction = require("../../main/js/map/data/Reaction");
var SearchDbOverlay = require("../../main/js/map/overlay/SearchDbOverlay"); var SearchDbOverlay = require("../../main/js/map/overlay/SearchDbOverlay");
var User = require("../../main/js/map/data/User");
function Helper() { function Helper() {
this.idCounter = 1000000; this.idCounter = 1000000;
...@@ -79,33 +80,80 @@ Helper.prototype.createPanelTab = function(id) { ...@@ -79,33 +80,80 @@ Helper.prototype.createPanelTab = function(id) {
return result; return result;
}; };
Helper.prototype.createUserTab = function() {
var result = document.createElement("div");
result.id = "userTab";
var userLoginTab = document.createElement("div");
userLoginTab.setAttribute("name", "userLoginTab");
result.appendChild(userLoginTab);
var loginText = document.createElement("input");
loginText.setAttribute("name", "loginText");
userLoginTab.appendChild(loginText);
var passwordText = document.createElement("input");
passwordText.setAttribute("name", "passwordText");
userLoginTab.appendChild(passwordText);
var loginButton = document.createElement("button");
loginButton.setAttribute("name", "loginButton");
userLoginTab.appendChild(loginButton);
var userDataTab = document.createElement("div");
userDataTab.setAttribute("name", "userDataTab");
result.appendChild(userDataTab);
var loginValue = document.createElement("span");
loginValue.setAttribute("name", "loginValue");
userDataTab.appendChild(loginValue);
var nameValue = document.createElement("span");
nameValue.setAttribute("name", "nameValue");
userDataTab.appendChild(nameValue);
var surnameValue = document.createElement("span");
surnameValue.setAttribute("name", "surnameValue");
userDataTab.appendChild(surnameValue);
var emailValue = document.createElement("span");
emailValue.setAttribute("name", "emailValue");
userDataTab.appendChild(emailValue);
var logoutButton = document.createElement("button");
logoutButton.setAttribute("name", "logoutButton");
userLoginTab.appendChild(logoutButton);
return result;
};
Helper.prototype.createOverlayTab = function() { Helper.prototype.createOverlayTab = function() {
var result = document.createElement("div"); var result = document.createElement("div");
result.id = "overlayTab"; result.id = "overlayTab";
var generalOverlaysDiv = document.createElement("div"); var generalOverlaysDiv = document.createElement("div");
generalOverlaysDiv.setAttribute("name", "generalOverlays"); generalOverlaysDiv.setAttribute("name", "generalOverlays");
result.appendChild(generalOverlaysDiv); result.appendChild(generalOverlaysDiv);
var generalOverlaysTabDiv = document.createElement("table"); var generalOverlaysTabDiv = document.createElement("table");
generalOverlaysTabDiv.setAttribute("name", "generalOverlaysTab"); generalOverlaysTabDiv.setAttribute("name", "generalOverlaysTab");
generalOverlaysDiv.appendChild(generalOverlaysTabDiv); generalOverlaysDiv.appendChild(generalOverlaysTabDiv);
var customOverlaysDiv = document.createElement("div"); var customOverlaysDiv = document.createElement("div");
customOverlaysDiv.setAttribute("name", "customOverlays"); customOverlaysDiv.setAttribute("name", "customOverlays");
result.appendChild(customOverlaysDiv); result.appendChild(customOverlaysDiv);
var customOverlaysTabDiv = document.createElement("table"); var customOverlaysTabDiv = document.createElement("table");
customOverlaysTabDiv.setAttribute("name", "customOverlaysTab"); customOverlaysTabDiv.setAttribute("name", "customOverlaysTab");
customOverlaysDiv.appendChild(customOverlaysTabDiv); customOverlaysDiv.appendChild(customOverlaysTabDiv);
var customOverlaysTitleDiv = document.createElement("h3"); var customOverlaysTitleDiv = document.createElement("h3");
customOverlaysTitleDiv.setAttribute("name", "customOverlaysTitle"); customOverlaysTitleDiv.setAttribute("name", "customOverlaysTitle");
customOverlaysDiv.appendChild(customOverlaysTitleDiv); customOverlaysDiv.appendChild(customOverlaysTitleDiv);
var addButton = document.createElement("button"); var addButton = document.createElement("button");
addButton.setAttribute("name", "addOverlay"); addButton.setAttribute("name", "addOverlay");
customOverlaysDiv.appendChild(addButton); customOverlaysDiv.appendChild(addButton);
return result; return result;
}; };
...@@ -213,6 +261,16 @@ Helper.prototype.createProject = function() { ...@@ -213,6 +261,16 @@ Helper.prototype.createProject = function() {
return result; return result;
}; };
Helper.prototype.createUser = function() {
var result = new User({
login : "testlogin",
id: this.idCounter++,
name: "some name",
surname: "surn",
});
return result;
};
Helper.prototype.createAlias = function(map) { Helper.prototype.createAlias = function(map) {
var mapId; var mapId;
if (map === undefined) { if (map === undefined) {
......
...@@ -32,6 +32,9 @@ describe('minerva global', function() { ...@@ -32,6 +32,9 @@ describe('minerva global', function() {
global.overlayTab = helper.createOverlayTab(); global.overlayTab = helper.createOverlayTab();
document.body.appendChild(global.overlayTab); document.body.appendChild(global.overlayTab);
global.userTab = helper.createUserTab();
document.body.appendChild(global.userTab);