From 4222e2d4d9bdb60ee3e9b59a56aa18c04087bcb2 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 10 May 2024 22:18:42 +0200 Subject: [PATCH] test(samba): refs #5770 minor clean-code --- modules/account/back/mixins/account-linker.js | 6 +- modules/account/back/models/samba-config.js | 58 ++++++++++--------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/modules/account/back/mixins/account-linker.js b/modules/account/back/mixins/account-linker.js index 6f399a993..d9871ddeb 100644 --- a/modules/account/back/mixins/account-linker.js +++ b/modules/account/back/mixins/account-linker.js @@ -55,13 +55,15 @@ module.exports = function(Self, options) { /** * Synchronizes roles. */ - async syncRoles() { try { + async syncRoles() { + try { await this.syncRoles(); } catch (e) { let err = new UserError(e.message); err.name = e.name; throw err; - }}, + } + }, /** * Tests linker configuration. diff --git a/modules/account/back/models/samba-config.js b/modules/account/back/models/samba-config.js index f8bab6db3..38a2555fb 100644 --- a/modules/account/back/models/samba-config.js +++ b/modules/account/back/models/samba-config.js @@ -191,6 +191,35 @@ module.exports = Self => { Object.assign(options, {filter}); return this.adClient.searchAll(type, options); }, + removeMembers(role, user) { + return this.sambaTool('group', ['removemembers', role, user]); + }, + addMembers(role, user) { + return this.sambaTool('group', ['addmembers', role, user]); + }, + handleRoleMembers(users) { + if (users.length === 0) return []; + let members = users[0]?.member; + if (!members) return []; + if (!Array.isArray(members))members = [members]; + return members.map((member => member.match(/CN=(.*?),(.*)/)[1])); + }, + async handleUsersRole(role, currentUsers, users) { + const forbiddenUsers = ['guest']; + users = users.filter(u => !u.includes(forbiddenUsers)); + const usersToDelete = differences(currentUsers, users); + if (usersToDelete.length > 0) { + const results = await Promise.all(usersToDelete.map(user => + this.removemembers(role, user))); + printResults(results); + } + const usersToInsert = differences(users, currentUsers); + if (usersToInsert.length > 0) { + const results = await Promise.all(usersToInsert.map(user => + this.addmembers(role, user))); + printResults(results); + } + }, async syncRoles() { await this.init(); let $ = app.models; @@ -272,7 +301,7 @@ module.exports = Self => { if (exist) { return usersMap.get(role)?.map( - a => this.sambaTool('group', ['removemembers', role, a]) + user => this.removeMembers(role, user) ); } else return []; } @@ -295,7 +324,7 @@ module.exports = Self => { // PROCEDIMIENTO PARA INSERTAR USUARIOS ASOCIADOS AL ROL let usersToGroup = rolesToInsert.flatMap(role => usersMap.get(role).map( - a => this.sambaTool('group', ['addmembers', role, a]) + user => this.addMembers(role, user) ) ); const resultsUserGroup = await Promise.all(usersToGroup); @@ -303,7 +332,6 @@ module.exports = Self => { } if (rolesToUpdate.length > 0) { - let promises = []; // OBTENER LDAPSJS MIEMBROS ROLES for await (const role of rolesToUpdate) { @@ -324,29 +352,5 @@ module.exports = Self => { console.error(error); } }, - handleRoleMembers(users) { - if (users.length === 0) return []; - let members = users[0]?.member; - if (!members) return []; - if (!Array.isArray(members))members = [members]; - return members.map((member => member.match(/CN=(.*?),(.*)/)[1])); - }, - async handleUsersRole(role, currentUsers, users) { - const forbiddenUsers = ['guest']; - users = users.filter(u => !u.includes(forbiddenUsers)); - const usersToDelete = differences(currentUsers, users); - if (usersToDelete.length > 0) { - const results = await Promise.all(usersToDelete.map(user => - this.sambaTool('group', ['removemembers', role, user]))); - printResults(results); - } - const usersToInsert = differences(users, currentUsers); - if (usersToInsert.length > 0) { - const results = await Promise.all(usersToInsert.map(user => - this.sambaTool('group', ['addmembers', role, user]))); - printResults(results); - } - } - }); };