I want to control the display of the upper management navigation bar (Control menu) in Liferay 7 / DXP

Corresponding version

Liferay DXP SP1 or later / Liferay 7 CE

Introduction

There are many project requirements that want to control the display of the control menu that appears at the top of Liferay. The method of dealing with such a case will be described.

Official Document: THEME CONTRIBUTORS Official Document: CONTEXT CONTRIBUTORS There is an explanation of how to deal with it, but here is an explanation of how to actually create a portlet.

Prerequisites

It is assumed that the Liferay workspace has been created and the blade tools and various Theme creation tools have been installed. The following document explains the setup method.

-How to create a service builder portlet in Liferay DXP / 7 -How to create a theme in Liferay 7 / DXP

As a test scenario, it is implemented with the specification that the standard role of Liferay, the Control Menu is displayed only to users with Administrator authority.

Creating a Template context contributor

Moved under $ (liferay_workspace) / modules.

So we need to create a Theme contributor to replace the Control Menu bundle. Select File-> New-> Liferay Module Project in Liferay Developer Studio / IDE, and select template context contributor in Project Template Name.

Or on the command line

blade create -t template-context-contributor -p com.liferay.product.navigation.control.menu.theme.contributor -c SampleProductNavigationControlMenu sample-product-navigation-control-menu-theme-contributor

Create a module project using the blade tool in.

Creating a SampleProductNavigationControlMenuTemplateContextContributor

Open the base file of the Control Menu

ProductNavigationControlMenuTemplateContextContributor.java


vi /liferay-portal/modules/apps/web-experience/product-navigation/product-navigation-control-menu-theme-contributor/src/main/java/com/liferay/product/navigation/control/menu/theme/contributor/internal/ProductNavigationControlMenuTemplateContextContributor.java

Copy the following part to SampleProductNavigationControlMenuTemplateContextContributor.java.

	@Override
	public void prepare(
		Map<String, Object> contextObjects, HttpServletRequest request) {

		boolean isShowControlMenu = isShowControlMenu(request);

		contextObjects.put("isShowControlMenu", String.valueOf(isShowControlMenuFlg));
	}

	protected boolean isShowControlMenu(HttpServletRequest request) {
		ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
			WebKeys.THEME_DISPLAY);

		if (themeDisplay.isImpersonated()) {
			return true;
		}

		if (!themeDisplay.isSignedIn()) {
			return false;
		}

		User user = themeDisplay.getUser();

		if (!user.isSetupComplete()) {
			return false;
		}

		return true;
	}

