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) {
ServerConnector.getToken = function() {
var self = this;
return new Promise(function(resolve) {
if (self._token === undefined) {
self.setToken(document.getElementById("authenticationForm:authenticationToken").value);
return new Promise(function(resolve, reject) {
var token = self.getSessionData(null).getToken();
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() {
return this.getServerBaseUrl()+"/api/";
};
......@@ -216,6 +228,12 @@ ServerConnector.getProjectUrl = function(projectId, token) {
});
};
ServerConnector.loginUrl = function() {
return this.getApiUrl({type:"user",
method: "login"
});
};
ServerConnector.getSuggestedQueryListUrl = function(params) {
return this.getApiUrl({type:"project",
method: "getSuggestedQueryList",
......@@ -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) {
var self = this;
return new Promise(function(resolve, reject) {
......@@ -781,6 +810,9 @@ ServerConnector.getSessionData = function(project) {
if (this._sessionData===undefined) {
this._sessionData = new SessionData(project);
}
if (project!==undefined && this._sessionData.getProject()===null) {
this._sessionData.setProject(project)
}
return this._sessionData;
};
......@@ -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) {
var self = this;
return new Promise(function(resolve, reject) {
......
......@@ -4,6 +4,8 @@ var SessionObjectType = require('./SessionObjectType');
var Cookies = require('js-cookie');
var logger = require('./logger');
function SessionData(project) {
if (project === undefined) {
throw new Error("Project must be passed as an argument");
......@@ -15,6 +17,10 @@ SessionData.prototype.setProject = function(project) {
this._project = project;
};
SessionData.prototype.getProject = function() {
return this._project;
};
SessionData.prototype.getProjectId = function() {
return this._project.getProjectId();
};
......@@ -139,6 +145,21 @@ SessionData.prototype.getZoomLevel = function(model) {
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) {
var key = this.getKey(SessionObjectType.CENTER, [ model.getId() ]);
Cookies.set(key, value.x + "," + value.y);
......
......@@ -11,6 +11,8 @@ var SessionObjectType = {
CENTER : "CENTER",
ZOOM_LEVEL : "ZOOM_LEVEL",
TOKEN: "TOKEN",
};
module.exports = SessionObjectType;
......@@ -13,10 +13,6 @@ function OverlayPanel(params) {
var self = this;
self.setPanelName(params.panelName);
self.setElement(params.element);
self.setMap(params.customMap);
if (self.getGeneralOverlaysElement() === undefined) {
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');
var OverlayPanel = require('./gui/OverlayPanel');
var SearchDbOverlay = require('./map/overlay/SearchDbOverlay');
var SearchPanel = require('./gui/SearchPanel');
var UserPanel = require('./gui/UserPanel');
var OriginalGuiConnector = require('./GuiConnector');
var OriginalServerConnector = require('./ServerConnector');
......@@ -175,6 +176,11 @@ function create(params) {
customMap : result
});
new UserPanel({
element : document.getElementById("userTab"),
customMap : result
});
return new Promise(function(resolve, reject) {
restoreSearchQuery(result).then(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");
var Project = require("../../main/js/map/data/Project");
var Reaction = require("../../main/js/map/data/Reaction");
var SearchDbOverlay = require("../../main/js/map/overlay/SearchDbOverlay");
var User = require("../../main/js/map/data/User");
function Helper() {
this.idCounter = 1000000;
......@@ -79,33 +80,80 @@ Helper.prototype.createPanelTab = function(id) {
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() {
var result = document.createElement("div");
result.id = "overlayTab";
var generalOverlaysDiv = document.createElement("div");
generalOverlaysDiv.setAttribute("name", "generalOverlays");
result.appendChild(generalOverlaysDiv);
var generalOverlaysTabDiv = document.createElement("table");
generalOverlaysTabDiv.setAttribute("name", "generalOverlaysTab");
generalOverlaysDiv.appendChild(generalOverlaysTabDiv);
var customOverlaysDiv = document.createElement("div");
customOverlaysDiv.setAttribute("name", "customOverlays");
result.appendChild(customOverlaysDiv);
var customOverlaysTabDiv = document.createElement("table");
customOverlaysTabDiv.setAttribute("name", "customOverlaysTab");
customOverlaysDiv.appendChild(customOverlaysTabDiv);
var customOverlaysTitleDiv = document.createElement("h3");
customOverlaysTitleDiv.setAttribute("name", "customOverlaysTitle");
customOverlaysDiv.appendChild(customOverlaysTitleDiv);
var addButton = document.createElement("button");
addButton.setAttribute("name", "addOverlay");
customOverlaysDiv.appendChild(addButton);
return result;
};
......@@ -213,6 +261,16 @@ Helper.prototype.createProject = function() {
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) {
var mapId;
if (map === undefined) {
......
......@@ -32,6 +32,9 @@ describe('minerva global', function() {
global.overlayTab = helper.createOverlayTab();
document.body.appendChild(global.overlayTab);
global.userTab = helper.createUserTab();
document.body.appendChild(global.userTab);
});
afterEach(function() {
......@@ -40,6 +43,7 @@ describe('minerva global', function() {
document.body.removeChild(global.chemicalTab);
document.body.removeChild(global.mirnaTab);
document.body.removeChild(global.overlayTab);
document.body.removeChild(global.userTab);
});
it('create', function() {
......
"use strict";
var Project = require("../../main/js/map/data/Project");
var Promise = require("bluebird");
var Cookies = require('js-cookie');
// GLOBAL configuration
global.navigator = {
userAgent : 'node.js'
};
userAgent : 'node.js'
};
var jsdom = require('jsdom');
global.document = jsdom.jsdom(undefined);
......@@ -21,56 +22,54 @@ require('jquery-ui-dist/jquery-ui.js');
require("bootstrap");
global.google = require('./google-map-mock');
global.GuiConnector = require('./GuiConnector-mock');
global.ServerConnector = require('./ServerConnector-mock');
// -----------------------------
var logger = require('./logger');
function removeCookies(){
var cookies = Cookies.get();
for(var cookie in cookies) {
function removeCookies() {
var cookies = Cookies.get();
for ( var cookie in cookies) {
Cookies.remove(cookie);
}
}
function mockBootstrap() {
$.fn.typeahead = function () {
$.fn.typeahead = function() {
logger.debug("Mock typeahead function call");
};
}
before(function() {
Promise.longStackTraces();
mockBootstrap();
});
beforeEach(function() {
logger.flushBuffer();
removeCookies();
ServerConnector.init();
ServerConnector.setToken("MOCK_TOKEN_ID");
ServerConnector.getSessionData(null).setToken("MOCK_TOKEN_ID");
GuiConnector.init();
global.testDiv = document.createElement("div");
global.testDiv.id = "test";
document.body.appendChild(testDiv);
global.dialogDiv = document.createElement("div");
global.dialogDiv.id = "feedbackContent";
document.body.appendChild(global.dialogDiv);
});
afterEach(function() {
......
{"expires":{"year":3017,"month":0,"dayOfMonth":30,"hourOfDay":14,"minute":52,"second":45},"id":"FILE_MOCK_TOKEN_ID"}
\ No newline at end of file