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