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,6 +80,53 @@ 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";
......@@ -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'
};
};
var jsdom = require('jsdom');
global.document = jsdom.jsdom(undefined);
......@@ -21,26 +22,24 @@ 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(){
function removeCookies() {
var cookies = Cookies.get();
for(var cookie in cookies) {
for ( var cookie in cookies) {
Cookies.remove(cookie);
}
}
function mockBootstrap() {
$.fn.typeahead = function () {
$.fn.typeahead = function() {
logger.debug("Mock typeahead function call");
};
}
......@@ -57,9 +56,9 @@ beforeEach(function() {
removeCookies();
ServerConnector.init();
ServerConnector.setToken("MOCK_TOKEN_ID");
ServerConnector.getSessionData(null).setToken("MOCK_TOKEN_ID");
GuiConnector.init();
......
{"expires":{"year":3017,"month":0,"dayOfMonth":30,"hourOfDay":14,"minute":52,"second":45},"id":"FILE_MOCK_TOKEN_ID"}
\ No newline at end of file
{"id":"MOCK_TOKEN_ID","expires":{"year":3017,"month":0,"dayOfMonth":30,"hourOfDay":18,"minute":34,"second":13}}
\ No newline at end of file
......@@ -29,10 +29,17 @@ public class UserController extends BaseController {
private UserRestImpl userRest;
@RequestMapping(value = "/login", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public AuthenticationToken login(@RequestParam(value = "login", defaultValue = Configuration.ANONYMOUS_LOGIN) String login,
public Map<String, Object> login(@RequestParam(value = "login", defaultValue = Configuration.ANONYMOUS_LOGIN) String login,
@RequestParam(value = "password", required = false) String password) {
AuthenticationToken token = userService.login(login, password);
return token;
Map<String, Object> result = new HashMap<>();
if (token == null) {
result.put("error", "Invalid credentials");
} else {
result.put("id", token.getId());
result.put("expires", token.getExpires());
}
return result;
}
@RequestMapping(value = "/tokenStatus", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
......@@ -42,8 +49,8 @@ public class UserController extends BaseController {
@RequestMapping(value = "/getUser", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> getUser(@RequestParam(value = "token", required = false) String token,
@RequestParam(value = "userId", defaultValue = "") String userId,
@RequestParam(value = "columns", defaultValue = "") String columns) throws SecurityException {
@RequestParam(value = "userId", defaultValue = "") String userId, @RequestParam(value = "columns", defaultValue = "") String columns)
throws SecurityException {
return userRest.getUser(token, userId, columns);
}
......
......@@ -3,40 +3,64 @@
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:cc="http://java.sun.com/jsf/composite/pfcomp"
xmlns:p="http://primefaces.org/ui">
<h:outputStylesheet library="css" name="login.css" />
<h:outputStylesheet library="css" name="global.css" />
<h:form name="loginForm" prependId="false" class="loginPanel">
<div style="top:0px; width:100%; display: block;">
<p:messages style="top:0px; width:100%;" id="loginMessages" showDetail="true" autoUpdate="true" closable="true" />
<div id="overlayTab">
<div name="userLogin" class="searchPanel">
<h5>AUTHORIZATION FORM:</h5>
<div style="width:100%;display: table;border-spacing: 10px;">
<div style="display: table-row;">
<div style="display: table-cell;">LOGIN:</div>
<div style="display: table-cell;"><input type="text" name="loginText" style="width:100%"/></div>
</div>