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_request.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 LDAPResult = require('./result');
  7. var dn = require('../dn');
  8. var filters = require('../filters');
  9. var Protocol = require('../protocol');
  10. ///--- Globals
  11. var Ber = asn1.Ber;
  12. ///--- API
  13. function SearchRequest(options) {
  14. options = options || {};
  15. assert.object(options);
  16. options.protocolOp = Protocol.LDAP_REQ_SEARCH;
  17. LDAPMessage.call(this, options);
  18. if (options.baseObject !== undefined) {
  19. this.baseObject = options.baseObject;
  20. } else {
  21. this.baseObject = dn.parse('');
  22. }
  23. this.scope = options.scope || 'base';
  24. this.derefAliases = options.derefAliases || Protocol.NEVER_DEREF_ALIASES;
  25. this.sizeLimit = options.sizeLimit || 0;
  26. this.timeLimit = options.timeLimit || 0;
  27. this.typesOnly = options.typesOnly || false;
  28. this.filter = options.filter || null;
  29. this.attributes = options.attributes ? options.attributes.slice(0) : [];
  30. }
  31. util.inherits(SearchRequest, LDAPMessage);
  32. Object.defineProperties(SearchRequest.prototype, {
  33. type: {
  34. get: function getType() { return 'SearchRequest'; },
  35. configurable: false
  36. },
  37. _dn: {
  38. get: function getDN() { return this.baseObject; },
  39. configurable: false
  40. },
  41. scope: {
  42. get: function getScope() {
  43. switch (this._scope) {
  44. case Protocol.SCOPE_BASE_OBJECT: return 'base';
  45. case Protocol.SCOPE_ONE_LEVEL: return 'one';
  46. case Protocol.SCOPE_SUBTREE: return 'sub';
  47. default:
  48. throw new Error(this._scope + ' is an invalid search scope');
  49. }
  50. },
  51. set: function setScope(val) {
  52. if (typeof (val) === 'string') {
  53. switch (val) {
  54. case 'base':
  55. this._scope = Protocol.SCOPE_BASE_OBJECT;
  56. break;
  57. case 'one':
  58. this._scope = Protocol.SCOPE_ONE_LEVEL;
  59. break;
  60. case 'sub':
  61. this._scope = Protocol.SCOPE_SUBTREE;
  62. break;
  63. default:
  64. throw new Error(val + ' is an invalid search scope');
  65. }
  66. } else {
  67. this._scope = val;
  68. }
  69. },
  70. configurable: false
  71. }
  72. });
  73. SearchRequest.prototype._parse = function (ber) {
  74. assert.ok(ber);
  75. this.baseObject = ber.readString();
  76. this.scope = ber.readEnumeration();
  77. this.derefAliases = ber.readEnumeration();
  78. this.sizeLimit = ber.readInt();
  79. this.timeLimit = ber.readInt();
  80. this.typesOnly = ber.readBoolean();
  81. this.filter = filters.parse(ber);
  82. // look for attributes
  83. if (ber.peek() === 0x30) {
  84. ber.readSequence();
  85. var end = ber.offset + ber.length;
  86. while (ber.offset < end)
  87. this.attributes.push(ber.readString().toLowerCase());
  88. }
  89. return true;
  90. };
  91. SearchRequest.prototype._toBer = function (ber) {
  92. assert.ok(ber);
  93. ber.writeString(this.baseObject.toString());
  94. ber.writeEnumeration(this._scope);
  95. ber.writeEnumeration(this.derefAliases);
  96. ber.writeInt(this.sizeLimit);
  97. ber.writeInt(this.timeLimit);
  98. ber.writeBoolean(this.typesOnly);
  99. var f = this.filter || new filters.PresenceFilter({attribute: 'objectclass'});
  100. ber = f.toBer(ber);
  101. ber.startSequence(Ber.Sequence | Ber.Constructor);
  102. if (this.attributes && this.attributes.length) {
  103. this.attributes.forEach(function (a) {
  104. ber.writeString(a);
  105. });
  106. }
  107. ber.endSequence();
  108. return ber;
  109. };
  110. SearchRequest.prototype._json = function (j) {
  111. assert.ok(j);
  112. j.baseObject = this.baseObject;
  113. j.scope = this.scope;
  114. j.derefAliases = this.derefAliases;
  115. j.sizeLimit = this.sizeLimit;
  116. j.timeLimit = this.timeLimit;
  117. j.typesOnly = this.typesOnly;
  118. j.filter = this.filter.toString();
  119. j.attributes = this.attributes;
  120. return j;
  121. };
  122. ///--- Exports
  123. module.exports = SearchRequest;