Context library

Contents

This API provides functions to access and use the current context.

Usage

Add the following to your build.gradle file:

dependencies {
  include xplibs.context
}

Add the import statement to your code:

import contextLib from '/lib/xp/context';

You are now ready to use the API.

Functions

get

Returns the current context.

Parameters

None

Returns

object : (Context) The current context. The attributes field is always present (possibly empty); branch, repository, and authInfo are present when set on the calling context.

Example

Getting the current context
const context = contextLib.get();
Sample response
{
  branch: "draft",
  repository: "com.enonic.cms.default",
  authInfo: {
      user: {                            (1)
          type: "user",
          key: "user:system:abc",
          displayName: "A.B.C.",
          disabled: false,
          email: "abc@enonic.com",
          login: "abc",
          idProvider: "system",
          hasPassword: true
      },
      principals: [
          "user:system:abc",
          "role:system.admin",
          "role:system.admin.login",
          "role:system.authenticated",
          "role:system.everyone"
      ]
  },
  attributes: {
     optionalAttributes: "of any kind"
  }
}
1 The user field is only set when a user is logged in.

run

Runs a function inside a custom context, for instance the one returned by the get() function call. Commonly used when accessing repositories, or to override the current user’s permissions.

Parameters

Name Kind Details

context

object

ContextParams to be used for the scope of the callback function. Any field omitted is inherited from the current context.

callback

function

Function to execute. Its return value is propagated as the return value of run.

Returns

any : Whatever the callback returns.

Example

Run a function in a different context
import {run} from '/lib/xp/context';

const result = run({
    repository: 'system-repo',
    branch: 'master',
    user: {
        login: 'su',
        idProvider: 'system'
    },
    principals: ['role:system.admin'],
    attributes: {
        ignorePublishTimes: true
    }
}, () => 'Hello from context');
Sample response
"Hello from context"

Type Definitions

GetContext

The shape of the object returned by get().

Sample context object
{
  repository: "some.repo.name", (1)
  branch: "master", (2)
  authInfo: { (3)
      user: { (4)
          type: "user",
          key: "user:system:mylogin",
          displayName: "My User",
          login: "mylogin",
          idProvider: "system",
          hasPassword: true
      },
      principals: ["role:system.admin"] (5)
  },
  attributes: { (6)
     optionalAttributes: "of any kind"
  }
}
1 repository (string, optional) Repository context.
2 branch (string, optional) Branch context.
3 authInfo (object, optional) Authentication information for the current context.
4 user (object, optional) The currently authenticated user. Omitted when no user is logged in.
5 principals (string[], optional) Principal keys (users, groups, and roles) attached to the authentication info.
6 attributes (object) Custom attributes set on the context. Always present; may be empty. Only values of type number, string, boolean, or plain object are serialized.

RunContext

The parameter object passed to run(). Any field omitted is inherited from the calling context.

Sample context object
{
  repository: "some.repo.name", (1)
  branch: "master", (2)
  user: { (3)
      login: "mylogin",
      idProvider: "system"
  },
  principals: ["role:system.admin"], (4)
  attributes: { (5)
     optionalAttributes: "of any kind"
  }
}
1 repository (string, optional) Repository to run the callback in. Defaults to the current repository.
2 branch (string, optional) Branch to run the callback in. Defaults to the current branch.
3 user (object, optional) User to run the callback as. login is required; idProvider defaults to the system id provider when omitted.
4 principals (string[], optional) Additional principal keys (users, groups, and roles) to attach to the authentication info for the duration of the callback.
5 attributes (object, optional) Additional context attributes. Only values of type number, string, boolean, or plain object are supported.

Contents

Contents