class usersgroups {
constructor(logger) {
// always initialize all instance properties
this._logger = logger;
}
/**
* Loads List of user groups from core database
* @param {function} callback rows or null
*/
getListOfGroups(callback) {
let query = 'SELECT ' +
'*, ' +
'( ' +
'SELECT count(userId) ' +
'FROM xt_users_users_groups ' +
'WHERE users_groups.id = xt_users_users_groups.groupId ' +
') ' +
'accountCount ' +
'FROM users_groups';
global.coreDb.all(query, [], (err, rows) => {
if (err) {
this._logger.error('USERSGROUPS-MODEL | Error loading users groups from core data: ' + err.message);
return callback(null);
} else {
return callback(rows);
}
});
}
/**
* Adds a new User Group to core database
* @param {string} groupname groupname of group to add
* @param {integer} isSystemGroup 1 == Yes, 0 == No
* @param {string} description Description of the group
* @param {string} grantnamespace namespace of group to be set
* @param {*} callback success (true or false)
*/
addGroup(groupname, isSystemGroup, description, grantnamespace, callback) {
this.doesGroupExist(groupname, (checkResult) => {
if (checkResult === null) {
// Error checking core database
return callback(null);
} else if (checkResult === true) {
// Username exists, or for this basedata id another user exists
return callback(false);
} else if (checkResult === false) {
// No user exists for this basedata id and the username does not exist in core database
// Go on and create user
let query = "INSERT INTO `users_groups` (title, isSystemGroup, description, grantnamespace) VALUES (?,?,?,?)";
global.coreDb.run(query,
[
groupname,
isSystemGroup,
description,
grantnamespace
],
function (err) {
if (err) {
console.log('ERROR: ' + err.message);
callback(false);
} else {
callback(true);
}
});
}
})
}
/**
* Checks if a group exists or not
* @param {string} groupname name of group
* @param {function} callback true or false (user group exists or not), null if error querying core database
*/
doesGroupExist(groupname, callback) {
let query = "SELECT * FROM `users_groups` WHERE title = ?";
global.coreDb.all(query, [
groupname
], (err, rows) => {
if (err) {
this._logger.error('USERSGROUPS-MODEL | Error checking if user group exists in core data: ' + err.message);
return callback(null);
} else {
if (rows.length > 0) {
return callback(true);
} else {
return callback(false);
}
}
});
}
/**
*
* @param {integer} gId id of group to be updated
* @param {string} groupname new groupname
* @param {integer} isSystemGroup 1 == Yes, 0 == No
* @param {string} description Description of the group
* @param {function} callback success, true of false
*/
updateGroup(gId, groupname, isSystemGroup, description, grantnamespace, callback) {
this.doesGroupExist(groupname, (exists) => {
if (exists) {
// new Groupname exists
return callback(false);
} else {
let query = 'UPDATE users_groups SET title = ?, isSystemGroup = ?, description = ?, grantnamespace = ? WHERE id = ?';
global.coreDb.run(query,
[
groupname,
isSystemGroup,
description,
grantnamespace,
gId
],
function (err) {
if (err) {
console.log('ERROR: ' + err.message);
callback(false);
} else {
callback(true);
}
});
}
})
}
/**
* Removes a user group from core database
* @param {integer} gID user id of user group to be removed from core database
* @param {function} callback success, true or false
*/
removeGroup(gID, callback) {
let query = "DELETE FROM users_group WHERE id = ?";
global.coreDb.run(query,
[
gID
],
function (err) {
if (err) {
console.log('ERROR: ' + err.message);
callback(false);
} else {
callback(true);
}
});
}
/**
* Updates the granted users groups for given user
* @param {integer} userId id of user to be updated
* @param {object} reqBody form data containing the usergroups
* @param {function} callback success (true/false)
*/
updateUsersGroups(userId, reqBody, callback) {
var newGrants = [];
var allGroups = this.getListOfGroups((rowsGroups) => {
rowsGroups.forEach((group) => {
if (typeof reqBody['usersgroup[' + group.id + ']'] !== 'undefined') {
console.log('group: ' + group.title + ' state: ' + reqBody['usersgroup[' + group.id + ']']);
newGrants.push(group.id);
}
})
// Now update database
// Check if combination exists
let query = "DELETE FROM xt_users_users_groups WHERE userId = ?";
global.coreDb.run(query,
[
userId
],
function (err) {
if (err) {
this._logger.error('USERSGROUPS-MODEL | Error purging existing user for user groups in core database: ' + err.message);
return callback(null);
} else {
// purged existing access rights -> now render the new ones
if (newGrants.length > 0) {
// New access groups need to be persisted
global.coreDb.serialize(() => {
newGrants.forEach((groupId) => {
let queryInsert = 'INSERT INTO xt_users_users_groups (userId, groupId) VALUES (?,?)';
global.coreDb.run(queryInsert,
[
userId,
groupId
],
function (err) {
if (err) {
console.log('ERROR: ' + err.message);
} else {
console.log('Successfully persisted new access group ' + groupId + ' for user ' + userId);
}
});
})
});
return callback(true);
} else {
// no new access groups defined.
return callback(true);
}
}
});
})
}
}
// export the class
module.exports = usersgroups;