123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- 'use strict'
- const warning = require('./deprecations')
- const RDN = require('./rdn')
- const parseString = require('./utils/parse-string')
- class DN {
- #rdns = []
-
- constructor ({ rdns = [] } = {}) {
- if (Array.isArray(rdns) === false) {
- throw Error('rdns must be an array')
- }
- const hasNonRdn = rdns.some(
- r => RDN.isRdn(r) === false
- )
- if (hasNonRdn === true) {
- throw Error('rdns must be an array of RDN objects')
- }
- Array.prototype.push.apply(
- this.#rdns,
- rdns.map(r => {
- if (Object.prototype.toString.call(r) === '[object LdapRdn]') {
- return r
- }
- return new RDN(r)
- })
- )
- }
- get [Symbol.toStringTag] () {
- return 'LdapDn'
- }
-
- get length () {
- return this.#rdns.length
- }
-
- childOf (dn) {
- if (typeof dn === 'string') {
- const parsedDn = DN.fromString(dn)
- return parsedDn.parentOf(this)
- }
- return dn.parentOf(this)
- }
-
- clone () {
- return new DN({ rdns: this.#rdns })
- }
-
- equals (dn) {
- if (typeof dn === 'string') {
- const parsedDn = DN.fromString(dn)
- return parsedDn.equals(this)
- }
- if (this.length !== dn.length) return false
- for (let i = 0; i < this.length; i += 1) {
- if (this.#rdns[i].equals(dn.rdnAt(i)) === false) {
- return false
- }
- }
- return true
- }
-
- format () {
- warning.emit('LDAP_DN_DEP_002')
- return this.toString()
- }
-
- isEmpty () {
- return this.#rdns.length === 0
- }
-
- parent () {
- if (this.length === 0) return undefined
- const save = this.shift()
- const dn = new DN({ rdns: this.#rdns })
- this.unshift(save)
- return dn
- }
-
- parentOf (dn) {
- if (typeof dn === 'string') {
- const parsedDn = DN.fromString(dn)
- return this.parentOf(parsedDn)
- }
- if (this.length >= dn.length) {
-
- return false
- }
- const numberOfElementsDifferent = dn.length - this.length
- for (let i = this.length - 1; i >= 0; i -= 1) {
- const myRdn = this.#rdns[i]
- const theirRdn = dn.rdnAt(i + numberOfElementsDifferent)
- if (myRdn.equals(theirRdn) === false) {
- return false
- }
- }
- return true
- }
-
- pop () {
- return this.#rdns.pop()
- }
-
- push (rdn) {
- if (Object.prototype.toString.call(rdn) !== '[object LdapRdn]') {
- throw Error('rdn must be a RDN instance')
- }
- return this.#rdns.push(rdn)
- }
-
- rdnAt (index) {
- return this.#rdns[index]
- }
-
- reverse () {
- this.#rdns.reverse()
- return this
- }
-
- setFormat () {
- warning.emit('LDAP_DN_DEP_004')
- }
-
- shift () {
- return this.#rdns.shift()
- }
-
- toString () {
- let result = ''
- for (const rdn of this.#rdns) {
- const rdnString = rdn.toString()
- result += `,${rdnString}`
- }
- return result.substring(1)
- }
-
- unshift (rdn) {
- if (Object.prototype.toString.call(rdn) !== '[object LdapRdn]') {
- throw Error('rdn must be a RDN instance')
- }
- return this.#rdns.unshift(rdn)
- }
-
- static isDn (dn) {
- if (Object.prototype.toString.call(dn) === '[object LdapDn]') {
- return true
- }
- if (
- Object.prototype.toString.call(dn) !== '[object Object]' ||
- Array.isArray(dn.rdns) === false
- ) {
- return false
- }
- if (dn.rdns.some(dn => RDN.isRdn(dn) === false) === true) {
- return false
- }
- return true
- }
-
- static fromString (dnString) {
- const rdns = parseString(dnString)
- return new DN({ rdns })
- }
- }
- module.exports = DN
|