Working with Liferay User Roles


Liferay  have different types of roles for user. So whenever we develop portlet application we may get need to fetch user roles.
 
The following article will give you more about Liferay Roles


Generally we have following roles in Liferay
  1. Regular Roles/Portal Roles
  2. Organization Roles
  3. Site Roles
  4. Inherited Roles
Portal Role/Regular Roles

Liferay is providing Portal Role/Regular Role for portal level. It’s not specific to anything like Organization, Site or User Group. 

This role can be assigned to any user who belongs to any one of Organization, Community/Site or User Group.

Generally when we associate Portal Role/Regular Role s to any user then the association can be stored in Users_Roles Mapping Table.

To fetch Portal Role/Regular Role we can use RoleLocalServiceUtil.java class and these classes have many service methods which can fetch roles with respect to selected user.

We can use following ways to fetch user Portal Role/Regular Roles

Use RoleLocalSercviceUtil


List<Role> userRoles=RoleLocalServiceUtil.getUserRoles(themeDisplay.getUserId());


Use User Object 
 

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


Site Roles 

Site Role is one of the types in liferay which is only associated to Site users. This role only we can associate to site users. When we create site role we can use this site role to any used who belongs to any site liferay.

When we associate any site roles to user then association will be stored in UserGroupRole table.When ever we want get site roles then we have to use respective service class to access those roles like we need use UserGroupRoleLocalService.java class there we can find many service methods.


List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> siteRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_SITE) {
siteRoles.add(userGroupRole);
}
}


Organization Roles

Similar to site role organization role is used for organization users. This role can be associated to any user who belongs to any organization in portal.

When we associate any Organization roles to user then association will be stored in UserGroupRole table.When ever we want get Organization roles then we have to use respective service class to access those roles like we need use UserGroupRoleLocalService.java class there we can find many service methods.


List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> organizationRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_ORGANIZATION) {
organizationRoles.add(userGroupRole);
}
}



Inherited Roles

Inherited roles really not existed in the liferay but we can see these roles in the user my account page roles section .these roles specially appear when the user can be member of user group which is assigned a role.

We can say if any roles which associates with User Group and the user is member of respective user group then role can be visible as part of inherited roles section.

Simply we can say that user directly not associated with role instead of that User Group will be associated with role and the user will be member of User Group then the roles are become as inherited role to users who are belong to User Group.

Understanding inherited roles
  • Create User Group (Photography) and assign Users to Photography User Group
  • Create role called Photography Group Member
  • Assign User Group to Role( associate role to User Group)

Now all users who are belongs to User Group will be get Photography Group Member role as inherited role when we observe here Photography Group Member role is not directly associated with user but role is associated with Photography user group because of this Photography Group Member become as inherited role.

When we want fetch inherited roles first we need to find all User Groups of respective user so that we can fetch all inherited roles.


<%
User selUser=themeDisplay.getUser();
List<Group> allGroups = new ArrayList<Group>();
List<UserGroup> userGroups = selUser.getUserGroups();
List<Group> groups = selUser.getGroups();
List<Organization> organizations = selUser.getOrganizations();
allGroups.addAll(groups);
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsGroups(organizations));
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsRelatedGroups(organizations));
allGroups.addAll
(GroupLocalServiceUtil.getUserGroupsGroups(userGroups));
allGroups.addAll(GroupLocalServiceUtil.
getUserGroupsRelatedGroups(userGroups));
for(int i=0;i<allGroups.size();i++){
com.liferay.portal.model.Group group=allGroups.get(i);
List<Role> groupRoles = RoleLocalServiceUtil.getGroupRoles(group.getGroupId());
if (!groupRoles.isEmpty()) {
Role groupRole = groupRoles.get(0);
out.println(ListUtil.toString(groupRoles,
 Role.NAME_ACCESSOR));
}
}
%>


Note:

When we work with above code respective Java classes should be import.

The following is sample code snippets which is in JSP page


<%@page import="com.liferay.portal.kernel.util.ListUtil"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Organization"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.model.UserGroup"%>
<%@page import="com.liferay.portal.model.Group"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.liferay.portal.model.RoleConstants"%>
<%@page import="com.liferay.portal.service.UserGroupRoleLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.UserGroupRole"%>
<%@page import="com.liferay.portal.model.Role"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.service.RoleLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<%
List<Role> userRoles=RoleLocalServiceUtil.getUserRoles(themeDisplay.getUserId());
List<Role> userRoles1=themeDisplay.getUser().getRoles();
for (Role role : userRoles) {
out.println(role.getName());
}
%>
<%
List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> organizationRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_ORGANIZATION) {
organizationRoles.add(userGroupRole);
out.println(userGroupRole.getRole().getName());
}
}
%>
<%
List<UserGroupRole> userGroupRoles1 = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId());
List<UserGroupRole> siteRoles = new ArrayList<UserGroupRole>();
for (UserGroupRole userGroupRole : userGroupRoles1) {
int roleType = userGroupRole.getRole().getType();
if (roleType == RoleConstants.TYPE_SITE) {
siteRoles.add(userGroupRole);
out.println(userGroupRole.getRole().getName());
}
}
%>
<%
User selUser=themeDisplay.getUser();
List<Group> allGroups = new ArrayList<Group>();
List<UserGroup> userGroups = selUser.getUserGroups();
List<Group> groups = selUser.getGroups();
List<Organization> organizations = selUser.getOrganizations();
allGroups.addAll(groups);
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsGroups(organizations));
allGroups.addAll(GroupLocalServiceUtil.
getOrganizationsRelatedGroups(organizations));
allGroups.addAll(GroupLocalServiceUtil.
getUserGroupsGroups(userGroups));
allGroups.addAll(GroupLocalServiceUtil.
getUserGroupsRelatedGroups(userGroups));
for(int i=0;i<allGroups.size();i++){
com.liferay.portal.model.Group group=allGroups.get(i);
List<Role> groupRoles = RoleLocalServiceUtil.getGroupRoles(group.getGroupId());
if (!groupRoles.isEmpty()) {
Role groupRole = groupRoles.get(0);
out.println(ListUtil.toString(groupRoles,
 Role.NAME_ACCESSOR));
}
}
%>


Author

Share on Google Plus

About Meera Prince

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment