refs #5770 feat usersMap

This commit is contained in:
Javier Segarra 2024-01-30 08:46:53 +01:00
parent 48a533e4a9
commit ca1ead228f
1 changed files with 27 additions and 25 deletions

View File

@ -88,17 +88,16 @@ async function syncRoles() {
// OBTENER USUARIOS Y SUS ROLES
if (rolesToDelete.length > 0 || rolesToInsert.length > 0) {
let users = await $.VnUser.find({
include: {
relation: 'role',
scope: {fields: ['name'],
where: {'name': {inq: rolesToInsert}}
}
},
fields: ['name', 'roleFk'],
where: {'active': true}
include: {
relation: 'role',
scope: {fields: ['name'],
where: {'name': {inq: rolesToInsert}}
}
},
fields: ['name', 'roleFk'],
where: {'active': true}
});
let accounts = await $.Account.find({
/* let accounts = await $.Account.find({
fields: ['id'],
include: {
relation: 'user',
@ -113,20 +112,21 @@ async function syncRoles() {
where: {'active': true}
}
}
});
let userMap = toMap(users, user => {
});*/
let usersMap = toMap(users, user => {
let role = user.role();
if (!role) return;
return {key: role.name, val: user.name};
});
console.log(userMap);
let accountMap = toMap(accounts, e => {
usersMap.set('group1', ['employee']);
console.log(usersMap);
/* toMap(accounts, e => {
let user = e.user();
if (!user) return;
let role = user.role();
if (!role) return;
return {key: role.name, val: user.name};
});
});*/
// for (const [key, value] of accountMap) {
// value.forEach(v => {
// console.log(v);
@ -137,17 +137,18 @@ async function syncRoles() {
if (rolesToDelete.length > 0) {
// PROCEDIMIENTO PARA ELIMINAR USUARIOS ASOCIADOS AL ROL
let usersToDelete = rolesToDelete.flatMap(role => {
const exist = accountMap.get(role);
const exist = usersMap.get(role);
if (exist) {
return accountMap.get(role)?.map(
return usersMap.get(role)?.map(
a => sambaTool('group', ['removemembers', role, a])
);
} else return [];
}
);
const resultsUserDelete = await Promise.all(usersToDelete);
expect(resultsUserDelete.length).toEqual(0);
expect(resultsUserDelete.length).toEqual(1);
expect(resultsUserDelete[0]).toEqual('samba-tool removemembers group1 employee');
// PROCEDIMIENTO PARA ELIMINAR ROLES
const resultsRoleDelete = await Promise.all(
rolesToDelete.map(role => sambaTool('group', ['delete', role]))
@ -158,7 +159,14 @@ async function syncRoles() {
if (rolesToInsert.length > 0) {
// PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL
let usersToInsert = rolesToInsert.flatMap(role => accountMap.get(role).map(
const resultsRoleInsert = await Promise.all(
rolesToInsert.map(role => sambaTool('group', ['add', role]))
);
expect(resultsRoleInsert.length).toEqual(2);
expect(resultsRoleInsert[0]).toEqual('samba-tool add administrative');
expect(resultsRoleInsert[1]).toEqual('samba-tool add guest');
let usersToInsert = rolesToInsert.flatMap(role => usersMap.get(role).map(
a => sambaTool('group', ['addmembers', role, a])
)
);
@ -167,12 +175,6 @@ async function syncRoles() {
expect(resultsUserInsert[0]).toEqual('samba-tool addmembers administrative administrative');
expect(resultsUserInsert[1]).toEqual('samba-tool addmembers guest guest');
// PROCEDIMIENTO PARA INSERTAR ROLES
const resultsRoleInsert = await Promise.all(
rolesToInsert.map(role => sambaTool('group', ['add', role]))
);
expect(resultsRoleInsert.length).toEqual(2);
expect(resultsRoleInsert[0]).toEqual('samba-tool add administrative');
expect(resultsRoleInsert[1]).toEqual('samba-tool add guest');
}
}
// let roleRoles = await $.RoleRole.find({