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

Merge branch '386-ldap-authentication' into 'master'

Resolve "LDAP authentication"

Closes #386

See merge request minerva/core!346
parents 7c40dabc a3f25ee1
......@@ -124,6 +124,12 @@ ServerConnector.getNeutralOverlayColorInt = function () {
});
};
/**
*
* @param url
* @param description
* @returns {Promise}
*/
ServerConnector.sendGetRequest = function (url, description) {
return this.sendRequest({
url: url,
......@@ -243,9 +249,9 @@ ServerConnector.sendDeleteRequest = function (url, json) {
/**
*
* @param url
* @param json
* @returns {PromiseLike}
* @param {string} url
* @param {Object} json
* @returns {Promise}
*/
ServerConnector.sendPatchRequest = function (url, json) {
return this.sendRequest({
......@@ -255,6 +261,10 @@ ServerConnector.sendPatchRequest = function (url, json) {
});
};
/**
*
* @returns {string}
*/
ServerConnector.getApiBaseUrl = function () {
return this.getServerBaseUrl() + "/api/";
};
......@@ -1137,6 +1147,11 @@ ServerConnector.getLoggedUser = function () {
}
};
/**
*
* @param {string} login
* @returns {Promise}
*/
ServerConnector.getUser = function (login) {
var self = this;
var queryParams = {
......@@ -1163,7 +1178,7 @@ ServerConnector.getUser = function (login) {
/**
*
* @param {User} user
* @returns {PromiseLike<any>}
* @returns {Promise}
*/
ServerConnector.updateUser = function (user) {
var self = this;
......@@ -1176,7 +1191,8 @@ ServerConnector.updateUser = function (user) {
surname: user.getSurname(),
password: user.getPassword(),
email: user.getEmail(),
termsOfUseConsent: user.isTermsOfUseConsent()
termsOfUseConsent: user.isTermsOfUseConsent(),
connectedToLdap: user.isConnectedToLdap()
}
};
return self.sendPatchRequest(self.getUserUrl(queryParams), filterParams).then(function () {
......
......@@ -24,6 +24,7 @@ var Promise = require("bluebird");
* @param params.parent
*
* @constructor
* @extends Panel
*/
function AbstractAdminPanel(params) {
params["scrollable"] = true;
......
......@@ -6,7 +6,6 @@ var PrivilegeType = require('../../map/data/PrivilegeType');
var AbstractAdminPanel = require('./AbstractAdminPanel');
var EditUserDialog = require('./EditUserDialog');
var SecurityError = require('../../SecurityError');
var User = require("../../map/data/User");
......@@ -15,6 +14,13 @@ var GuiConnector = require('../../GuiConnector');
// noinspection JSUnusedLocalSymbols
var logger = require('../../logger');
/**
*
* @param {Object} params
*
* @constructor
* @extends AbstractAdminPanel
*/
function UsersAdminPanel(params) {
var self = this;
AbstractAdminPanel.call(self, params);
......@@ -25,6 +31,10 @@ function UsersAdminPanel(params) {
UsersAdminPanel.prototype = Object.create(AbstractAdminPanel.prototype);
UsersAdminPanel.prototype.constructor = UsersAdminPanel;
/**
*
* @private
*/
UsersAdminPanel.prototype._createGui = function () {
var self = this;
var usersDiv = Functions.createElement({
......@@ -44,6 +54,11 @@ UsersAdminPanel.prototype._createGui = function () {
};
/**
*
* @returns {HTMLElement}
* @private
*/
UsersAdminPanel.prototype._createMenuRow = function () {
var self = this;
var menuRow = Functions.createElement({
......@@ -75,6 +90,11 @@ UsersAdminPanel.prototype._createMenuRow = function () {
return menuRow;
};
/**
*
* @returns {HTMLElement}
* @private
*/
UsersAdminPanel.prototype._createUsersTableRow = function () {
var self = this;
var projectsRow = Functions.createElement({
......@@ -90,7 +110,7 @@ UsersAdminPanel.prototype._createUsersTableRow = function () {
});
projectsRow.appendChild(usersTable);
// noinspection JSUnusedGlobalSymbols
// noinspection JSCheckFunctionSignatures
$(usersTable).DataTable({
fnRowCallback: function (nRow, aData) {
nRow.setAttribute('id', aData[0]);
......@@ -103,6 +123,8 @@ UsersAdminPanel.prototype._createUsersTableRow = function () {
title: 'Surname'
}, {
title: 'Email'
}, {
title: 'Authentication'
}, {
title: 'Edit'
}, {
......@@ -136,6 +158,27 @@ UsersAdminPanel.prototype._createUsersTableRow = function () {
}).catch(GuiConnector.alert);
});
$(usersTable).on("click", "[name='ldap-auth']", function () {
var field = this;
var login = $(this).attr('data');
GuiConnector.showProcessing();
return self.getServerConnector().getUser(login).then(function (user) {
var newIsConnected = $('input:checked', field).val() === "LDAP";
var isConnected = user.isConnectedToLdap();
if (isConnected !== newIsConnected) {
console.log(newIsConnected);
user.setConnectedToLdap(newIsConnected);
return self.getServerConnector().updateUser(user);
} else {
console.log('dont change');
}
}).catch(function (error) {
GuiConnector.alert(error);
}).finally(function(){
GuiConnector.hideProcessing();
});
});
$(usersTable).on("click", "[name='showEditDialog']", function () {
var button = this;
return self.showEditDialog($(button).attr("data")).then(null, GuiConnector.alert);
......@@ -144,10 +187,15 @@ UsersAdminPanel.prototype._createUsersTableRow = function () {
return projectsRow;
};
/**
*
* @param {String} login
* @returns {Promise}
*/
UsersAdminPanel.prototype.showEditDialog = function (login) {
var self = this;
GuiConnector.showProcessing();
return ServerConnector.getUser(login).then(function (user) {
return self.getServerConnector().getUser(login).then(function (user) {
return self.getDialog(user);
}).then(function (dialog) {
dialog.open();
......@@ -158,6 +206,11 @@ UsersAdminPanel.prototype.showEditDialog = function (login) {
});
};
/**
*
* @param {User} user
* @returns {Promise}
*/
UsersAdminPanel.prototype.getDialog = function (user) {
var self = this;
if (self._dialogs === undefined) {
......@@ -186,14 +239,18 @@ UsersAdminPanel.prototype.getDialog = function (user) {
}
};
/**
*
* @returns {Promise}
*/
UsersAdminPanel.prototype.init = function () {
var self = this;
return AbstractAdminPanel.prototype.init.call(this).then(function () {
return ServerConnector.getLoggedUser();
return self.getServerConnector().getLoggedUser();
}).then(function (user) {
var privilege = self.getConfiguration().getPrivilegeType(PrivilegeType.USER_MANAGEMENT);
if (user.hasPrivilege(privilege)) {
return ServerConnector.getUsers().then(function (users) {
return self.getServerConnector().getUsers().then(function (users) {
return self.setUsers(users);
});
} else {
......@@ -202,6 +259,10 @@ UsersAdminPanel.prototype.init = function () {
});
};
/**
*
* @param {User[]} users
*/
UsersAdminPanel.prototype.setUsers = function (users) {
var self = this;
var dataTable = $($("[name='usersTable']", self.getElement())[0]).DataTable();
......@@ -218,6 +279,11 @@ UsersAdminPanel.prototype.setUsers = function (users) {
};
/**
*
* @param {User} user
* @param {Array} dataTableRow
*/
UsersAdminPanel.prototype.addUpdateListener = function (user, dataTableRow) {
var self = this;
......@@ -241,7 +307,12 @@ UsersAdminPanel.prototype.addUpdateListener = function (user, dataTableRow) {
user.addListener("onreload", listener);
};
/**
*
* @param {User} user
* @param {Array} [row]
* @returns {Array}
*/
UsersAdminPanel.prototype.userToTableRow = function (user, row) {
if (row === undefined) {
row = [];
......@@ -251,20 +322,39 @@ UsersAdminPanel.prototype.userToTableRow = function (user, row) {
row[1] = user.getName();
row[2] = user.getSurname();
row[3] = user.getEmail();
row[4] = "<button name='showEditDialog' data='" + user.getLogin() + "'><i class='fa fa-edit' style='font-size:17px'></i></button>";
row[5] = "<button name='removeUser' data='" + user.getLogin() + "'><i class='fa fa-trash-o' style='font-size:17px'></i></button>";
var ldapFieldId = 'ldap-auth-' + user.getLogin();
if (user.isConnectedToLdap()) {
row[4] = "<fieldset name='ldap-auth' id='" + ldapFieldId + "' data='" + user.getLogin() + "'> <input type='radio' name='" + ldapFieldId + "' value='LOCAL'> LOCAL <input type='radio' name='" + ldapFieldId + "' checked value='LDAP'> LDAP </fieldset>";
} else {
if (user.isLdapAccountAvailable()) {
row[4] = "<fieldset name='ldap-auth' id='" + ldapFieldId + "' data='" + user.getLogin() + "'> <input type='radio' name='" + ldapFieldId + "' value='LOCAL' checked> LOCAL <input type='radio' name='" + ldapFieldId + "' value='LDAP'> LDAP </fieldset>";
// row[4] = "LOCAL <button name='connectLdap' data='" + user.getLogin() + "'>CONNECT LDAP</button>"
} else {
row[4] = "<fieldset name='ldap-auth' id='" + ldapFieldId + "' data='" + user.getLogin() + "'> <input type='radio' name='" + ldapFieldId + "' value='LOCAL' checked> LOCAL</fieldset>";
}
}
row[5] = "<button name='showEditDialog' data='" + user.getLogin() + "'><i class='fa fa-edit' style='font-size:17px'></i></button>";
row[6] = "<button name='removeUser' data='" + user.getLogin() + "'><i class='fa fa-trash-o' style='font-size:17px'></i></button>";
return row;
};
/**
*
* @returns {Promise}
*/
UsersAdminPanel.prototype.onRefreshClicked = function () {
var self = this;
return ServerConnector.getUsers(true).then(function (users) {
return self.getServerConnector().getUsers(true).then(function (users) {
return self.setUsers(users);
});
};
/**
*
*/
UsersAdminPanel.prototype.destroy = function () {
var self = this;
var table = $("[name='usersTable']", self.getElement())[0];
......@@ -277,9 +367,12 @@ UsersAdminPanel.prototype.destroy = function () {
self._dialogs[key].destroy();
}
}
};
/**
*
* @returns {Promise}
*/
UsersAdminPanel.prototype.onAddClicked = function () {
var self = this;
var user = new User({});
......@@ -293,10 +386,15 @@ UsersAdminPanel.prototype.onAddClicked = function () {
});
};
/**
*
* @param {string} login
* @returns {Promise}
*/
UsersAdminPanel.prototype.removeUser = function (login) {
var self = this;
GuiConnector.showProcessing();
return ServerConnector.removeUser(login).then(function () {
return self.getServerConnector().removeUser(login).then(function () {
return self.onRefreshClicked();
}).then(function () {
GuiConnector.hideProcessing();
......
......@@ -2,11 +2,17 @@
/* exported logger */
// noinspection JSUnusedLocalSymbols
var logger = require('../../logger');
var UserPreferences = require('./UserPreferences');
var ObjectWithListeners = require('../../ObjectWithListeners');
/**
*
* @param javaObject
* @constructor
*/
function User(javaObject) {
// call super constructor
ObjectWithListeners.call(this);
......@@ -25,6 +31,8 @@ function User(javaObject) {
this.setNeutralColor(javaObject.neutralColor);
this.setSimpleColor(javaObject.simpleColor);
this.setTermsOfUseConsent(javaObject.termsOfUseConsent);
this.setLdapAccountAvailable(javaObject.ldapAccountAvailable);
this.setConnectedToLdap(javaObject.connectedToLdap);
}
// this class inherits from ObjectWithListeners class where generic methods for
......@@ -32,86 +40,170 @@ function User(javaObject) {
User.prototype = Object.create(ObjectWithListeners.prototype);
User.prototype.constructor = User;
/**
*
* @param {string} login
*/
User.prototype.setLogin = function (login) {
this._login = login;
};
/**
*
* @returns {string}
*/
User.prototype.getLogin = function () {
return this._login;
};
/**
*
* @param {string} name
*/
User.prototype.setName = function (name) {
this._name = name;
};
/**
*
* @returns {string}
*/
User.prototype.getName = function () {
return this._name;
};
/**
*
* @param {string} surname
*/
User.prototype.setSurname = function (surname) {
this._surname = surname;
};
/**
*
* @returns {string}
*/
User.prototype.getSurname = function () {
return this._surname;
};
/**
*
* @param {string} email
*/
User.prototype.setEmail = function (email) {
this._email = email;
};
/**
*
* @returns {string}
*/
User.prototype.getEmail = function () {
return this._email;
};
/**
*
* @param {boolean} removed
*/
User.prototype.setRemoved = function (removed) {
this._removed = removed;
};
/**
*
* @returns {boolean}
*/
User.prototype.getRemoved = function () {
return this._removed;
};
/**
*
* @param {string} minColor
*/
User.prototype.setMinColor = function (minColor) {
this._minColor = minColor;
};
/**
*
* @returns {string}
*/
User.prototype.getMinColor = function () {
return this._minColor;
};
/**
*
* @param {string} simpleColor
*/
User.prototype.setSimpleColor = function (simpleColor) {
this._simpleColor = simpleColor;
};
/**
*
* @returns {string}
*/
User.prototype.getSimpleColor = function () {
return this._simpleColor;
};
/**
*
* @param {string} neutralColor
*/
User.prototype.setNeutralColor = function (neutralColor) {
this._neutralColor = neutralColor;
};
/**
*
* @returns {string}
*/
User.prototype.getNeutralColor = function () {
return this._neutralColor;
};
/**
*
* @param {string} maxColor
*/
User.prototype.setMaxColor = function (maxColor) {
this._maxColor = maxColor;
};
/**
*
* @returns {string}
*/
User.prototype.getMaxColor = function () {
return this._maxColor;
};
/**
*
* @param {string} password
*/
User.prototype.setPassword = function (password) {
this._password = password;
};
/**
*
* @returns {string}
*/
User.prototype.getPassword = function () {
return this._password;
};
/**
*
* @param {Object[]} privileges
*/
User.prototype.setPrivileges = function (privileges) {
this._privileges = privileges;
if (this._privileges === undefined) {
......@@ -119,10 +211,18 @@ User.prototype.setPrivileges = function (privileges) {
}
};
/**
*
* @returns {Array}
*/
User.prototype.getPrivileges = function () {
return this._privileges;
};
/**
*
* @param {UserPreferences|Object} preferences
*/
User.prototype.setPreferences = function (preferences) {
if (!(preferences instanceof UserPreferences)) {
preferences = new UserPreferences(preferences);
......@@ -130,14 +230,31 @@ User.prototype.setPreferences = function (preferences) {
this._preferences = preferences;
};
/**
*
* @returns {UserPreferences}
*/
User.prototype.getPreferences = function () {
return this._preferences;
};
/**
*
* @param {PrivilegeType} type
* @param {number} [objectId]
* @returns {boolean}
*/
User.prototype.hasPrivilege = function (type, objectId) {
return this.getPrivilegeValue(type, objectId) > 0;
};
/**
*
* @param {Object} params
* @param {number} params.objectId
* @param {PrivilegeType} params.type
* @param {number} [params.value=1]
*/
User.prototype.setPrivilege = function (params) {
var objectId = params.objectId;
var type = params.type;
......@@ -165,6 +282,13 @@ User.prototype.setPrivilege = function (params) {
});
};
/**
*
* @param {PrivilegeType} type
* @param {number} [objectId]
*
* @returns {number}
*/
User.prototype.getPrivilegeValue = function (type, objectId) {
for (var i = 0; i < this._privileges.length; i++) {
var privilege = this._privileges[i];
......@@ -177,6 +301,12 @@ User.prototype.getPrivilegeValue = function (type, objectId) {
return 0;
};
/**
*
* @param {Configuration} configuration
*
* @returns {Object}
*/
User.prototype.privilegesToExport = function (configuration) {
var self = this;
var result = {};
......@@ -201,6 +331,11 @@ User.prototype.privilegesToExport = function (configuration) {