Role Checking in Liferay for Show/Hide Content

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
Explanation:-

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

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