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;
}
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
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:
Post a Comment