Node library

Contents

This API provides functions to get, query and manipulate nodes.

Do not use lib-node against the CMS (content) repository. Content is layered on top of nodes with content-type validation, draft/master synchronization, publish-state bookkeeping, audit-log entries, child-project propagation, and indexing of system fields. Direct lib-node writes against a content node bypass all of that and corrupt the content model.

Use lib-content for content. Reach for lib-node only when working with custom (non-content) repositories. See Editors for the pattern.

Usage

Add the following to your build.gradle file:

dependencies {
  include xplibs.node
}

Add the import statement to your code:

import nodeLib from '/lib/xp/node';

You are now ready to use the API.

Events

Usage of this API produces the following events:

Table 1. Distributed events
Event Occurs when

node.created

a node is created

node.deleted

a node is deleted

node.pushed

a node is pushed to a different branch

node.duplicated

a node is duplicated

node.updated

a node is updated

node.moved

a node is moved in the tree structure

node.renamed

a node is renamed

node.sorted

a node is sorted

node.stateUpdated

a node state is updated

For information on events, check out the Event library

Functions

connect

Creates a connection to a repository with a given branch and authentication info.

Parameters

Name Kind Details

params

Source

object with parameters

Returns

RepoConnection : A new connection to the repository

Example

Connect to repo 'my-repo', branch 'master'.
import {connect} from '/lib/xp/node';

const connection = connect({
    repoId: 'my-repo',
    branch: 'master'
});

multiRepoConnect

Creates a connection to several repositories with a given branch and authentication info.

Parameters

Name Kind Details

sources

Source[]

array of repository source objects

Returns

MultiRepoConnection : A new connection object for all connected repositories.

Example

Connect to repos 'myRepo' and 'my-other-repo', both on branch 'master'.
import {multiRepoConnect} from '/lib/xp/node';

const searchConnection = multiRepoConnect({
    sources: [
        {
            repoId: 'my-repo',
            branch: 'myBranch',
            principals: ["role:system.admin"]
        },
        {
            repoId: 'my-other-repo',
            branch: 'master',
            principals: ["role:system.admin"]
        }
    ]
});

MultiRepoConnection

A MultiRepoConnection makes it possible to search across multiple repositories. The object has only one method: query.

query

Queries nodes in a multi-repo connection.

Parameters

An object with the following keys and their values:

Name Type Attributes Details

start

number

optional

Start index used for paging - default: 0

count

number

optional

Number of content to fetch, used for paging - default: 10

query

string/object

Query string or DSL expression.

filters

object

optional

Query filters

sort

string

optional

Sorting expression - default: '_score DESC'

aggregations

object

optional

Aggregations config

highlight

object

optional

Highlighting config

explain

boolean

optional

If set to true, score calculation explanation will be included in result.

Returns

object : Result of query

Example

Query multi-repo connection
const result = multiRepoConnection.query({
    start: 0,
    count: 2,
    query: "startTime > instant('2016-10-11T14:38:54.454Z')",
    filters: {
        boolean: {
            must: {
                exists: {
                    field: "modifiedTime"
                }
            },
            mustNot: {
                hasValue: {
                    field: "myField",
                    values: [
                        "cheese",
                        "fish",
                        "onion"
                    ]
                }
            }
        },
        notExists: {
            field: "unwantedField"
        },
        ids: {
            values: ["id1", "id2"]
        }
    }
});
Sample response
const expected = {
    total: 12902,
    count: 2,
    hits: [
        {
            id: "b186d24f-ac38-42ca-a6db-1c1bda6c6c26",
            score: 1.2300000190734863,
            repoId: "my-repo",
            branch: "master"
        },
        {
            id: "350ba4a6-589c-498b-8af0-f183850e1120",
            score: 1.399999976158142,
            repoId: "com.enonic.cms.default",
            branch: "draft"
        }
    ]
}

RepoConnection

A single-repo connection with various methods for working in a single repo.

All the examples below are using a repo connection to execute the functions. The repo connection can be created like this:

Connecting to a repo.
import {connect} from '/lib/xp/node';

const repo = connect({
    repoId: "com.enonic.cms.default",
    branch: "master"
});

commit

Commits the active version of nodes.

Parameters

Name Kind Details

keys

