How do I check if a user has a certain role?

RoleUtil is the starting point to use to check if a user has the required role(s) for a particular page

RoleUtil has methods for providing a RoleMatcherBuilder which is used to set up which user and which role(s) should be matched. RoleUtil also has a method for finding the needed role node(s) (of type sv:role).

When the RoleMatcherBuilder is set up you call the method build() and it will return a instance of RoleMatcher. RoleMatcher has methods for either require the user to match all the added roles or any one of them.

In the example below PortletContextUtil is used to get the current page and the current user. Two roles are added and RoleMatcher is used with its matchesAny-method.

Step 1 - Get instances of the utililies

For WebApp/RESTApp 2; use import:

import portletContextUtil from '@sitevision/api/server/PortletContextUtil';
import roleUtil from '@sitevision/api/server/RoleUtil';

For Script module and WebApp/RESTApp 1; use require:

const portletContextUtil = require('PortletContextUtil');
const roleUtil = require('RoleUtil');

Step 2 - Make use of the utilities

const currentUser = portletContextUtil.getCurrentUser();
const currentPage = portletContextUtil.getCurrentPage();
const roleMatcherBuilder = roleUtil.getRoleMatcherBuilder();


const roleMatcher =;
const currentUserHasRequiredRole = roleMatcher.matchesAny(currentPage);

Do you want to subscribe to News from Sitevision Developer team? Subscribe here!