diff --git a/types/ldapjs/index.d.ts b/types/ldapjs/index.d.ts index 8034add042..b2444ae232 100644 --- a/types/ldapjs/index.d.ts +++ b/types/ldapjs/index.d.ts @@ -52,7 +52,7 @@ export interface ClientOptions { export interface SearchOptions { scope?: string; - filter?: string; + filter?: string | Filter; attributes?: string[]; sizeLimit?: number; timeLimit?: number; @@ -238,3 +238,46 @@ export interface Client extends EventEmitter { } export function createClient(options?: ClientOptions): Client; + +declare class Filter { + matches(obj: any): boolean; + type: string; +} + +export function parseFilter(filterString: string): Filter; + +export class EqualityFilter extends Filter { + constructor(options: { attribute: string, value: string }) +} + +export class PresenceFilter extends Filter { + constructor(options: { attribute: string }) +} + +export class SubstringFilter extends Filter { + constructor(options: { attribute: string, initial: string, any?: string[], final?: string }) +} + +export class GreaterThanEqualsFilter extends Filter { + constructor(options: { attribute: string, value: string }) +} + +export class LessThanEqualsFilter extends Filter { + constructor(options: { attribute: string, value: string }) +} + +export class AndFilter extends Filter { + constructor(options: { filters: Filter[] }) +} + +export class OrFilter extends Filter { + constructor(options: { filters: Filter[] }) +} + +export class NotFilter extends Filter { + constructor(options: { filter: Filter }) +} + +export class ApproximateFilter extends Filter { + constructor(options: { attribute: string, value: string }) +} diff --git a/types/ldapjs/ldapjs-tests.ts b/types/ldapjs/ldapjs-tests.ts index ec106fd29a..51a0edbd77 100644 --- a/types/ldapjs/ldapjs-tests.ts +++ b/types/ldapjs/ldapjs-tests.ts @@ -2,7 +2,7 @@ import ldap = require("ldapjs"); let client = ldap.createClient({ - url: 'ldap://127.0.0.1:1389' + url: 'ldap://127.0.0.1:1389' }); client.bind('cn=root', 'secret', (err: Error): void => { @@ -10,9 +10,9 @@ client.bind('cn=root', 'secret', (err: Error): void => { }); let opts: ldap.SearchOptions = { - filter: '(&(l=Seattle)(email=*@foo.com))', - scope: 'sub', - attributes: ['dn', 'sn', 'cn'] + filter: '(&(l=Seattle)(email=*@foo.com))', + scope: 'sub', + attributes: ['dn', 'sn', 'cn'] }; client.search('o=example', opts, (err: Error, res: NodeJS.EventEmitter): void => { @@ -20,12 +20,89 @@ client.search('o=example', opts, (err: Error, res: NodeJS.EventEmitter): void => }); let change = new ldap.Change({ - operation: 'add', - modification: { - pets: ['cat', 'dog'] - } + operation: 'add', + modification: { + pets: ['cat', 'dog'] + } }); -client.modify('cn=foo, o=example', change, function(err) { - // nothing +client.modify('cn=foo, o=example', change, function (err) { + // nothing }); + + +let f = ldap.parseFilter('(objectclass=*)'); +f.matches({}); + +let equalityFilter = new ldap.EqualityFilter({ + attribute: 'cn', + value: 'foo' +}); +equalityFilter.matches({ cn: 'foo' }); + +let presenceFilter = new ldap.PresenceFilter({ + attribute: 'cn' +}); +presenceFilter.matches({ cn: 'foo' }); + +let substringFilter = new ldap.SubstringFilter({ + attribute: 'cn', + initial: 'foo', + any: ['bar'], + final: 'baz' +}); +substringFilter.matches({ cn: 'foobigbardogbaz' }); + +let greaterThanEqualsFilter = new ldap.GreaterThanEqualsFilter({ + attribute: 'cn', + value: 'foo', +}); +greaterThanEqualsFilter.matches({ cn: 'foobar' }); + +let lessThanEqualsFilter = new ldap.LessThanEqualsFilter({ + attribute: 'cn', + value: 'foo', +}); +lessThanEqualsFilter.matches({ cn: 'abc' }); + +let andFilter = new ldap.AndFilter({ + filters: [ + new ldap.EqualityFilter({ + attribute: 'cn', + value: 'foo' + }), + new ldap.EqualityFilter({ + attribute: 'sn', + value: 'bar' + }) + ] +}); +andFilter.matches({ cn: 'foo', sn: 'bar' }); + +let orFilter = new ldap.OrFilter({ + filters: [ + new ldap.EqualityFilter({ + attribute: 'cn', + value: 'foo' + }), + new ldap.EqualityFilter({ + attribute: 'sn', + value: 'bar' + }) + ] +}); +orFilter.matches({ cn: 'foo', sn: 'baz' }); + +let notFilter = new ldap.NotFilter({ + filter: new ldap.EqualityFilter({ + attribute: 'cn', + value: 'foo' + }) +}); +notFilter.matches({ cn: 'bar' }); + +let approximateFilter = new ldap.ApproximateFilter({ + attribute: 'cn', + value: 'foo' +}); +approximateFilter.matches({ cn: 'foo' });