string | string[]

Node keys to commit. Each argument could be an id, a path or an array of the two. Prefer the usage of ID rather than paths.

message

string

Optional commit message

Returns

object : Commit object. A single commit is created and applied to all the nodes referenced in keys.

Example

Committing a node.
const result1 = repo.commit({keys: 'nodeId'});
Sample response
const expected = {
    id: "aa1f76bf-4bb9-41be-b166-03561c1555b2",
    message: "",
    committer: "user:system:anonymous",
    timestamp: "2019-01-24T15:16:36.260799Z"
}
Committing nodes.
const result2 = repo.commit({
    keys: ['nodeId', 'nodeId2'],
    message: 'Commit message'
});
Sample response
const expected = {
    id: "aa1f76bf-4bb9-41be-b166-03561c1555b2",
    message: "Commit message",
    committer: "user:system:anonymous",
    timestamp: "2019-01-24T15:19:30.818029Z"
}

create

Creates a node. To create a content where the name is not important and there could be multiple instances under the same parent content, skip the name parameter and specify a displayName.

Parameters

An object with the following keys and their values:

Name Type Attributes Details

_name

string

<optional>

Name of content.

_parentPath

string

<optional>

Path to place content under.

_indexConfig

object

<optional>

How the document should be indexed. A default template value "byType" will be set if no value specified.

_permissions

object

<optional>

The access control list for the node. By default the creator will have full access

_inheritsPermissions

boolean

<optional>

  Set to true if the permissions should be inherited from the node parent. This is a meta-property and is not persisted. Default is false.

_manualOrderValue

number

<optional>

 Value used to order document when ordering by parent and child-order is set to manual

_childOrder

string

<optional>

Default ordering of children when doing getChildren if no order is given in query

Returns

object : Created object

Example

Create a node on a repo connection:
const result1 = repo.create({
    _name: 'nerd',
    displayName: 'Carpenter and IT expert',
    likes: 'Plywood',
    numberOfUselessGadgets: 123
});
Sample response
const expected = {
  _id: "2f0d5c09-39fe-4746-9754-74491d75a185",
  _name: "nerd",
  _path: "/nerd",
  _childOrder: "_ts DESC",
  _indexConfig: {
    default: {
      decideByType: true,
      enabled: true,
      nGram: false,
      fulltext: false,
      includeInAllText: false,
      path: false,
      indexValueProcessors: [],
      languages: []
    },
    configs: []
  },
  _permissions: [
    {
      principal: "role:system.admin",
      allow: [
        "READ",
        "CREATE",
        "MODIFY",
        "DELETE",
        "PUBLISH",
        "READ_PERMISSIONS",
        "WRITE_PERMISSIONS"
      ],
      deny: []
    },
    {
      principal: "role:system.everyone",
      allow: [
        "READ"
      ],
      deny: []
    }
  ],
  _state: "DEFAULT",
  _nodeType: "default",
  _versionKey: "aa95ae7e-b975-4879-a19c-cce3b1fc3742",
  _ts: "2021-05-15T08:46:37.227Z",
  displayName: "Carpenter and IT expert",
  likes: "Plywood",
  numberOfUselessGadgets: 123
}
Create node with stemmed indices for specific languages

Setting the languages in the node config will create stemmed indices for all mapped properties.

Use stemmed() function to query data based on these indices.
repo.create({
    _name: "fruits",
    displayName: "Fruit basket",
    description: "language indices usage example",
    english_set: {
      fruit_a: "Apple",
      fruit_set: {
            fruit_b: "Lemon",
            fruit_c: "Orange"
        }
    },
    norwegian_set: {
        fruit_a: "Eple",
        fruit_set: {
            fruit_b: "Sitron",
            fruit_c: "Oransje"
        }
    },
    _indexConfig: {
        default: {
            enabled: true,
            decideByType: true,
            nGram: false,
            fulltext: false,
            includeInAllText: false,
            path: false,
            languages: ['en']
        }, configs: [{
            path: "norwegian_set.**",
            config: {
                enabled: true,
                decideByType: false,
                nGram: true,
                fulltext: true,
                includeInAllText: true,
                path: false,
                languages: ['no']
            }
        }]
    }
});

Stemmed english indices will be generated for displayName, description and all strings inside english_set. Norwegian indices will be created for strings inside norwegian_set only.

delete

Deletes a node or nodes.

Parameters

Name Kind Details

keys

string | string[]

Node keys to delete. Each argument could be an id, a path or an array of the two. Prefer the usage of ID rather than paths.

Returns

string[] : The list of keys that were actually deleted.

Example

Deleting nodes
const result = repo.delete('nodeId', '/node2-path', 'missingNodeId');
Sample response
[
    "7b175b63-b012-4f20-b31d-b8f78420d7f1",
    "69f225a1-e775-4845-89f0-29b808a9a659"
]

diff

Resolves the differences for a node between the branch in the current context and a target branch.

Parameters

An object with the following keys and their values:

Name Type Attributes Details

key

string

Path or id to resolve diff for

target

string

Branch to differentiate with

includeChildren

boolean

optional

If set to true, differences are resolved for all children.

Returns

object : An array with differences in the status for each node in the tree that has differences between the branches.

Example

Comparing draft and master branch
const result = repo.diff({
    key: '/my-name',
    target: 'draft',
    includeChildren: true
});
Sample response
{
    diff: [
        {
            id: "c8121875-a9b7-42e4-acf2-75cb52687ddb", (1)
            status: "NEWER"
        }
    ]
}
1 There are 2 children, but only one has been changed

duplicate

Duplicates a node.

Parameters

An object with the following keys and their values:

Name Kind Attributes Default Details

nodeId

string

Id to the node.

name

string

<optional>

New node name.

includeChildren

boolean

<optional>

true

Indicates that children nodes must be duplicated, too.

parent

string

<optional>

Destination parent path. By default, a duplicated node will be added as a sibling of the source node.

refresh

string

<optional>

Refresh the index for the current repoConnection.

dataProcessor

function

<optional>

Node data processor.

Returns duplicated node.

object : Duplicated node

Example

Duplicate a node
const result = repo.duplicate({
    nodeId: 'nodeId',
    name: 'duplicated-node',
    includeChildren: false,
    dataProcessor: (originalData) => {
        originalData.data.extraProp = 'extraPropValue';
        return originalData;
    },
    parent: '/',
    refresh: 'SEARCH'
});

exists

Checks if a node exists in the current context.

Parameters

Name Kind Details

key

string

Path or id to the node

Returns

boolean : true if exists, false otherwise

Examples

Check for existing node by path
// Checking if a node exists by path
const result = repo.exists('/path/to/mynode');

if (result) {
    log.info('Node exists');
} else {
    log.info('Node does not exist');
}
Check for existing node by id
// Checking if a node exists by id
const result = repo.exists('77963d9e-b3c8-4a53-85a9-20c375a92742');

if (result) {
    log.info('Node exists');
} else {
    log.info('Node does not exist');
}

findChildren

Fetches children of a node.

Parameters

An object with the following keys and their values:

Name Type Attributes Details

parentKey

string

Path or ID of parent to get children of

start

number

optional

start index used for paging - default: 0

count

number

optional

number of content to fetch, used for paging - default: 10

childOrder

string

optional

How to order the children - default is value stored on parent

countOnly

boolean

optional

Optimize for count children only - default is false

recursive

boolean

optional

Do recursive fetching of all children of children - default is false

Returns

object : An object with stats about the result and an array with all IDs of the children.

Example

Find child nodes under a given parent path
const result = repo.findChildren({
    parentKey: "/my-name"
});
Sample response
const expected = {
    total: 2,
    count: 10,
    hits: [
        {
            id: "c8121875-a9b7-42e4-acf2-75cb52687ddb"
        },
        {
            id: "636a616b-c8e4-480c-afa9-be88a737cf52"
        }
    ]
}

getVersions

Returns versions of a node.

Parameters

An object with the following keys and their values:

Name Type Attributes Details

key

string

Path or ID of a node to get versions of

cursor

string

optional

Opaque cursor returned in a previous result, used to fetch the next page

count

number

optional

Number of versions to fetch - default: 10

Returns

object : An object with stats about the result, the cursor for the next page (if any), and an array listing the versions.

Example

Fetch the first page of versions, then the next page using the returned cursor.
const firstPage = repo.getVersions({
    key: '/my-name',
    count: 2
});

if (firstPage.cursor) {
    const nextPage = repo.getVersions({
        key: '/my-name',
        count: 2,
        cursor: firstPage.cursor
    });
}
Sample response
const expected = {
    total: 40,
    count: 2,
    cursor: "eyJ0cyI6MTAwMDAwMCwiaWQiOiJub2RlVmVyc2lvbk9sZCJ9",
    hits: [
        {
            versionId: "72dcd8c1-9d70-444d-ad7a-508d8c1865a0",
            nodeId: "ddd1c933-0725-45e8-9a54-a3dd87193f60",
            nodePath: "/my-name",
            timestamp: "2019-05-13T15:56:17.018Z"
        },
        {
            versionId: "04c6b8d7-3c56-458f-80c9-c6ee551fc21d",
            nodeId: "ddd1c933-0725-45e8-9a54-a3dd87193f60",
            nodePath: "/my-name",
            timestamp: "2019-05-13T15:56:16.995Z"
        }
    ]
}

get

Fetches a specific node or nodes by path or ID.

Parameters

Name Kind Details

params

string | string[]|object

One or multiple strings, or one or multiple arrays of strings, or an object with the following properties:

Properties
Name Type Attributes Default Description

key

string

Node id or path

versionId

string

<optional>

Specific version to get the node from (default is active version)

Returns

object : The node or node array (as JSON) fetched from the repository.

Example

Fetch a single node using id as a string.
const node = repo.get('nodeId');
Fetch several nodes using a sequence of nodeIds.
const nodes = repo.get('nodeId_1', 'nodeId_2');
Fetch several nodes using an array of nodeIds.
const nodes = repo.get(['nodeId_1', 'nodeId_2']);
Fetch several nodes using mixed parameters.
const nodes = repo.get([{
    key: 'nodeId',
    versionId: 'versionKey'
}, {
    key: '/my-name'
}, 'nodeId']);

getActiveVersion

Returns the active version of a node in the current context branch.

Parameters

An object with the following key and value:

Name Kind Details

key

string

Path or ID of the node

Returns

object : The active node version, or null if the node was not found.

Example

Fetch the active version of a node.
const version = repo.getActiveVersion({
    key: '/my-name'
});
Sample response
const expected = {
    versionId: "72dcd8c1-9d70-444d-ad7a-508d8c1865a0",
    nodeId: "ddd1c933-0725-45e8-9a54-a3dd87193f60",
    nodePath: "/my-name",
    timestamp: "2019-05-13T15:56:17.018Z"
}

getBinary

Returns a binary stream.

Parameters

An object with the following keys and their values:

Name Kind Details

key

string

Path or ID of the node

binaryReference

string

to the binary

Returns

\* : Stream of the binary

Example

Fetching the binary of a node.
const binaryStream = repo.getBinary({
    key: "/myNode",
    binaryReference: "myBinaryReference"
});

getCommit

Returns a node version commit.

Parameters

An object with the following keys and their value:

Name Kind Details

id

string

ID of the commit

Returns

object : Commit object

Name Kind Details

id

string

Commit ID

message

string

Optional commit message

committer

string

Principal key of commit creator

timestamp

string

Time of creation

Example

Fetching a commit version of a node.
const commit = repo.getCommit({
    id: "aa1f76bf-4bb9-41be-b166-03561c1555b2"
});
Sample response
const expected = {
    id: "aa1f76bf-4bb9-41be-b166-03561c1555b2",
    message: "",
    committer: "user:system:anonymous",
    timestamp: "2019-01-24T15:16:36.260799Z"
}

update

Updates a node.

Parameters

An object with the following keys and their values:

Name Kind Details

key

string

Path or ID of the node

editor

function

Editor callback function

Returns

object : The modified node

Example

Updating a node.
const result = repo.update({
    key: key,
    editor: (node) => {
        node.someData.cars.push('peugeot');
        return node;
    }
});

patch

Applies a patch to a node.

This is a low-level function. It should be used with caution as it can corrupt data if used incorrectly.

Parameters

An object with the following keys and their values:

Name Type Attributes Default Details

key

string

Path or id to the node

editor

function

Patcher callback function

branches

string[]

<optional>

[]

