context

Built-in context functions.

Example
var contextLib = require('/lib/xp/context');

Methods

(static) get() → {object}

Returns the current context.

Returns:

Return the current context as JSON object.

Type
object
Examples
// Returns the current context.
var result = contextLib.get();
log.info('Context as JSON %s', result);
// Context returned.
var expected = {
    "branch": "draft",
    "repository": "com.enonic.cms.default",
    "authInfo": {
        "principals": [
            "user:system:anonymous",
            "role:system.everyone"
        ]
    }
};

(static) run(context, callback) → {object}

Runs a function within a specified context.

Parameters:
Name Type Description
context object

JSON parameters.

Properties
Name Type Attributes Description
repository string <optional>

Repository to execute the callback in. Default is the current repository set in portal.

branch string <optional>

Name of the branch to execute the callback in. Default is the current branch set in portal.

user object <optional>

User to execute the callback with. Default is the current user.

Properties
Name Type Attributes Description
login string

Login of the user.

idProvider string <optional>

Id provider containing the user. By default, all the id providers will be used.

principals array <optional>

Additional principals to execute the callback with.

attributes object <optional>

Additional Context attributes.

callback function

Function to execute.

Returns:

Result of the function execution.

Type
object
Example
// Define the callback to be executed.
function callback() {
    return 'Hello from context';
}

// Executes a function using different context.
var result = contextLib.run({
    repository: 'system-repo',
    branch: 'master',
    user: {
        login: 'su',
        idProvider: 'system'
    },
    principals: ["role:system.admin"],
    attributes: {
        'ignorePublishTimes': true
    }
}, callback);

log.info('Callback says "%s"', result);