CoreID/app/models/ldap/Group.model.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-05-04 01:16:54 +00:00
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,
2020-05-12 01:26:09 +00:00
active: {type: Boolean, default: true},
2020-05-04 01:16:54 +00:00
ldap_visible: {type: Boolean, default: true},
}
}
2020-05-12 01:26:09 +00:00
async to_api() {
return {
id: this.id,
role: this.role,
user_ids: this.user_ids,
name: this.name,
ldap_visible: this.ldap_visible,
}
}
2020-05-04 01:16:54 +00:00
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