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

editing of user fields implemented

parent 45beee3d
......@@ -204,7 +204,7 @@ ServerConnector.sendPatchRequest = function (url, json) {
ServerConnector.getToken = function () {
var self = this;
var login = self.getSessionData(null).getLogin()
var login = self.getSessionData(null).getLogin();
var token = self.getSessionData(null).getToken();
if (token === undefined || login === undefined) {
return self.login();
......@@ -858,7 +858,7 @@ ServerConnector.getLoggedUser = function () {
ServerConnector.getUser = function (login) {
var self = this;
var queryParams = {
login: login,
login: login
};
var filterParams = {};
......@@ -874,10 +874,31 @@ ServerConnector.getUser = function (login) {
});
};
ServerConnector.updateUser = function (user) {
var self = this;
var queryParams = {
login: user.getLogin()
};
var filterParams = {
user: {
name: user.getName(),
surname: user.getSurname(),
password: user.getPassword(),
email: user.getEmail()
}
};
return self.sendPatchRequest(self.getUserUrl(queryParams), filterParams).then(function () {
return self.getConfiguration();
}).then(function (configuration) {
return self.updateUserPrivileges({user: user, privileges: user.privilegesToExport(configuration)});
});
};
ServerConnector.updateUserPrivileges = function (params) {
var self = this;
var queryParams = {
login: params.user.getLogin(),
login: params.user.getLogin()
};
return self.sendPatchRequest(self.getUpdateUserPrivilegesUrl(queryParams), {
......@@ -891,8 +912,27 @@ ServerConnector.updateUserPrivileges = function (params) {
self._usersByLogin[user.getLogin()] = user;
}
return self._usersByLogin[user.getLogin()];
}).then(null, function (error) {
return self.processNetworkError(error);
});
};
ServerConnector.processNetworkError = function (error) {
if ((error instanceof NetworkError)) {
switch (error.statusCode) {
case HttpStatus.NOT_FOUND:
return null;
case HttpStatus.FORBIDDEN:
return Promise.reject(new SecurityError("Access denied."));
default:
return Promise.reject(error);
}
} else {
return Promise.reject(error);
}
};
ServerConnector.updateUserPreferences = function (params) {
var self = this;
var queryParams = {
......
......@@ -5,8 +5,10 @@ var Promise = require("bluebird");
var AbstractGuiElement = require('../AbstractGuiElement');
var GuiConnector = require('../../GuiConnector');
var ValidationError = require('../../ValidationError');
var Functions = require('../../Functions');
// noinspection JSUnusedLocalSymbols
var logger = require('../../logger');
var guiUtils = new (require('../leftPanel/GuiUtils'))();
......@@ -113,10 +115,98 @@ EditUserDialog.prototype.createGeneralTabContent = function () {
style: "display:table-cell",
content: "Login"
}));
loginRow.appendChild(new Functions.createElement({
if (user.getLogin() === undefined) {
loginRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "<input name='userLogin' value=''/>"
}));
} else {
loginRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "<input name='userLogin' value='" + user.getLogin() + "' readonly/>"
}));
}
var passwordRow = new Functions.createElement({
type: "div",
style: "display:table-row"
});
table.appendChild(passwordRow);
passwordRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "Password"
}));
passwordRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "<input type=\"password\" name='userPassword' value=''/>"
}));
var passwordRow2 = new Functions.createElement({
type: "div",
style: "display:table-row"
});
table.appendChild(passwordRow2);
passwordRow2.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "Confirm password"
}));
passwordRow2.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: user.getLogin()
content: "<input type=\"password\" name='userPassword2' value=''/>"
}));
var nameRow = new Functions.createElement({
type: "div",
style: "display:table-row"
});
table.appendChild(nameRow);
nameRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "Name"
}));
nameRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "<input name='userName' value='" + user.getName() + "'/>"
}));
var surnameRow = new Functions.createElement({
type: "div",
style: "display:table-row"
});
table.appendChild(surnameRow);
surnameRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "Surname"
}));
surnameRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "<input name='userSurname' value='" + user.getSurname() + "'/>"
}));
var emailRow = new Functions.createElement({
type: "div",
style: "display:table-row"
});
table.appendChild(emailRow);
emailRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "Email"
}));
emailRow.appendChild(new Functions.createElement({
type: "div",
style: "display:table-cell",
content: "<input name='userEmail' value='" + user.getEmail() + "'/>"
}));
......@@ -274,10 +364,74 @@ EditUserDialog.prototype.open = function () {
EditUserDialog.prototype.onSaveClicked = function () {
var self = this;
var user = self.getUser();
return self.checkValidity().then(function () {
user.setLogin(self.getLogin());
user.setPassword(self.getPassword());
user.setEmail(self.getEmail());
user.setName(self.getName());
user.setSurname(self.getSurname());
return ServerConnector.updateUser(user);
});
};
return ServerConnector.updateUser(user);
EditUserDialog.prototype.checkValidity = function () {
var self = this;
var isValid = true;
var error = "<b>Some data is missing.</b><ul>";
if (self.getPassword() !== self.getPassword2()) {
error += "<li>Password doesn't match</li>";
isValid = false;
}
if (self.getLogin() === "" || self.getLogin() === undefined || self.getLogin() === null) {
error += "<li>Login must not be empty</li>";
isValid = false;
}
var promise = Promise.resolve();
if (self.getUser().getLogin() !== self.getLogin()) {
promise = ServerConnector.getUser(self.getLogin()).then(function (remoteUser) {
if (remoteUser !== null) {
error += "<li>Login already used</li>";
isValid = false;
}
});
}
return promise.then(function () {
if (isValid) {
return Promise.resolve(true);
} else {
return Promise.reject(new ValidationError(error));
}
});
};
EditUserDialog.prototype.getPassword = function () {
var self = this;
return $("[name='userPassword']", self.getElement()).val();
};
EditUserDialog.prototype.getPassword2 = function () {
var self = this;
return $("[name='userPassword2']", self.getElement()).val();
};
EditUserDialog.prototype.getLogin = function () {
var self = this;
return $("[name='userLogin']", self.getElement()).val();
};
EditUserDialog.prototype.getEmail = function () {
var self = this;
return $("[name='userEmail']", self.getElement()).val();
};
EditUserDialog.prototype.getName = function () {
var self = this;
return $("[name='userName']", self.getElement()).val();
};
EditUserDialog.prototype.getSurname = function () {
var self = this;
return $("[name='userSurname']", self.getElement()).val();
};
EditUserDialog.prototype.close = function () {
var self = this;
$(self.getElement()).dialog("close");
......
......@@ -66,7 +66,7 @@ MapsAdminPanel.prototype._createMenuRow = function () {
content: '<span class="ui-icon ui-icon-refresh"></span>&nbsp;REFRESH',
onclick: function () {
return self.onRefreshClicked().then(null, GuiConnector.alert);
},
}
});
menuRow.appendChild(addProjectButton);
menuRow.appendChild(refreshButton);
......@@ -77,36 +77,37 @@ MapsAdminPanel.prototype._createProjectTableRow = function () {
var self = this;
var projectsRow = Functions.createElement({
type: "div",
style: "display:table-row; width:100%",
style: "display:table-row; width:100%"
});
var projectsTable = Functions.createElement({
type: "table",
name: "projectsTable",
className: "display",
style: "width:100%",
style: "width:100%"
});
projectsRow.appendChild(projectsTable);
// noinspection JSUnusedGlobalSymbols
$(projectsTable).DataTable({
fnRowCallback: function (nRow, aData) {
nRow.setAttribute('id', aData[0]);
},
columns: [{
title: 'ProjectId',
title: 'ProjectId'
}, {
title: 'Name',
title: 'Name'
}, {
title: 'Disease',
title: 'Disease'
}, {
title: 'Organism',
title: 'Organism'
}, {
title: 'Status',
title: 'Status'
}, {
title: 'Edit',
title: 'Edit'
}, {
title: 'Remove',
},],
title: 'Remove'
}]
});
$(projectsTable).on("click", "[name='removeProject']", function () {
var button = this;
......
"use strict";
var AbstractAdminPanel = require('./AbstractAdminPanel');
var EditUserDialog = require('./EditUserDialog');
var Functions = require('../../Functions');
var GuiConnector = require('../../GuiConnector');
// noinspection JSUnusedLocalSymbols
var logger = require('../../logger');
function UsersAdminPanel(params) {
var self = this;
......@@ -79,7 +82,11 @@ UsersAdminPanel.prototype._createUsersTableRow = function () {
});
projectsRow.appendChild(usersTable);
// noinspection JSUnusedGlobalSymbols
$(usersTable).DataTable({
fnRowCallback: function (nRow, aData) {
nRow.setAttribute('id', aData[0]);
},
columns: [{
title: 'Login'
}, {
......@@ -107,6 +114,44 @@ UsersAdminPanel.prototype._createUsersTableRow = function () {
return projectsRow;
};
UsersAdminPanel.prototype.showEditDialog = function (login) {
var self = this;
GuiConnector.showProcessing();
return ServerConnector.getUser(login).then(function (user) {
return self.getDialog(user);
}).then(function (dialog) {
dialog.open();
GuiConnector.hideProcessing();
}).then(null, function (error) {
GuiConnector.hideProcessing();
return Promise.reject(error);
});
};
UsersAdminPanel.prototype.getDialog = function (user) {
var self = this;
if (self._dialogs === undefined) {
self._dialogs = [];
}
var dialog = self._dialogs[user.getLogin()];
if (dialog === undefined) {
dialog = new EditUserDialog({
element: Functions.createElement({
type: "div"
}),
user: user,
project: self.getProject(),
customMap: null
});
self._dialogs[user.getLogin()] = dialog;
return dialog.init().then(function () {
return dialog;
});
} else {
return Promise.resolve(dialog);
}
};
UsersAdminPanel.prototype.init = function () {
var self = this;
return ServerConnector.getUsers().then(function (users) {
......@@ -121,11 +166,36 @@ UsersAdminPanel.prototype.setUsers = function (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
var rowData = self.userToTableRow(user);
self.addUpdateListener(user, rowData);
data.push(rowData);
}
dataTable.clear().rows.add(data).draw();
};
UsersAdminPanel.prototype.addUpdateListener = function (user, dataTableRow) {
var self = this;
var listenerName = "USER_LIST_LISTENER";
var listeners = user.getListeners("onreload");
for (var i = 0; i < listeners.length; i++) {
if (listeners[i].listenerName === listenerName) {
user.removeListener("onreload", listeners[i]);
}
}
var listener = function () {
var login = user.getLogin().replace(".", "\\.");
self.userToTableRow(user, dataTableRow);
var row = $($("[name='usersTable']", self.getElement())[0]).DataTable().row("#" + login);
if (row.length > 0) {
row.data(dataTableRow).draw();
}
};
listener.listenerName = listenerName;
user.addListener("onreload", listener);
};
UsersAdminPanel.prototype.userToTableRow = function (user, row) {
if (row === undefined) {
row = [];
......@@ -155,6 +225,13 @@ UsersAdminPanel.prototype.destroy = function () {
if ($.fn.DataTable.isDataTable(table)) {
$(table).DataTable().destroy();
}
for (var key in self._dialogs) {
if (self._dialogs.hasOwnProperty(key)) {
self._dialogs[key].destroy();
}
}
};
module.exports = UsersAdminPanel;
......@@ -5,8 +5,14 @@
var logger = require('../../logger');
var UserPreferences = require('./UserPreferences');
var ObjectWithListeners = require('../../ObjectWithListeners');
function User(javaObject) {
// call super constructor
ObjectWithListeners.call(this);
this.registerListenerType("onreload");
this.setLogin(javaObject.login);
this.setName(javaObject.name);
this.setSurname(javaObject.surname);
......@@ -19,6 +25,11 @@ function User(javaObject) {
this.setSimpleColor(javaObject.simpleColor);
}
// this class inherits from ObjectWithListeners class where generic methods for
// listeners are set
User.prototype = Object.create(ObjectWithListeners.prototype);
User.prototype.constructor = User;
User.prototype.setLogin = function (login) {
this._login = login;
};
......@@ -83,6 +94,14 @@ User.prototype.getMaxColor = function () {
return this._maxColor;
};
User.prototype.setPassword = function (password) {
this._password = password;
};
User.prototype.getPassword = function () {
return this._password;
};
User.prototype.setPrivileges = function (privileges) {
this._privileges = privileges;
};
......@@ -114,17 +133,44 @@ User.prototype.hasPrivilege = function (type, objectId) {
return false;
};
User.prototype.privilegesToExport = function (configuration) {
var self = this;
var result = {};
var validTypes = {};
var i;
for (i = 0; i < configuration.getPrivilegeTypes().length; i++) {
validTypes[configuration.getPrivilegeTypes()[i].getName()] = true;
}
for (i = 0; i < self.getPrivileges().length; i++) {
var privilege = self.getPrivileges()[i];
if (validTypes[privilege.type]) {
if (result[privilege.type] === undefined) {
result[privilege.type] = {};
}
if (privilege.objectId !== undefined) {
result[privilege.type][privilege.objectId] = privilege.value;
} else {
result[privilege.type] = privilege.value;
}
}
}
return result;
};
User.prototype.update = function (user) {
this.setLogin(user.getLogin());
this.setName(user.getName());
this.setSurname(user.getSurname());
this.setEmail(user.getEmail());
this.setRemoved(user.getRemoved());
this.setPrivileges(user.getPrivileges());
this.setPreferences(user.getPreferences());
this.setMinColor(user.getMinColor());
this.setMaxColor(user.getMaxColor());
this.setSimpleColor(user.getSimpleColor());
var self = this;
self.setLogin(user.getLogin());
self.setName(user.getName());
self.setSurname(user.getSurname());
self.setEmail(user.getEmail());
self.setRemoved(user.getRemoved());
self.setPrivileges(user.getPrivileges());
self.setPreferences(user.getPreferences());
self.setMinColor(user.getMinColor());
self.setMaxColor(user.getMaxColor());
self.setSimpleColor(user.getSimpleColor());
return self.callListeners("onreload");
};
module.exports = User;
......@@ -6,8 +6,13 @@ require("../../mocha-config");
var EditUserDialog = require('../../../../main/js/gui/admin/EditUserDialog');
var User = require('../../../../main/js/map/data/User');
var ValidationError = require('../../../../main/js/ValidationError');
// noinspection JSUnusedLocalSymbols
var logger = require('../../logger');
// noinspection JSUnusedLocalSymbols
var assert = require('assert');
describe('EditUserDialog', function () {
......@@ -32,4 +37,80 @@ describe('EditUserDialog', function () {
});
});
describe('checkValidity', function () {
it('empty user', function () {
var dialog;
var project;
var user = new User({});
return ServerConnector.getProject().then(function (result) {
project = result;
dialog = new EditUserDialog({
element: testDiv,
project: project,
user: user,
customMap: null
});
return dialog.init();
}).then(function () {
return dialog.checkValidity().then(function () {
assert.ok(null);
}, function (error) {
assert.ok(error instanceof ValidationError);
});
}).then(function () {
dialog.destroy();
});
});
it('existing user', function () {
var dialog;
var project;
var user;
return ServerConnector.getUser("anonymous").then(function (result) {