Wednesday, 17 September 2014

Liferay, Display Docbar based on User Role

Firstly go to Control Panel > Roles and create a Role Dockbar. This is the Name that will be used to check if the Dockbar has to be displayed to the User.

In the portal_normal.vm file, present in the theme, we have to modify the below lines. Right now, dockbar is visible to anybody who signs in. We need to change it so that it is visible to users with a specific role.
#if ($is_signed_in)
 #dockbar()
#end
This makes the com.liferay.portal.service.UserService available in the Velocity Template. It is assigned to the $userService variable.
#set($userService = $serviceLocator.findService("com.liferay.portal.service.UserService"))
This line Calls the hasRoleUser method, to check if the user has the Role Dockbar assigned to him.
#set($docbarVisible = $userService.hasRoleUser($company_id, "Dockbar", $user_id, true))
The below line checks if the user is Signed In AND has the role Dockbar OR if the User is the Company Admin.
#if ($is_signed_in && ($docbarVisible || $permissionChecker.isCompanyAdmin()))
Final Code
#set($userService = $serviceLocator.findService("com.liferay.portal.service.UserService"))
#set($docbarVisible = $userService.hasRoleUser($company_id, "Dockbar", $user_id, true))

#if ($is_signed_in && ($docbarVisible || $permissionChecker.isCompanyAdmin()))
 #dockbar()
#end
Now all that is left out is to assign few users the Dockbar role to verify that the Users will have access to the Dockbar. Hope the entire thing makes sense.
Feel free to post your comments.

No comments :

Post a Comment