WIP: #5770 - Sync Groups Samba #1946
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue