test(samba): refs #5770create class SambaHelper

This commit is contained in:
Javier Segarra 2024-05-11 15:48:25 +02:00
parent 4222e2d4d9
commit 50997f6c7b
1 changed files with 186 additions and 172 deletions

View File

@ -172,38 +172,63 @@ module.exports = Self => {
await this.adClient.searchForeach(this.fullUsersDn, opts,
o => usersToSync.add(o.sAMAccountName));
},
async syncRoles() {
await this.init();
// Prepare data
const sambaHandler = new SambaHelper(this);
try {
await sambaHandler.syncFromDB();
await sambaHandler.syncMembers();
} catch (error) {
console.error(error);
}
},
});
};
class SambaHelper {
constructor(ctx) {
Object.assign(this, ctx);
}
deleteRole(role) {
return this.sambaTool('group', ['delete', role]);
},
}
addRole({description, name}) {
return this.sambaTool('group',
['add', `${ROLE_PREFIX}${name}`, `--groupou=${this.groupDn}`, `--description=${description}`]);
},
}
getRoleMembers(role) {
return this.getMembers(`(cn=${role})`, this.fullGroupsDn);
},
}
getMembers(filter = '', type = this.fullUsersDn) {
const options = {
scope: 'sub',
attributes: ['cn', 'member', 'member.cn']
attributes: ['cn', 'member']
};
if (filter !== '')
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));
@ -219,23 +244,13 @@ module.exports = Self => {
this.addmembers(role, user)));
printResults(results);
}
},
async syncRoles() {
await this.init();
let $ = app.models;
// let {
// client,
// accountConfig
// } = this;
// Prepare data
try {
// const filter = '(cn=VN_*)';
}
async syncFromDB() {
// const baseDN = 'cn=Users,dc=verdnatura,dc=es';
const ldapMembersGroups = await this.getMembers();
// OBTENER ROLES
let roles = (await $.VnRole.find({
let roles = (await app.models.VnRole.find({
fields: ['id', 'name', 'description'],
order: 'modified DESC',
limit: 2
@ -275,7 +290,7 @@ module.exports = Self => {
rolesToDelete.length > 0 ||
rolesToInsert.length > 0 ||
rolesToUpdate.length > 0) {
let users = await $.VnUser.find({
let users = await app.models.VnUser.find({
include: {
relation: 'role',
scope: {fields: ['name'],
@ -348,9 +363,8 @@ module.exports = Self => {
}
}
}
} catch (error) {
console.error(error);
}
},
});
};
syncMembers() {
}
}