Show/Hide Content on basis of Role in Liferay
Today we will discuss how to Show/Hide content on the basis of role or we can say that how we perform Role checking in liferay. For achieving this we create two roles teacher and student then we create two users Student and Teacher and assign role accordingly.Then finally Check roles and show/hide content. Lets start step by step:-
Step 1:-Create Two Roles
Go to control panel-->Users-->Roles-->Add-->Regular Roles
Then provide Name ,title and description as:-
and press save .Similarly create Student Role.
Step 2:-Create Two Users
Then create two users and assign roles to it.
Go to control panel-->Users and Organizations-->All users-->Add User-->And provide details as:-
then click save.After that provide a password and click save .Similarly create another user with name Student.
Step 3:-Assign Role to Users
Go to control panel-->Roles-->On Teacher Click action-->Assign Users-->Available-->Selected Users Teacher-->Update Association.
Similarly assign Student role to student user.
Step 4:-Check Role in jsp
view.jsp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@page import="com.liferay.portal.model.Role"%> | |
<%@page import="java.util.List"%> | |
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | |
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%> | |
<liferay-theme:defineObjects /> | |
<portlet:defineObjects /> | |
<h1>Role Checking in Liferay </h1> | |
<% | |
List<Role> userRoles = themeDisplay.getUser().getRoles(); | |
%> | |
<c:forEach var="role" items="<%= userRoles %>"> | |
<c:if test="${role.getName() == 'Student'}"> | |
<h2>Student can See This.</h2><br> | |
</c:if> | |
<c:if test="${role.getName() == 'Teacher'}"> | |
<h2>Teacher can See This.</h2><br> | |
</c:if> | |
</c:forEach> | |
List<Role> userRoles = themeDisplay.getUser().getRoles();
this will return List because one User can have many Roles so we iterare the List and and Checking Role name and on the basis of Role print statement accordingly.
Step 5(Optional):-Check Role in Controller
If you want to check role in your controller then:-
Controller.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import java.io.IOException; | |
import java.util.List; | |
import javax.portlet.GenericPortlet; | |
import javax.portlet.PortletException; | |
import javax.portlet.PortletRequestDispatcher; | |
import javax.portlet.RenderRequest; | |
import javax.portlet.RenderResponse; | |
import com.liferay.portal.kernel.exception.SystemException; | |
import com.liferay.portal.kernel.log.Log; | |
import com.liferay.portal.kernel.log.LogFactoryUtil; | |
import com.liferay.portal.kernel.util.WebKeys; | |
import com.liferay.portal.model.Role; | |
import com.liferay.portal.service.RoleLocalServiceUtil; | |
import com.liferay.portal.theme.ThemeDisplay; | |
public class TestingForRole extends GenericPortlet { | |
public void init() { | |
viewTemplate = getInitParameter("view-template"); | |
} | |
public void doView(RenderRequest request, RenderResponse renderResponse) throws IOException, PortletException { | |
try { | |
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY); | |
//List<Role> userRoles = themeDisplay.getUser().getRoles(); OR | |
List<Role> userRoles=RoleLocalServiceUtil.getUserRoles(themeDisplay.getUserId()); | |
for (Role role : userRoles) { | |
System.out.println("Roles:-"+role.getName()); | |
} | |
} catch (SystemException e) { | |
e.printStackTrace(); | |
} | |
include(viewTemplate, request, renderResponse); | |
} | |
protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, | |
PortletException { | |
PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path); | |
if (portletRequestDispatcher == null) { | |
_log.error(path + " is not a valid include"); | |
} else { | |
portletRequestDispatcher.include(renderRequest, renderResponse); | |
} | |
} | |
protected String viewTemplate; | |
private static Log _log = LogFactoryUtil.getLog(TestingForRole.class); | |
} |
Step 6:-Check Output
Logout from Admin(test@liferay.com) and then
1)Login via teacher@gmail.com and add your portlet on page you can see output as:-
2)Login via student@gmail.com and add your portlet on page you can see output as:-
As you can see output is Changed on the basis of Users.
You can download source code from Role Checking in Liferay
Hope this will help....
Related Post:-
Spring MVC portlet in Liferay
Form Handling in Spring Portlet
Many To Many Relationship mapping in Liferay Services
Liferay Service Builder in Detail
How to install Maven
Creating Portlet/Services using Maven
very nice..
ReplyDeleteThanks Vinod
ReplyDeleteFor unequivocal people this is the best improvement here see how to do it. Guest post
ReplyDelete