const LDAPBase = require('../LDAPBase') const LDAP = require('ldapjs') class GroupModel extends LDAPBase { static get services() { return [...super.services, 'configs', 'ldap_server', 'models'] } static get schema() { return { role: String, user_ids: [String], name: String, active: {type: Boolean, default: true}, ldap_visible: {type: Boolean, default: true}, } } async to_api() { return { id: this.id, role: this.role, user_ids: this.user_ids, name: this.name, ldap_visible: this.ldap_visible, } } get dn() { return LDAP.parseDN(`cn=${this.name},${this.ldap_server.group_dn().format(this.configs.get('ldap:server.format'))}`) } async users() { const User = this.models.get('auth:User') return User.find({ $or: [ { _id: { $in: this.user_ids.map(x => this.constructor.to_object_id(x)) } }, { roles: this.role }, ], }) } async to_ldap() { const users = await this.users() return { cn: this.name, dn: this.dn.format(this.configs.get('ldap:server.format')), objectClass: 'groupOfNames', member: users.map(x => x.dn.format(this.configs.get('ldap:server.format'))) } } } module.exports = exports = GroupModel