API
Many ECMAScript 6 features are available in scripts, including but not limited to:
- Arrow Functions
- Async Functions
- Block-scoped Variable let
- Block-scoped Variable const
- Template Strings
- Desctructuring (Karma v2.8.10 onwards)
Globals
In each script has the following additional global variables available:
- JSON
- Promise
- _: lodash v2.4.2 with underscore.string v2.4.0
- jsonStableStringify v1.0.1
Sandbox/Context
An object passed as first parameters to all functions. It has the following properties
viewerlogScriptVaultIDVaultconfigformatDN(dn: string | DN): string- normalizes the given DN and returns a stringparseDN(dn: string | DN): DN- parses the given parameter to DNescapeLDAPFilter(value: any): string- escapes the given value to be used in an LDAP filter; the following chars are escaped:*,(,),\\,\\0escapeLDAPFilter('star*') // => 'star\2A'
Viewer
The viewer represent the currently logged in user. It has the following properties:
entryDN: string- the normalized DN$id: string- as configured in theusers.id(defaults toentryUUID)$type: string- usually'user'$name: string- as configured in theusers.name(defaults tocn)$description: string- as configured in theusers.description(defaults todisplayNameorfullName)$role: string- the karma role; one ofguest,user,adminhas(dn: string | DN): boolean- determines if the viewer has this principal (dn, parent container, role or group)is(dnOrKarmaRole | DN): boolean- determines if the viewer has at least this karma role (admin, user or guest) or this principal (viewer dn, parent container, role or group)$role is('admin') is('user') is('guest') admin true true true user false true true guest false false true $principals: string[]Note: Use
is(dn)instead of$principalsas that method normalizes the passed DN before looking for it in this array.There are the following principals:
- on any object type: all parent containers of the object
- on a user:
groupMembership nrfDynamicGroupMembership nrfMemberOf - on a role:
nrfChildRoles nrfExternalChildRoles
$partition: string- the partition of the viewer (only if partitions are enabled)$primaryPartition: string- the primary partition of the viewer (only if partitions are enabled)
ScriptVault
logconfigimport(scriptLocation: string): any- load and parse/execute the given script; ifscriptLocationstarts withfile:the script is located relative to config dir (it may be a javascript, json or json5 file) otherwise it is expected to be an DN of an object which has aDirXML-Dataattribute that is used as the scriptrunScript: (scriptLocation, context: any = Sandbox): any- executes themodule.exportsfunction withcontext(defaults to a fresh Sandbox passed as first parameter and returns its result;scriptLocationis same as inimport()abovelocalizeAttributes: (entry: object, attrs: any): object-const entry = { siteLocation: 'en~1st Floor|de~1.OG' } const result = ScriptVault.localizeAttributes(entry, { siteLocation: 'localizedSiteLocation' }) // -> result === entry // -> entry.localizedSiteLocation === '1.OG' // -> entry.siteLocation === undefined ScriptVault.localizeAttributes(entry, { siteLocation: '=' }) ScriptVault.localizeAttributes(entry, ['siteLocation']) // -> entry.siteLocation === '1.OG'
IDVault
get(dn [, ignored], attribute_key: string | string[]): Promise<any>- loads a LDAP entry by its DNdn: string | DN- of entry to loadignored- (optional)attribute_key: string | string[]- either a list of attributes (an object with these attributes is returned) or single attribute as string (only that attribute is returned)
globalQuery(ignored, dalKey, dalOptions): Promise<any>- seedal(key, options)dal(key, options): Promise<any>- executes the given (key) dal querysearch(baseDN: string | DN [, options: Options]): Promise<any[]>- find several LDAP entriesbaseDN: string | DN- the LDAP search baseoptions: Options- (optional) a config object with the following properties (all optional)scope: 'base' | 'one' | 'sub' = 'sub'filter: string = '(objectClass=*)'attributes: string[] = ['1.1']- unlessattributeis definedattribute: string- =>attributes = [attribute]-> result is an array with only the attribute value for each entrysort: string = undefined- like'cn'(same as'+cn') or'-modifiedTimestamp'skip: number = 0take: number = undefined
Log
Config
An object to access the application configuration. It has the following methods:
get(key: string): any- returns the given config value (likeusers.base)Note: This methods throws an error if the given key is not defined! Guard the access with
config.exists(key).const value = (config.exists(key) && config.get(key)) || 'fallback value'exists(key: string): boolean- determines if the given config key exists