const AuthUser = require('flitter-auth/model/User') const LDAP = require('ldapjs') const ActiveScope = require('../scopes/ActiveScope') /* * Auth user model. This inherits fields and methods from the default * flitter-auth/model/User model, however you can override methods and * properties here as you need. */ class User extends AuthUser { static get services() { return [...super.services, 'auth', 'ldap_server', 'ldap_dn_format'] } static get schema() { return {...super.schema, ...{ // other schema fields here first_name: String, last_name: String, email: String, ldap_visible: {type: Boolean, default: true}, active: {type: Boolean, default: true}, }} } static scopes = [ new ActiveScope({}) ] static async ldap_directory() { return this.find({ldap_visible: true}) } // Prefer soft delete because of the active scope async delete() { this.active = false await this.save() } async check_password(password) { return this.get_provider().check_user_auth(this, password) } to_ldap() { const ldap_data = { uid: this.uid, uuid: this.uuid, cn: this.first_name, sn: this.last_name, gecos: `${this.first_name} ${this.last_name}`, mail: this.email, objectClass: 'inetOrgPerson', dn: this.dn.format(this.ldap_dn_format), } const addl_data = JSON.parse(this.data) for ( const key in addl_data ) { if ( !addl_data.hasOwnProperty(key) || !key.startsWith('ldap_') ) continue ldap_data[`data${key.substr(4)}`] = `${addl_data[key]}` } return ldap_data } get dn() { return LDAP.parseDN(`uid=${this.uid},${this.ldap_server.auth_dn().format(this.ldap_dn_format)}`) } } module.exports = exports = User