A list of branches to patch.
* If not provided or empty, the node in the current context branch will be patched.
* If one branch is listed, the patch occurs in that specific branch.
* If multiple branches are listed:
1. The patch is applied to the first branch in the list.
2. For each subsequent branch in the list:
* If its node version is identical to the original (pre-patch) version from any processed branch, it is simply switched to point to the new, patched version. (publish permission is required)
* If its node version is different, the patch is applied to it separately (update permission is required).

Returns

object : Patch result, with one entry in branchResults per branch where the node was patched.

Patchable Fields:

Name Type Description

displayName

string

The display name of the node.

_nodeType

string

The type definition of the node.

_indexConfig

object

An object defining the search index configuration for this node.

_childOrder

string

Defines the default sort order for the node’s direct children.

_manualOrderValue

number

Numeric value used for manual sorting among siblings (when parent’s _childOrder is manual).

Any data can also be patched if the name of the field does not start with an underscore (_).

Examples

Patching a node’s display name and adding a new field.
const patchedNode = repo.patch({
    key: '/my-node',
    editor: (node) => {
        node.displayName = 'New Display Name';
        node.newField = 'New Value';
        return node;
    }
});
Patching childOrder in multiple branches.
const patchedNode = repo.patch({
    key: '/my-node',
    branches: ['master', 'draft', 'other'],
    editor: (node) => {
        node._childOrder = 'field DESC';
        return node;
    }
});
Patching a node with data stream.
var ioLib = require('/lib/xp/io');

var stream = ioLib.newStream('Hello World');

const patchedNode = repo.patch({
    key: '/my-node',
    editor: (node) => {
        node.attachmentField = {
            data: stream
        };
        return node;
    }
});

move

Renames a node or moves it to a new path.

Parameters

An object with the following keys and their values:

Name Kind Details

source

string

Path or id of the node to be moved or renamed

target

string

New path or name for the node. If the target ends in slash '/', it specifies the parent path where to be moved. Otherwise it means the new desired path or name for the node.

Returns

object : The moved or renamed node.

Examples

Changing the name of a node.
const moved = repo.move({
    source: '/my-name',
    target: 'new-name'
});
Moving a node to a new location.
const moved = repo.move({
    source: '/my-name',
    target: '/new-parent/'
});
Move and rename a node in the same operation.
const moved = repo.move({
    source: '/my-name',
    target: '/content/my-site/folder/new-name'
});

push

Pushes a node to a given branch.

Parameters

An object with the following keys and their values:

Name Kind Details

key

string

Id or path to the nodes

keys

string[]

Array of ids or paths to the nodes

target

string

Branch to push to

includeChildren

boolean

Also push children of given nodes. Default is false.

resolve

boolean

Resolve dependencies before pushing, meaning that references will also be pushed. Default is true.

exclude

string[]

Optional array of ids or paths to nodes not to be pushed. If using this, be aware that nodes need to maintain data integrity (e.g parents must be present in target). If data integrity is not maintained with excluded nodes, they will be pushed anyway.

Returns

object : PushNodeResult

Examples

Pushing nodes from current branch.
const result = repo.push({
    keys: ['a'],
    target: 'otherBranch',
    resolve: false
});
Sample response
const expected = {
    success: [
        "a"
    ],
    failed: []
}
Pushing content with children where not all children are allowed.
const result = repo.push({
    keys: ['a'],
    target: 'otherBranch',
    resolve: true,
    includeChildren: true
});
Sample response
const expected = {
    success: [
        "a",
        "b",
        "c"
    ],
    failed: [
        {
            id: "d",
            reason: "ACCESS_DENIED"
        }
    ]
}
Pushing node with children, but excluding some of them.
const result = repo.push({
    keys: ['/a'],
    target: 'otherBranch',
    resolve: true,
    includeChildren: true,
    exclude: ['/a/b', '/a/c']
});
Sample response
const expected = {
    success: [
        "a",
        "d"
    ],
    failed: []
}

query

Queries nodes

Parameters

An object with the following keys and their values:

Name Type Attributes Details

start

number

optional

Start index used for paging - default: 0

count

number

optional

Number of content to fetch, used for paging - default: 10

query

string/object

Query string or DSL expression.

filters

object

optional

Query filters