Next, if you narrow down the display only if you have the Administrator authority of the standard role, and add the process to set the variable that outputs the display condition to the Theme side, it will be as follows.

    @Override
    public void prepare(
        Map<String, Object> contextObjects, HttpServletRequest request) {

        //Write a variable so that Theme can judge whether the menu is displayed or not.
        boolean isShowControlMenu = isShowControlMenu(request);
        contextObjects.put("isShowControlMenu", String.valueOf(isShowControlMenu));
    }

    protected boolean isShowControlMenu(HttpServletRequest request) {
        ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
            WebKeys.THEME_DISPLAY);

        if(!isAdmin(request)) {
            return false;
        }

        if (themeDisplay.isImpersonated()) {
            return true;
        }

        if (!themeDisplay.isSignedIn()) {
            return false;
        }

        User user = themeDisplay.getUser();

        if (!user.isSetupComplete()) {
            return false;
        }

        return true;
    }

    /**
     * Extra check for test
     * 
     * @param request
     * @return
     */
    protected boolean isAdmin(HttpServletRequest request) {
        ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
                WebKeys.THEME_DISPLAY);
        boolean bRet = false;

        try {

            //I want to display it only when I have administrator privileges.
            //Here, for the sake of explanation, the role is acquired as a character string, but it may be better to use the role ID in the actual project.
            //(Since the role name of the administrator may be changed in the actual project. If the role ID may change for each DB, it may be better to use a character string such as Administrator as the key.)
            Role adminRole = RoleLocalServiceUtil.getRole(themeDisplay.getCompanyId(), "Administrator");

            List<Role> roles = themeDisplay.getUser().getRoles();

            for(Role role : roles) {
                if(role.getName().equals(adminRole.getName())) {
                    bRet = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bRet;
    }	

Next, modify the bnd.bnd file as follows.

bnd.bnd


Bundle-SymbolicName: sampleproductnavigationcontrolmenutemplatecontextcontributor
Bundle-Version: 2.0.6
Liferay-Releng-Module-Group-Description:
Liferay-Releng-Module-Group-Title: Product Navigation
Liferay-Theme-Contributor-Type: product-navigation-control-menu
Web-ContextPath: /product-navigation-control-menu-theme-contributor

Bundle-Version, Liferay-Theme-Contributor-Type, Web-ContextPath copies bnd.bnd of ProductNavigationControlMenuTemplateContextContributor.java included in the current DXP.

Now that the Theme Contributor is ready,

blade deploy

Deploy with, then create Theme.

Creating Theme

Go to the $ (liferay_workspace) / themes directory and create a theme with a Liferay CE classic theme. (See How to create a theme in Liferay 7 / DXP)

Change the following

init-custom.ftl


<#if isShowControlMenuFlg>
    css_class = css_class?replace("has-control-menu", "")
    css_class = css_class?replace("open", "")
</#if>

After changing the CSS class like

portal_normal.ftl


<@liferay.control_menu />

To

portal_normal.ftl


<#assign isShowControlMenu = htmlUtil.escape(isShowControlMenu!) />
<#if "true" == isShowControlMenu >
	<@liferay.control_menu />
</#if>

change to,

gulp deploy

Deploy with.

If you deploy the above Contributor / Theme, the Control Menu should be visible to users with Administrator privileges and not visible to other users.

Recommended Posts

I want to control the display of the upper management navigation bar (Control menu) in Liferay 7 / DXP
I want to change the color of the upper control navigation bar (Control menu) in Liferay 7 / DXP
I want to display the name of the poster of the comment
[Rails] I want to display the link destination of link_to in a separate tab
I want to narrow down the display of docker ps
I want to display the images under assets/images in the production environment
I want to control the default error message of Spring Boot
I want to change the value of Attribute in Selenium of Ruby
I want to display the number of orders for today using datetime.
I want to know the JSP of the open portlet when developing Liferay
[Ruby] I want to display posted items in order of newest date
I want to display an error message when registering in the database
I want to output the day of the week
I want to var_dump the contents of the intent
I want to get the value in Ruby
I want to control the start / stop of servers and databases with Alexa
[Beginner] I tried to decorate the bar after displaying the details of the hamburger menu
I want to embed any TraceId in the log
I want to know the answer of the rock-paper-scissors app
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I want to control the maximum file size in file upload for each URL in Spring Boot
I want to transition to the same screen in the saved state
I want to recreate the contents of assets from scratch in the environment built with capistrano
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to simplify the conditional if-else statement in Java
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
I want to understand the flow of Spring processing request parameters
The story of Collectors.groupingBy that I want to keep for posterity
I want to limit the input by narrowing the range of numbers
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
[Android] I want to get the listener from the button in ListView
I want to import the pull-down menu items when submitting a form in Rails into CSV and display them from the DB data.
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
[Ruby] I want to output only the odd-numbered characters in the character string
I want to display images with REST Controller of Java and Spring!
[Swing] How to display an arbitrary name on the menu bar on Mac
[Ruby] I want to extract only the value of the hash and only the key
How to switch the display of the header menu for each transition page
I want to pass the argument of Annotation and the argument of the calling method to aspect
I want to display a PDF in Chinese (Korean) with thin reports
I want to get the IP address when connecting to Wi-Fi in Java
I tried to make full use of the CPU core in Ruby
I want to get the field name of the [Java] field. (Old tale tone)
I want you to use Enum # name () for the Key of SharedPreference
I want to display background-ground-image on heroku.
I want to use @Autowired in Servlet
If you want to satisfy the test coverage of private methods in JUnit
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to get the value of Cell transparently regardless of CellType (Apache POI)
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
Development memo ~ I want to display only the first image posted multiple times ~
I want to separate the handling of call results according to the API caller (call trigger)
I want to see the contents of Request without saying four or five
I want to recursively get the superclass and interface of a certain class