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

edit project privileges uses grant/revoke privileges for project

parent e547f063
"use strict";
/* exported logger */
// noinspection JSUnusedLocalSymbols
var logger = require('./logger');
/**
*
* @param {string} message
* @constructor
* @extends Error
*/
function ObjectNotFoundError(message) {
this.message = message;
this.stack = (new Error(message)).stack;
}
ObjectNotFoundError.prototype = Object.create(Error.prototype);
ObjectNotFoundError.prototype.constructor = ObjectNotFoundError;
module.exports = ObjectNotFoundError;
......@@ -28,6 +28,7 @@ var Mesh = require('./map/data/Mesh');
var MiRna = require('./map/data/MiRna');
var NetworkError = require('./NetworkError');
var ObjectExistsError = require('./ObjectExistsError');
var ObjectNotFoundError = require('./ObjectNotFoundError');
var PluginData = require('./map/data/PluginData');
var Project = require('./map/data/Project');
var ProjectStatistics = require('./map/data/ProjectStatistics');
......@@ -475,6 +476,34 @@ ServerConnector.getProjectUrl = function (queryParams, filterParams) {
});
};
/**
*
* @param queryParams
* @param filterParams
* @return {string}
*/
ServerConnector.getGrantProjectPrivilegesUrl = function (queryParams, filterParams) {
var id = this.getIdOrAsterisk(queryParams.projectId);
return this.getApiUrl({
url: this.getProjectsUrl(queryParams) + id + ":grantPrivileges",
params: filterParams
});
};
/**
*
* @param queryParams
* @param filterParams
* @return {string}
*/
ServerConnector.getRevokeProjectPrivilegesUrl = function (queryParams, filterParams) {
var id = this.getIdOrAsterisk(queryParams.projectId);
return this.getApiUrl({
url: this.getProjectsUrl(queryParams) + id + ":revokePrivileges",
params: filterParams
});
};
ServerConnector.getProjectStatisticsUrl = function (queryParams, filterParams) {
return this.getApiUrl({
url: this.getProjectUrl(queryParams) + "statistics/",
......@@ -1489,6 +1518,62 @@ ServerConnector.grantUserPrivileges = function (params) {
});
};
/**
*
* @param {Object} params
* @param {User} params.user
* @param {string} params.projectId
* @param {string} params.privilegeType
*
* @returns {Promise}
*/
ServerConnector.grantProjectPrivilege = function (params) {
var self = this;
var queryParams = {
projectId: params.projectId
};
var privileges = [{login: params.user.getLogin(), privilegeType: params.privilegeType}];
return self.sendPatchRequest(self.getGrantProjectPrivilegesUrl(queryParams), privileges).then(function (content) {
if (self._usersByLogin[params.user.getLogin()] !== undefined) {
self._usersByLogin[params.user.getLogin()].setPrivilege({
privilegeType: params.privilegeType,
objectId: params.projectId
});
}
}).catch(function (error) {
return self.processNetworkError(error, true);
});
};
/**
*
* @param {Object} params
* @param {User} params.user
* @param {string} params.projectId
* @param {string} params.privilegeType
*
* @returns {Promise}
*/
ServerConnector.revokeProjectPrivilege = function (params) {
var self = this;
var queryParams = {
projectId: params.projectId
};
var privileges = [{login: params.user.getLogin(), privilegeType: params.privilegeType}];
return self.sendPatchRequest(self.getRevokeProjectPrivilegesUrl(queryParams), privileges).then(function (content) {
if (self._usersByLogin[params.user.getLogin()] !== undefined) {
self._usersByLogin[params.user.getLogin()].revokePrivilege({
privilegeType: params.privilegeType,
objectId: params.projectId
});
}
}).catch(function (error) {
return self.processNetworkError(error, true);
});
};
/**
*
* @param {Object} params
......@@ -1531,12 +1616,16 @@ ServerConnector.revokeUserPrivileges = function (params) {
/**
*
* @param {Error} error
* @param {boolean} [forbidNotFound=false]
* @returns {Promise}
*/
ServerConnector.processNetworkError = function (error) {
ServerConnector.processNetworkError = function (error, forbidNotFound) {
if ((error instanceof NetworkError)) {
switch (error.statusCode) {
case HttpStatus.NOT_FOUND:
if (forbidNotFound) {
return Promise.reject(new ObjectNotFoundError("Object not found."));
}
return null;
case HttpStatus.CONFLICT:
return Promise.reject(new ObjectExistsError("Object already exists."));
......
......@@ -596,20 +596,16 @@ EditProjectDialog.prototype.createUsersTabContent = function () {
var type = $(this).attr("data").split(",")[0];
var login = $(this).attr("data").split(",")[1];
if ($(this).prop("checked")) {
return self.grantPrivilege(self._userByLogin[login], {
privilegeType: type,
objectId: self.getProject().getProjectId()
});
return self.grantPrivilege(self._userByLogin[login], type, self.getProject().getProjectId());
} else {
return self.revokePrivilege(self._userByLogin[login], {
privilegeType: type,
objectId: self.getProject().getProjectId()
});
return self.revokePrivilege(self._userByLogin[login], type, self.getProject().getProjectId());
}
});
})
;
return result;
};
}
;
/**
*
......@@ -1140,30 +1136,34 @@ EditProjectDialog.prototype.updateMap = function (map) {
/**
*
* @param {User} user
* @param {Authority} privilege
* @param {string} privilegeType
* @param {string} projectId
* @returns {Promise}
*/
EditProjectDialog.prototype.grantPrivilege = function (user, privilege) {
EditProjectDialog.prototype.grantPrivilege = function (user, privilegeType, projectId) {
var self = this;
GuiConnector.showProcessing();
return self.getServerConnector().grantUserPrivileges({
return self.getServerConnector().grantProjectPrivilege({
projectId: projectId,
user: user,
privileges: [privilege]
privilegeType: privilegeType
}).catch(GuiConnector.alert).finally(GuiConnector.hideProcessing);
};
/**
*
* @param {User} user
* @param {Authority} privilege
* @param {string} privilegeType
* @param {string} projectId
* @returns {Promise}
*/
EditProjectDialog.prototype.revokePrivilege = function (user, privilege) {
EditProjectDialog.prototype.revokePrivilege = function (user, privilegeType, projectId) {
var self = this;
GuiConnector.showProcessing();
return self.getServerConnector().revokeUserPrivileges({
return self.getServerConnector().revokeProjectPrivilege({
projectId: projectId,
user: user,
privileges: [privilege]
privilegeType: privilegeType
}).catch(GuiConnector.alert).finally(GuiConnector.hideProcessing);
};
......
......@@ -5,6 +5,7 @@ require("../../mocha-config");
var EditProjectDialog = require('../../../../main/js/gui/admin/EditProjectDialog');
var ServerConnector = require('../../ServerConnector-mock');
var logger = require('../../logger');
var PrivilegeType = require('../../../../main/js/map/data/PrivilegeType');
var chai = require('chai');
var assert = chai.assert;
......@@ -125,5 +126,39 @@ describe('EditProjectDialog', function () {
});
});
it('grantPrivilege', function () {
var dialog;
var user;
helper.loginAsAdmin();
return createDialog().then(function (result) {
dialog = result;
return ServerConnector.getUser("anonymous");
}).then(function (result) {
user = result;
return dialog.grantPrivilege(user, PrivilegeType.WRITE_PROJECT, "sample");
}).then(function () {
assert.ok(user.hasPrivilege(helper.getConfiguration().getPrivilegeType(PrivilegeType.WRITE_PROJECT), "sample"));
return dialog.destroy();
});
});
it('revokePrivilege', function () {
var dialog;
var user;
helper.loginAsAdmin();
return createDialog().then(function (result) {
dialog = result;
return ServerConnector.getUser("anonymous");
}).then(function (result) {
user = result;
return dialog.grantPrivilege(user, PrivilegeType.WRITE_PROJECT, "sample");
}).then(function () {
return dialog.revokePrivilege(user, PrivilegeType.WRITE_PROJECT, "sample");
}).then(function () {
assert.notOk(user.hasPrivilege(helper.getConfiguration().getPrivilegeType(PrivilegeType.WRITE_PROJECT), "sample"));
return dialog.destroy();
});
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment