Friday, June 13, 2014

[Liferay] Permission Checker : Setting a portlet to be available for Admin only..

In last post, we got all the sites, but everyone can access what we have in front end if we don't take care of Permissions. 

Permission checker comes into the picture.. 


PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker(); 
Boolean isUserAdmin =permissionChecker.isOmniadmin();
if(permissionChecker.isOmniadmin()){
          // Do what you want.. no user other than admin can see this.
}
Imports..
com.liferay.portal.security.permission.PermissionThreadLocal
com.liferay.portal.security.permission.PermissionChecker


That's all we need. 

Enjoy. Now you have all the sites.

Thursday, June 12, 2014

[Liferay] Get all the sites in Liferay

Its time I start posting about Liferay.. :)

Most of the times we need very small things.. code that can help us getting data from liferay.. its all there already, but we are not aware most often..

I had to load all groups/sites in a dropdown and then select one to extract some content from the document library of that group. So to make it easier, and clean It was better to provide a list of all sites to Admin and let Admin decide which one to be selected.

To get all the groups we can simply use.
List<Group> groups = GroupLocalServiceUtil.getGroups(0, GroupLocalServiceUtil.getGroupsCount());

But to get only sites we need.. to use search method.
List<Group> groups = GroupLocalServiceUtil.search(PortalUtil.getCompanyId(renderRequest), null, null,null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);

Imports are..
com.liferay.portal.kernel.dao.orm.QueryUtil
com.liferay.portal.util.PortalUtil
com.liferay.portal.model.Group
com.liferay.portal.service.GroupLocalServiceUtil

Thats it.. :)