Remove ldap:Group model

This commit is contained in:
garrettmills
2020-05-20 20:35:17 -05:00
parent ea77402750
commit faab948a6b
7 changed files with 41 additions and 99 deletions

View File

@@ -1,9 +1,10 @@
const { Model } = require('flitter-orm')
const LDAP = require('ldapjs')
// For organizational purposes only.
class GroupModel extends Model {
static get services() {
return [...super.services, 'models']
return [...super.services, 'models', 'ldap_server', 'configs']
}
static get schema() {
@@ -11,6 +12,7 @@ class GroupModel extends Model {
name: String,
user_ids: [String],
active: { type: Boolean, default: true },
ldap_visible: { type: Boolean, default: true },
}
}
@@ -18,16 +20,35 @@ class GroupModel extends Model {
return this.name.toLowerCase().replace(/\s/g, '_')
}
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 await User.find({ _id: { $in: this.user_ids.map(x => this.constructor.to_object_id(x)) } })
}
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'))),
}
}
static async ldap_directory() {
return this.find({ ldap_visible: true, active: true })
}
async to_api() {
return {
id: this.id,
name: this.name,
user_ids: this.user_ids,
ldap_visible: this.ldap_visible,
}
}
}

View File

@@ -153,16 +153,6 @@ class User extends AuthUser {
return Group.find({ active: true, user_ids: this.id })
}
async ldap_groups() {
const Group = this.models.get('ldap:Group')
return await Group.find({
$or: [
{ user_ids: this.id },
{ role: { $in: this.roles } },
],
})
}
async to_ldap() {
const ldap_data = {
uid: this.uid,
@@ -182,10 +172,11 @@ class User extends AuthUser {
ldap_data[`data${key.substr(4)}`] = `${addl_data[key]}`
}
const ldap_groups = await this.ldap_groups()
if ( ldap_groups.length > 0 ) {
ldap_data.memberOf = ldap_groups.map(x => x.dn.format(this.configs.get('ldap:server.format')))
ldap_data.memberof = ldap_groups.map(x => x.dn.format(this.configs.get('ldap:server.format')))
const groups = await this.groups()
if ( groups.length > 0 ) {
const group_data = groups.map(x => x.dn.format(this.configs.get('ldap:server.format')))
ldap_data.memberOf = group_data
ldap_data.memberof = group_data
}
return ldap_data

View File

@@ -1,54 +0,0 @@
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