sort

string/object

optional

Sorting string or DSL expression. Default: '_score DESC'

aggregations

object

optional

Aggregations config

highlight

object

optional

Highlighting config

explain

boolean

optional

If set to true, score calculation explanation will be included in result.

Returns

object : stats, hits and if requested, aggregations

Examples

A simple query using the fulltext function
const result = repo.query({
    query: "fulltext('_name', '_name')"
});
Sample response
const expected = {
    total: 4,
    count: 4,
    hits: [
        {
            id: "5be12847-cbad-4e61-a36c-e94ffb597afb",
            score: 0.7071067690849304
        },
        {
            id: "3928476f-915d-4a6a-97cc-f1c8a905f71a",
            score: 0.625
        },
        {
            id: "a2e38aab-6efb-4f20-862c-e23ef24b3fc3",
            score: 0.5
        },
        {
            id: "3a842eff-ef49-4781-8c1a-dcfcf515db51",
            score: 0.5
        }
    ],
    aggregations: {}
}
Query content using aggregations.
const result = repo.query({
    start: 0,
    count: 2,
    query: "startTime > instant('2016-10-11T14:38:54.454Z')",
    filters: {
        boolean: {
            must: [
                {
                    exists: {
                        field: "modifiedTime"
                    }
                },
                {
                    exists: {
                        field: "other"
                    }
                }
            ],
            mustNot: {
                hasValue: {
                    field: "myField",
                    values: [
                        "cheese",
                        "fish",
                        "onion"
                    ]
                }
            }
        },
        notExists: {
            field: "unwantedField"
        },
        ids: {
            values: ["id1", "id2"]
        }
    },
    sort: "duration DESC",
    aggregations: {
        urls: {
            terms: {
                field: "url",
                order: "_count desc",
                size: 2
            },
            aggregations: {
                duration: {
                    histogram: {
                        field: "duration",
                        interval: 100,
                        minDocCount: 1,
                        extendedBoundMin: 0,
                        extendedBoundMax: 10000,
                        order: "_key desc"
                    }
                },
                durationStats: {
                    stats: {
                        field: "duration"
                    }
                }
            }
        }
    }
});
Sample response
const expected = {
    total: 12902,
    count: 2,
    hits: [
        {
            id: "b186d24f-ac38-42ca-a6db-1c1bda6c6c26",
            score: 1.2300000190734863
        },
        {
            id: "350ba4a6-589c-498b-8af0-f183850e1120",
            score: 1.399999976158142
        }
    ],
    aggregations: {
        urls: {
            buckets: [
                {
                    key: "/site/draft/superhero/search",
                    docCount: 6762,
                    duration: {
                        buckets: [
                            {
                                key: "1600",
                                docCount: 2
                            },
                            {
                                key: "1400",
                                docCount: 1
                            },
                            {
                                key: "1300",
                                docCount: 5
                            }
                        ]
                    }
                },
                {
                    key: "/site/draft/superhero",
                    docCount: 1245,
                    duration: {
                        buckets: [
                            {
                                key: "1600",
                                docCount: 2
                            },
                            {
                                key: "1400",
                                docCount: 1
                            },
                            {
                                key: "1300",
                                docCount: 5
                            }
                        ]
                    }
                }
            ]
        }
    }
}
Query node using stemmed DSL function for specific language.
const result = repo.query({
    query: {
        stemmed: {
            field: "description",
            value: "running",
            language: "en"
        }
    }
});
Sample response
const expected = {
    total: 1,
    count: 1,
    hits: [
        {
            id: "2f0d5c09-39fe-4746-9756-74491d75a185",
            score: 0.5
        }
    ],
    aggregations: {}
}

refresh

Refreshes indices for the current repoConnection. The index has two parts: search and storage. It is possible to index both or just one of them.

Parameters

Name Kind Details

mode

string

ALL for both indices, SEARCH for search-index or STORAGE for storage-index. Default is ALL.

Returns

NULL

Examples

Refreshing the full index
repo.refresh();
Refreshing the serach index
repo.refresh('SEARCH');

sort

Sorts the children of a node by changing the parent’s _childOrder. When the parent is set to manual ordering, child nodes are reordered according to their _manualOrderValue.

Parameters

An object with the following keys and their values:

Name Kind Details

key

string

Path or ID of the parent node

childOrder

string

New child order expression for the parent (for example displayName ASC or _manualOrderValue DESC)

Returns

object : An object with the updated parent node and an array of reorderedNodes describing children whose ordering was affected.

Example

Setting the children to be ordered in reverse alphabetical order by name
const result = repo.sort({
    key: '/name-list',
    childOrder: 'name DESC'
});

applyPermissions

Applies permissions on a node.

Parameters

An object with the following keys and their values:

Name Type Attributes Details

key

string

Path or id of the node.

permissions

object[]

<optional>

Array of permissions to overwrite current permissions. Cannot be combined with addPermissions or removePermissions.

addPermissions

object[]

<optional>

Array of permissions to add. Cannot be combined with permissions.

removePermissions

object[]

<optional>

Array of permissions to remove. Cannot be combined with permissions.

scope

string

<optional>

Scope of applying permissions. 'SINGLE' applies permissions only to the node itself. 'TREE' applies permissions to the node and its descendants. 'SUBTREE' applies permissions only to the node’s descendants. Default is 'SINGLE'.

branches

string[]

<optional>

Array of additional branches to apply permissions to. Current context branch should not be included.

Each permission entry has the following shape:

Name Type Attributes Details

principal

string

Principal key (for example role:system.everyone or user:system:my-user).

allow

string[]

<optional>

Permissions to allow. Possible values are READ, CREATE, MODIFY, DELETE, PUBLISH, READ_PERMISSIONS, WRITE_PERMISSIONS.

deny

string[]

<optional>

Permissions to deny.

Returns

object : An object keyed by node id, with one branchResults entry per branch the permissions were applied to.

Examples

1) Applying permissions to a node by overwriting the current permissions.
repo.applyPermissions({
    key: '/my-node',
    permissions: [
        {
            principal: 'role:system.everyone',
            allow: ['READ']
        },
        {
            principal: 'user:system:my-user',
            allow: ['READ', 'CREATE', 'MODIFY', 'DELETE']
        }
    ]
});
2) Adding permissions to a node.
repo.applyPermissions({
    key: '/my-node',
    addPermissions: [
        {
            principal: 'role:system.everyone',
            allow: ['CREATE', 'MODIFY']  // 'READ' permission is already set and will be kept.
        },
        {
            principal: 'user:system:my-new-user',
            allow: ['READ', 'CREATE', 'MODIFY', 'DELETE'] // New user will have all permissions.
        }
    ]
});
3) Removing permissions from a node.
repo.applyPermissions({
    key: '/my-node',
    removePermissions: [
        {
            principal: 'role:system.everyone',
            allow: ['CREATE', 'MODIFY']  // 'READ' permission will be kept.
        },
        {
            principal: 'user:system:my-user',
            allow: ['DELETE'] // 'DELETE' permission will be removed. All other permissions will be kept.
        },
        {
            principal: 'user:system:my-new-user' // All permissions for this principal will be removed.
        }
    ]
});
Applying permissions to a node and its descendants.
repo.applyPermissions({
    key: '/my-node',
    permissions: [
        {
            principal: 'role:system.everyone',
            allow: ['READ']
        }
    ],
    scope: 'TREE'
});
Applying permissions to a node’s descendants only.
repo.applyPermissions({
    key: '/my-node',
    permissions: [
        {
            principal: 'role:system.everyone',
            allow: ['READ']
        }
    ],
    scope: 'SUBTREE'
});
Applying permissions to a node and its descendants in multiple branches.
repo.applyPermissions({
    key: '/my-node',
    permissions: [
        {
            principal: 'role:system.everyone',
            allow: ['READ']
        }
    ],
    scope: 'TREE',
    branches: ['branch1', 'branch2']
});

Source

A source definition of repositories:

Fields

Name Kind Details

repoId

string

Repository ID

branch

string

Branch ID

user

User

Optional user to execute the callback with - default is the current user. Not allowed when this source is part of a multiRepoConnect call.

principals

string[]

Additional principals to execute the callback with

User

User object to use for connections:

Fields

Name Kind Details

login

string

user ID of the user

idProvider

string

Optional ID provider containing the user. By default the system provider is used.


Contents

Contents