Liferay DXP SP1 or later / Liferay 7 CE
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.
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.
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.
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.
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