modify-request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict'
  2. const { operations } = require('@ldapjs/protocol')
  3. const Change = require('@ldapjs/change')
  4. const LdapMessage = require('../ldap-message')
  5. /**
  6. * Implements the MODIFY request message as described in
  7. * https://www.rfc-editor.org/rfc/rfc4511.html#section-4.6.
  8. *
  9. * Changes should be in the order of operation as described in
  10. * the spec. If sorting is desired, sort the array prior to
  11. * adding it to the request.
  12. *
  13. * @example <caption>Sorting Changes</caption>
  14. * const {ModifyRequest} = require('@ldapjs/messages')
  15. * const Change = require('@ldapjs/change')
  16. * const changes = someArrayOfChanges.sort(Change.sort)
  17. * const req = new ModifyRequest({
  18. * object: 'dn=foo,dc=example,dc=com',
  19. * changes
  20. * })
  21. */
  22. class ModifyRequest extends LdapMessage {
  23. #object
  24. #changes
  25. /**
  26. * @typedef {LdapMessageOptions} ModifyRequestOptions
  27. * @property {string|null} [object] The LDAP object (DN) to modify.
  28. * @property {import('@ldapjs/change')[]} [changes] The set of changes to
  29. * apply.
  30. */
  31. /**
  32. * @param {ModifyRequestOptions} [options]
  33. */
  34. constructor (options = {}) {
  35. options.protocolOp = operations.LDAP_REQ_MODIFY
  36. super(options)
  37. this.#object = options.object || null
  38. this.changes = options.changes || []
  39. }
  40. /**
  41. * A copy of the set of changes to be applied to the LDAP object.
  42. *
  43. * @returns {import('@ldapjs/change')[]}
  44. */
  45. get changes () {
  46. return this.#changes.slice(0)
  47. }
  48. /**
  49. * Define the set of changes to apply to the LDAP object.
  50. *
  51. * @param {import('@ldapjs/change')[]} values
  52. *
  53. * @throws When `values` is not an array or contains any elements that
  54. * are not changes.
  55. */
  56. set changes (values) {
  57. this.#changes = []
  58. if (Array.isArray(values) === false) {
  59. throw Error('changes must be an array')
  60. }
  61. for (let change of values) {
  62. if (Change.isChange(change) === false) {
  63. throw Error('change must be an instance of Change or a Change-like object')
  64. }
  65. if (Object.prototype.toString.call(change) !== '[object LdapChange]') {
  66. change = new Change(change)
  67. }
  68. this.#changes.push(change)
  69. }
  70. }
  71. /**
  72. * The object (DN) to be modified.
  73. *
  74. * @returns {string}
  75. */
  76. get object () {
  77. return this.#object
  78. }
  79. /**
  80. * Define the object (DN) to be modified.
  81. *
  82. * @param {string} value
  83. */
  84. set object (value) {
  85. this.#object = value
  86. }
  87. /**
  88. * The name of the request type.
  89. *
  90. * @type {string}
  91. */
  92. get type () {
  93. return 'ModifyRequest'
  94. }
  95. get _dn () {
  96. return this.#object
  97. }
  98. /**
  99. * Internal use only.
  100. *
  101. * @param {import('@ldapjs/asn1').BerWriter} ber
  102. *
  103. * @returns {import('@ldapjs/asn1').BerWriter}
  104. */
  105. _toBer (ber) {
  106. ber.startSequence(operations.LDAP_REQ_MODIFY)
  107. ber.writeString(this.#object.toString())
  108. ber.startSequence()
  109. for (const change of this.#changes) {
  110. ber.appendBuffer(change.toBer().buffer)
  111. }
  112. ber.endSequence()
  113. ber.endSequence()
  114. return ber
  115. }
  116. /**
  117. * Internal use only.
  118. *
  119. * @param {object}
  120. *
  121. * @returns {object}
  122. */
  123. _pojo (obj = {}) {
  124. obj.object = this.#object
  125. obj.changes = this.#changes.map(c => c.pojo)
  126. return obj
  127. }
  128. /**
  129. * Implements the standardized `parseToPojo` method.
  130. *
  131. * @see LdapMessage.parseToPojo
  132. *
  133. * @param {import('@ldapjs/asn1').BerReader} ber
  134. */
  135. static parseToPojo (ber) {
  136. const protocolOp = ber.readSequence()
  137. if (protocolOp !== operations.LDAP_REQ_MODIFY) {
  138. const op = protocolOp.toString(16).padStart(2, '0')
  139. throw Error(`found wrong protocol operation: 0x${op}`)
  140. }
  141. const object = ber.readString()
  142. const changes = []
  143. ber.readSequence()
  144. const end = ber.offset + ber.length
  145. while (ber.offset < end) {
  146. const change = Change.fromBer(ber)
  147. changes.push(change.pojo)
  148. }
  149. return { protocolOp, object, changes }
  150. }
  151. }
  152. module.exports = ModifyRequest