Tips & Tricks - Displaying Elements Based on Permissions

To display buttons, groups, or grids based on a user's permissions, Velocity provides methods that can be used in conditional display. Using these methods, information can also be displayed based on permissions using a VTL include element. Enable the expert options so that all the dialog boxes mentioned in this example are accessible. Knowledge of Velocity and application development is a plus for this position.

First, determine the GUIDs of all user groups or roles that are to be granted permissions. In the "Users" module, the details—which include the GUID—can be displayed using the main menu option "View / Details" as soon as a user object is selected. You can then select the GUID and copy it to the clipboard. The GUIDs determined in this way are defined as an array in the Velocity context.

            #set($groupGuids = ['GUID1','GUID2', … 'GUIDn'])
        

In the second step, all memberships associated with the logged-in user are identified.

            #set($userMembership = $Portal.OrgStructure.getMembershipSets($User))
        

The ".intersects()" method can be used to check whether the user is a member of any of the authorized groups or roles. The method returns "true" if there is at least one membership and "false" if there are no matches. This can be checked in an IF statement.

            #if($userMembership.intersects($groupGuids) == true)
  ## Display information or code
#end

        

The following example checks whether the logged-in user is a member of the Administrators user group and displays this information. The ".isEmpty()" method can be used to check whether the user is a member of a group or role, or whether the user has not been assigned to any group or role.

            #set($groupGuids = ['EF16F15EDA8562E19D7CD56BF2E43001F119193C'])

#set($userMembership = $Portal.OrgStructure.getMembershipSets($User))

#if($userMembership.intersects($groupGuids) == true)
 The logged-in user is a member of the "Administrators" group
#else
 The logged-in user is not a member of the "Administrators" group
#end

#if($userMembership.isEmpty() == true)
 The logged in user is not assigned to any group/role
#end

        

When you open the Conditional Display Editor for the first time, a variable referencing the current element (button, group, or grid) is already predefined there.

            #set($show_simplegroup155043BA = true)
        

The variable consists of "$show_" followed by the element's "name" attribute. If the value is set to "false," the element will not be generated on the server. If "true," the element is created and displayed. The following example displays a group only if the logged-in user is a member of the "Users" user group.

            #set($show_simplegroup155043BA = false)

#set($groupGuids = ['6AA80844C3C99EF93BF4536EB18605BF86FDD3C5'])
#set($userMembership = $Portal.OrgStructure.getMembershipSets($User))

#if($userMembership.intersects($groupGuids) == true)
  #set($show_simplegroup155043BA = true)
#end

        

If buttons, groups, or grids are to be displayed only to anonymous users, you can check the currently logged-in user using the ".isAnonymous()" method.

            #set($show_simplegroup9238DAD3 = false)

#if($User.isAnonymous())	
  #set($show_simplegroup9238DAD3 = true)
#end