Sitevision Developer
Log in

Log in

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.

Published: 2022-07-22  |  Updated: 2024-09-26

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 - use import:

For Script module - use require:

Step 2 - Make use of the utilities