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.

search_entry.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
  2. var assert = require('assert-plus');
  3. var util = require('util');
  4. var asn1 = require('asn1');
  5. var LDAPMessage = require('./message');
  6. var Attribute = require('../attribute');
  7. var Protocol = require('../protocol');
  8. var lassert = require('../assert');
  9. ///--- Globals
  10. var BerWriter = asn1.BerWriter;
  11. ///--- API
  12. function SearchEntry(options) {
  13. options = options || {};
  14. assert.object(options);
  15. lassert.optionalStringDN(options.objectName);
  16. options.protocolOp = Protocol.LDAP_REP_SEARCH_ENTRY;
  17. LDAPMessage.call(this, options);
  18. this.objectName = options.objectName || null;
  19. this.setAttributes(options.attributes || []);
  20. }
  21. util.inherits(SearchEntry, LDAPMessage);
  22. Object.defineProperties(SearchEntry.prototype, {
  23. type: {
  24. get: function getType() { return 'SearchEntry'; },
  25. configurable: false
  26. },
  27. _dn: {
  28. get: function getDN() { return this.objectName; },
  29. configurable: false
  30. },
  31. object: {
  32. get: function getObject() {
  33. var obj = {
  34. dn: this.dn.toString(),
  35. controls: []
  36. };
  37. this.attributes.forEach(function (a) {
  38. if (a.vals && a.vals.length) {
  39. if (a.vals.length > 1) {
  40. obj[a.type] = a.vals.slice();
  41. } else {
  42. obj[a.type] = a.vals[0];
  43. }
  44. } else {
  45. obj[a.type] = [];
  46. }
  47. });
  48. this.controls.forEach(function (element, index, array) {
  49. obj.controls.push(element.json);
  50. });
  51. return obj;
  52. },
  53. configurable: false
  54. },
  55. raw: {
  56. get: function getRaw() {
  57. var obj = {
  58. dn: this.dn.toString(),
  59. controls: []
  60. };
  61. this.attributes.forEach(function (a) {
  62. if (a.buffers && a.buffers.length) {
  63. if (a.buffers.length > 1) {
  64. obj[a.type] = a.buffers.slice();
  65. } else {
  66. obj[a.type] = a.buffers[0];
  67. }
  68. } else {
  69. obj[a.type] = [];
  70. }
  71. });
  72. this.controls.forEach(function (element, index, array) {
  73. obj.controls.push(element.json);
  74. });
  75. return obj;
  76. },
  77. configurable: false
  78. }
  79. });
  80. SearchEntry.prototype.addAttribute = function (attr) {
  81. if (!attr || typeof (attr) !== 'object')
  82. throw new TypeError('attr (attribute) required');
  83. this.attributes.push(attr);
  84. };
  85. SearchEntry.prototype.toObject = function () {
  86. return this.object;
  87. };
  88. SearchEntry.prototype.fromObject = function (obj) {
  89. if (typeof (obj) !== 'object')
  90. throw new TypeError('object required');
  91. var self = this;
  92. if (obj.controls)
  93. this.controls = obj.controls;
  94. if (obj.attributes)
  95. obj = obj.attributes;
  96. this.attributes = [];
  97. Object.keys(obj).forEach(function (k) {
  98. self.attributes.push(new Attribute({type: k, vals: obj[k]}));
  99. });
  100. return true;
  101. };
  102. SearchEntry.prototype.setAttributes = function (obj) {
  103. if (typeof (obj) !== 'object')
  104. throw new TypeError('object required');
  105. if (Array.isArray(obj)) {
  106. obj.forEach(function (a) {
  107. if (!Attribute.isAttribute(a))
  108. throw new TypeError('entry must be an Array of Attributes');
  109. });
  110. this.attributes = obj;
  111. } else {
  112. var self = this;
  113. self.attributes = [];
  114. Object.keys(obj).forEach(function (k) {
  115. var attr = new Attribute({type: k});
  116. if (Array.isArray(obj[k])) {
  117. obj[k].forEach(function (v) {
  118. attr.addValue(v.toString());
  119. });
  120. } else {
  121. attr.addValue(obj[k].toString());
  122. }
  123. self.attributes.push(attr);
  124. });
  125. }
  126. };
  127. SearchEntry.prototype._json = function (j) {
  128. assert.ok(j);
  129. j.objectName = this.objectName.toString();
  130. j.attributes = [];
  131. this.attributes.forEach(function (a) {
  132. j.attributes.push(a.json || a);
  133. });
  134. return j;
  135. };
  136. SearchEntry.prototype._parse = function (ber) {
  137. assert.ok(ber);
  138. this.objectName = ber.readString();
  139. assert.ok(ber.readSequence());
  140. var end = ber.offset + ber.length;
  141. while (ber.offset < end) {
  142. var a = new Attribute();
  143. a.parse(ber);
  144. this.attributes.push(a);
  145. }
  146. return true;
  147. };
  148. SearchEntry.prototype._toBer = function (ber) {
  149. assert.ok(ber);
  150. ber.writeString(this.objectName.toString());
  151. ber.startSequence();
  152. this.attributes.forEach(function (a) {
  153. // This may or may not be an attribute
  154. ber = Attribute.toBer(a, ber);
  155. });
  156. ber.endSequence();
  157. return ber;
  158. };
  159. ///--- Exports
  160. module.exports = SearchEntry;