RE: Hide Dockbar based on role in Liferay 7

Durga prasada Rao Kasina, modified 8 Years ago. New Member Posts: 21 Join Date: 7/26/12 Recent Posts
I wanted to hide Dockbar based on role in Liferay 7. when i searched in internet i got the below code but it is in velocity
<#if ( $is_signed_in ) >
<# $rService = $serviceLocator.findService("com.liferay.portal.service.RoleService")>
<# $usrRoles = $rService.getUserRoles( $user_id )>
<# list( $usrRoles as $usrRole )>
<#if ( $usrRole.getName() == "Administrator" || $usrRole.getName() == "Content-Admin" || $usrRole.getName() == "Content-Editor") >
#dockbar()
</#if>
</# list>
</#if>

Can any body pls. let me know the code snippet for freemarker.
thumbnail
Christoph Rabel, modified 8 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
Sure. Each and every customer ask us to remove the dockbar for normal users. I wonder for many years now why there is no permission for that ...


&lt;#if is_signed_in&gt;
	&lt;#assign roles = user.getRoles() 
           showcontrolmenu = false
	/&gt;
	&lt;#list roles as role&gt;
  		&lt;#if role.getName() == "Administrator" || role.getName() == "Other Role" &gt;
     		       &lt;#assign showcontrolmenu = true /&gt;
     		       &lt;#break&gt;
		<!--#if-->             
	<!--#list--> 
	&lt;#if showcontrolmenu&gt;
		@liferay.control_menu
	<!--#if-->
<!--#if-->
thumbnail
Jose Francisco Pertuz Perez, modified 7 Years ago. New Member Posts: 11 Join Date: 8/11/14 Recent Posts
Thank you Christoph Rabel
thumbnail
Enrico Oliosi, modified 7 Years ago. Junior Member Posts: 73 Join Date: 7/6/10 Recent Posts
Hi @Christoph, I think it is necessary a more effort. In your proposal you're looking for regular roles only, but this is not enough. I could have to check other types as site or organizational roles. It is not simple to perform this check using a frontend language (freemarker or velocity).

There is another complexity. The "has-control-menu" css class which is injected into body tag. If you remove @liferay.control_menu macro only, you will find a white bar on the top of the side and this is not a desiderable setting. I think we have to walk on two different ways: write a new theme contributor component and add some controls into frontend template.

First step: add my new theme contributor component

@Component(
	immediate = true,
	property = {"type=" + TemplateContextContributor.TYPE_THEME},
	service = TemplateContextContributor.class
)
public class MyProductNavigationControlMenuTemplateContextContributor implements TemplateContextContributor {

	private static final String ADMIN_PATTERN_ROLE = "administrator";
	
	private static final Log _log = LogFactoryUtil.getLog(MyProductNavigationControlMenuTemplateContextContributor.class);

	@Override
	public void prepare(Map<string, object> contextObjects, HttpServletRequest request) {

		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
	
		//Check some roles
		if ( !themeDisplay.isSignedIn() || hasAdminRegularRole(request) || hasAdminSiteRole(request, themeDisplay) || ...)
		{
			return;
		}
		
		String cssClass = GetterUtil.getString(contextObjects.get("bodyCssClass"));

		//Add a new class to set if a user cannot access to control panel menu
		contextObjects.put("bodyCssClass", cssClass + " delete-control-menu");
	}

[...]
</string,>

Second step: modify template (freemarker in this example)

1) init_custom.ftl, I have added this lines:

[...]
&lt;#if css_class?lower_case?contains("delete-control-menu")&gt;
    &lt;#assign
    	css_class = css_class?replace("has-control-menu", "")
    	showcontrolmenu = false
    /&gt;
&lt;#else&gt;
    &lt;#assign showcontrolmenu = true /&gt;
<!--#if-->
[...]

1) portal_normal.ftl, I have added showcontrolmenu variable checking:

[...]


    &lt;@liferay_ui["quick-access"] contentId="#main-content" /&gt;
    &lt;@liferay_util["include"] page=body_top_include /&gt;
    
  	&lt;#if showcontrolmenu&gt;
	    &lt;@liferay.control_menu/&gt;
        <!--#if-->
[...]


I am working on LF CE GA 6 (7.0.5).

Enrico
Oscar Reyes, modified 7 Years ago. New Member Posts: 2 Join Date: 2/1/18 Recent Posts

Hi Enrico!

 

Can I have the jar?

thumbnail
sanju ., modified 6 Years ago. New Member Posts: 3 Join Date: 7/16/10 Recent Posts
Hi
​​​​​​​
I can able to set the permistion for Regular Roles but it's not working for Site Role.

Please help. how to add site roles permistion as well

-------------------------------------------------------------------------------------
<#if is_signed_in>
    <#assign roles = user.getRoles() 
           showcontrolmenu = false />
    <#list roles as role>
          <#if role.getName() == "Administrator" || role.getName() == "Site Administrator" >
            <#assign showcontrolmenu = true />
            <#break>
        </#if>             
    </#list> 

    <#if showcontrolmenu>
        <@liferay.control_menu />
    </#if>

</#if>
-------------------------------------------------------------------------------------
thumbnail
Christoph Rabel, modified 6 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
You could use UserGroupRoleLocalServiceUtil:

https://community.liferay.com/de/forums/-/message_boards/message/12032442
thumbnail
sanju ., modified 6 Years ago. New Member Posts: 3 Join Date: 7/16/10 Recent Posts
Thank you for reply 
Lee Jordan, modified 5 Years ago. Expert Posts: 449 Join Date: 5/26/15 Recent Posts
Adding to this interesting topic. Wiping out the dockbar for site members and guest users has been a terrible thing for me to have to correct. Our users are never signed out and always need a dockbar regardless of if they are not an admin; however the dockbar contained our branding, access to our help site and a list of applications like a hub for all our organizations other web apps. It's just gone now for 80% of our users which is ridiculous.


So it's not correct that non-admins and other users like site visitors don't need a dockbar. I've had to create my own "fake dockbar" containing all the stuff that was there in 7.0 and do a lot of jiggery pokery that has taken MONTHS to get right. When we make these suggestions in discussions about taking things away from users can we understand that doing so impacts other people please? It should be a configurable option not just taken away from site members like it has been.


The result is that 7.1+ now has a dockbar only for site and portal admins, so we've had our branding and global dockbar links wiped out.
thumbnail
Christoph Rabel, modified 5 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
And what stopped you from giving all users the permission to see the dockbar? Liferay took nothing away here, it just added "You need a permission now to see the dockbar".
Lee Jordan, modified 5 Years ago. Expert Posts: 449 Join Date: 5/26/15 Recent Posts
Lack of documentation and communication.
Lee Jordan, modified 5 Years ago. Expert Posts: 449 Join Date: 5/26/15 Recent Posts
On the actual topic of detecting if the user is an admin or member of the site ... this is CRAZY difficult in a theme portal_normal and I've already given up a few times.
thumbnail
Olaf Kock, modified 5 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Lee Jordan:

On the actual topic of detecting if the user is an admin or member of the site ... this is CRAZY difficult in a theme portal_normal and I've already given up a few times.
Luckily, in the mean time, there's an answer here. (You're aware of that thread, I know. I'm just crossreferencing for people coming along this post later)