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

setting/revoking privileges locally fixed

parent eda9c1f7
Pipeline #11797 failed with stage
in 8 minutes and 36 seconds
......@@ -411,11 +411,16 @@ EditUserDialog.prototype.initPrivilegesTab = function () {
*/
EditUserDialog.prototype.grantPrivilege = function (privilege) {
var self = this;
GuiConnector.showProcessing();
return self.getServerConnector().grantUserPrivileges({
user: self.getUser(),
privileges: [privilege]
}).catch(GuiConnector.alert).finally(GuiConnector.hideProcessing);
if (self._isNewUser) {
self.getUser().setPrivilege(privilege);
return Promise.resolve();
} else {
GuiConnector.showProcessing();
return self.getServerConnector().grantUserPrivileges({
user: self.getUser(),
privileges: [privilege]
}).catch(GuiConnector.alert).finally(GuiConnector.hideProcessing);
}
};
/**
......@@ -425,11 +430,16 @@ EditUserDialog.prototype.grantPrivilege = function (privilege) {
*/
EditUserDialog.prototype.revokePrivilege = function (privilege) {
var self = this;
GuiConnector.showProcessing();
return self.getServerConnector().revokeUserPrivileges({
user: self.getUser(),
privileges: [privilege]
}).catch(GuiConnector.alert).finally(GuiConnector.hideProcessing);
if (self._isNewUser()) {
self.getUser().removePrivilege(privilege);
return Promise.resolve();
} else {
GuiConnector.showProcessing();
return self.getServerConnector().revokeUserPrivileges({
user: self.getUser(),
privileges: [privilege]
}).catch(GuiConnector.alert).finally(GuiConnector.hideProcessing);
}
};
/**
......
......@@ -255,7 +255,7 @@ User.prototype.getPreferences = function () {
* @returns {boolean}
*/
User.prototype.hasPrivilege = function (type, objectId) {
if (objectId===undefined) {
if (objectId === undefined) {
objectId = null;
}
for (var i = 0; i < this._privileges.length; i++) {
......@@ -271,36 +271,38 @@ User.prototype.hasPrivilege = function (type, objectId) {
/**
*
* @param {Object} params
* @param {number|string} [params.objectId]
* @param {PrivilegeType} params.type
* @param {boolean} [params.value=true]
* @param {Authority} authority
*/
User.prototype.setPrivilege = function (params) {
var objectId = params.objectId;
var type = params.type;
var value = params.value;
if (value === undefined) {
value = 1;
} else if (value === true) {
value = 1;
} else if (value === false) {
value = 0;
User.prototype.setPrivilege = function (authority) {
if (authority.objectId === undefined) {
authority.objectId = null;
}
for (var i = 0; i < this._privileges.length; i++) {
var privilege = this._privileges[i];
if (privilege.type === type.getName()) {
if (objectId === privilege.objectId) {
privilege.value = value;
return;
}
if (privilege.privilegeType === authority.privilegeType &&
privilege.objectId === authority.objectId) {
return;
}
}
this._privileges.push(authority);
};
/**
*
* @param {Authority} authority
*/
User.prototype.revokePrivilege = function (authority) {
if (authority.objectId === undefined) {
authority.objectId = null;
}
for (var i = 0; i < this._privileges.length; i++) {
var privilege = this._privileges[i];
if (privilege.privilegeType === authority.privilegeType &&
privilege.objectId === authority.objectId) {
this._privileges.splice(i, 1);
return;
}
}
this._privileges.push({
type: type.getName(),
value: value,
objectId: objectId
});
};
/**
......
......@@ -37,9 +37,9 @@ describe('User', function () {
assert.ok(user);
return ServerConnector.getConfiguration().then(function (configuration) {
var privilegeType = configuration.getPrivilegeTypes()[0];
user.setPrivilege({type: privilegeType, value: true});
assert.ok(user.hasPrivilege(privilegeType));
var privilege = configuration.getPrivilegeTypes()[0];
user.setPrivilege({privilegeType: privilege.getName()});
assert.ok(user.hasPrivilege(privilege));
});
});
......@@ -48,22 +48,23 @@ describe('User', function () {
assert.ok(user);
return ServerConnector.getConfiguration().then(function (configuration) {
var privilegeType = configuration.getPrivilegeTypes()[0];
user.setPrivilege({type: privilegeType, value: false});
user.setPrivilege({type: privilegeType, value: true});
assert.ok(user.hasPrivilege(privilegeType));
var privilege = configuration.getPrivilegeTypes()[0];
user.setPrivilege({privilegeType: privilege.getName()});
user.setPrivilege({privilegeType: privilege.getName()});
assert.ok(user.hasPrivilege(privilege));
});
});
it("undefined value", function () {
it("revoked privilege", function () {
var user = new User({});
assert.ok(user);
return ServerConnector.getConfiguration().then(function (configuration) {
var privilegeType = configuration.getPrivilegeTypes()[0];
user.setPrivilege({type: privilegeType, value: false});
user.setPrivilege({type: privilegeType});
assert.ok(user.hasPrivilege(privilegeType));
var privilege = configuration.getPrivilegeTypes()[0];
user.revokePrivilege({privilegeType: privilege.getName()});
assert.notOk(user.hasPrivilege(privilege));
user.setPrivilege({privilegeType: privilege.getName()});
assert.ok(user.hasPrivilege(privilege));
});
});
});
......
Supports Markdown
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