const { Model } = require('flitter-orm') const LDAP = require('ldapjs') const bcrypt = require('bcrypt') class MachineModel extends Model { static get services() { return [...super.services, 'models', 'ldap_server', 'configs'] } static get schema() { return { name: String, bind_password: String, description: String, host_name: String, location: String, active: { type: Boolean, default: true }, ldap_visible: { type: Boolean, default: true }, } } async to_api() { return { id: this.id, name: this.name, description: this.description, host_name: this.host_name, location: this.location, ldap_visible: this.ldap_visible, iam_filter: `(|(iamTarget=${this.id}))`, } } async groups() { const MachineGroup = this.models.get('ldap:MachineGroup') return MachineGroup.find({ machine_ids: this.id, active: true }) } async set_bind_password(password) { this.bind_password = await bcrypt.hash(password, 10) return this } async check_bind_password(password) { return await bcrypt.compare(password, this.bind_password) } get dn() { return LDAP.parseDN(`cn=${this.name},${this.ldap_server.machine_dn().format(this.configs.get('ldap:server.format'))}`) } async to_ldap() { const data = { cn: this.name, dn: this.dn.format(this.configs.get('ldap:server.format')), name: this.name, id: this.id, objectClass: ['computer'], description: this.description, dNSHostName: this.host_name, location: this.location, primaryGroupID: 515, // compat with AD sAMAccountType: 805306369, // compat with AD } return data; } } module.exports = exports = MachineModel