How do I find the current user and its properties?
Use a combination of PortletContextUtil and Properties to extract the current user and its properties
In order to get the current user (as a JCR Node) the method getCurrentUser of PortletContextUtil is used.
To extract properties from a node in Sitevision either Properties or PropertyUtil can be used. This example will use Properties.
Step 1 - Get instances of utilities
For WebApp/RESTApp 2; use import:
import portletContextUtil from '@sitevision/api/server/PortletContextUtil';
import properties from '@sitevision/api/server/Properties';
For Script module and WebApp/RESTApp 1; use require:
const portletContextUtil = require('PortletContextUtil');
const properties = require('Properties');
Step 2 - Make use of the utilities
// Get the current user (JCR Node)
const currentUser = portletContextUtil.getCurrentUser();
// Get a single property
const currentUserMail = properties.get(currentuser, 'mail');
// Get a number of properties as an object
const currentUserData = properties.get(
currentUser,
'displayName',
'mail',
'telephoneNumber',
'title'
);
/*
currentUserData will be an object:
{
displayName: 'Anna Andersson',
mail: 'anna.andersson@mail.com',
telephoneNumber: '0123456789',
title: 'Key account manager'
}
*/
Regarding properties
What properties there is to extract from each node type is documented on the node types page.
Note regarding user types
In Sitevision there are 3 types of users:
Note that available properties vary between the user types. If a property which is not available for the user type is being requested, null will be returned.
Do you want to subscribe to News from Sitevision Developer team? Subscribe here!