Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

add_request.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
  2. var assert = require('assert-plus');
  3. var util = require('util');
  4. var LDAPMessage = require('./message');
  5. var Attribute = require('../attribute');
  6. var Protocol = require('../protocol');
  7. var lassert = require('../assert');
  8. ///--- API
  9. function AddRequest(options) {
  10. options = options || {};
  11. assert.object(options);
  12. lassert.optionalStringDN(options.entry);
  13. lassert.optionalArrayOfAttribute(options.attributes);
  14. options.protocolOp = Protocol.LDAP_REQ_ADD;
  15. LDAPMessage.call(this, options);
  16. this.entry = options.entry || null;
  17. this.attributes = options.attributes ? options.attributes.slice(0) : [];
  18. }
  19. util.inherits(AddRequest, LDAPMessage);
  20. Object.defineProperties(AddRequest.prototype, {
  21. type: {
  22. get: function getType() { return 'AddRequest'; },
  23. configurable: false
  24. },
  25. _dn: {
  26. get: function getDN() { return this.entry; },
  27. configurable: false
  28. }
  29. });
  30. AddRequest.prototype._parse = function (ber) {
  31. assert.ok(ber);
  32. this.entry = ber.readString();
  33. ber.readSequence();
  34. var end = ber.offset + ber.length;
  35. while (ber.offset < end) {
  36. var a = new Attribute();
  37. a.parse(ber);
  38. a.type = a.type.toLowerCase();
  39. if (a.type === 'objectclass') {
  40. for (var i = 0; i < a.vals.length; i++)
  41. a.vals[i] = a.vals[i].toLowerCase();
  42. }
  43. this.attributes.push(a);
  44. }
  45. this.attributes.sort(Attribute.compare);
  46. return true;
  47. };
  48. AddRequest.prototype._toBer = function (ber) {
  49. assert.ok(ber);
  50. ber.writeString(this.entry.toString());
  51. ber.startSequence();
  52. this.attributes.forEach(function (a) {
  53. a.toBer(ber);
  54. });
  55. ber.endSequence();
  56. return ber;
  57. };
  58. AddRequest.prototype._json = function (j) {
  59. assert.ok(j);
  60. j.entry = this.entry.toString();
  61. j.attributes = [];
  62. this.attributes.forEach(function (a) {
  63. j.attributes.push(a.json);
  64. });
  65. return j;
  66. };
  67. AddRequest.prototype.indexOf = function (attr) {
  68. if (!attr || typeof (attr) !== 'string')
  69. throw new TypeError('attr (string) required');
  70. for (var i = 0; i < this.attributes.length; i++) {
  71. if (this.attributes[i].type === attr)
  72. return i;
  73. }
  74. return -1;
  75. };
  76. AddRequest.prototype.attributeNames = function () {
  77. var attrs = [];
  78. for (var i = 0; i < this.attributes.length; i++)
  79. attrs.push(this.attributes[i].type.toLowerCase());
  80. return attrs;
  81. };
  82. AddRequest.prototype.getAttribute = function (name) {
  83. if (!name || typeof (name) !== 'string')
  84. throw new TypeError('attribute name (string) required');
  85. name = name.toLowerCase();
  86. for (var i = 0; i < this.attributes.length; i++) {
  87. if (this.attributes[i].type === name)
  88. return this.attributes[i];
  89. }
  90. return null;
  91. };
  92. AddRequest.prototype.addAttribute = function (attr) {
  93. if (!(attr instanceof Attribute))
  94. throw new TypeError('attribute (Attribute) required');
  95. return this.attributes.push(attr);
  96. };
  97. /**
  98. * Returns a "pure" JS representation of this object.
  99. *
  100. * An example object would look like:
  101. *
  102. * {
  103. * "dn": "cn=unit, dc=test",
  104. * "attributes": {
  105. * "cn": ["unit", "foo"],
  106. * "objectclass": ["top", "person"]
  107. * }
  108. * }
  109. *
  110. * @return {Object} that looks like the above.
  111. */
  112. AddRequest.prototype.toObject = function () {
  113. var self = this;
  114. var obj = {
  115. dn: self.entry ? self.entry.toString() : '',
  116. attributes: {}
  117. };
  118. if (!this.attributes || !this.attributes.length)
  119. return obj;
  120. this.attributes.forEach(function (a) {
  121. if (!obj.attributes[a.type])
  122. obj.attributes[a.type] = [];
  123. a.vals.forEach(function (v) {
  124. if (obj.attributes[a.type].indexOf(v) === -1)
  125. obj.attributes[a.type].push(v);
  126. });
  127. });
  128. return obj;
  129. };
  130. ///--- Exports
  131. module.exports = AddRequest;