Friday, April 10, 2020

Checking Permission for Page in Liferay

The below code will help you to check the guest permission for page in Liferay.

import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.model.Role;
import com.liferay.portal.kernel.model.ResourcePermission;
import com.liferay.portal.kernel.service.ResourcePermissionLocalServiceUtil;
import com.liferay.portal.kernel.service.RoleLocalServiceUtil;
import com.liferay.portal.kernel.util.Validator;

// We are passing the Layout Object, as we know in liferay page information will be available in Layout table
public static boolean checkguestpermission(Layout layout) {
boolean guestPermission = false;
try {
Role guestRole =  RoleLocalServiceUtil.fetchRole(layout.getCompanyId(), "Guest");
List resourcePermissionList = ResourcePermissionLocalServiceUtil.getResourceResourcePermissions(layout.getCompanyId(),
layout.getGroupId(), Layout.class.getName(), String.valueOf(layout.getPlid()));
logger.debug("resourcePermissionList :" + resourcePermissionList.size());
for(ResourcePermission permission : resourcePermissionList){
                  // ActionId 1 for VIEW permission in liferay
if(permission.getActionIds() == 1){
Role role = RoleLocalServiceUtil.getRole(permission.getRoleId());
if(Validator.isNotNull(role)&&permission.getRoleId()==guestRole.getRoleId()){
guestPermission = true;
}
}
}
}catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return guestPermission;
}

No comments:

Liferay DXP JNDI Data Source Cofiguration

 This Blog will help us to learn about the JNDI Data Source Configuration in Liferay DXP. We have tested this with Liferay 7.3 with Tomcat. ...