30 March, 2016

Useful Roles and permission API

Reading portlet resources and model resources:
<pre>
List<String> portletResources = ResourceActionsUtil.getPortletResourceActions(PortletLocalServiceUtil.getPortletById(portletId).getPortletId());

List<String> modelResource = ResourceActionsUtil.getPortletModelResources(portletId);
</pre>

Adding and removing permission:

for(String resouce :  portletResources){
if(valid.contains(resouce)){
ResourcePermissionLocalServiceUtil.addResourcePermission(
role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY, String.valueOf(role.getCompanyId()),
role.getRoleId(), resouce);
}else{
try{
ResourcePermissionServiceUtil.removeResourcePermissions(
groupId, role.getCompanyId(), portletId,
ResourceConstants.SCOPE_COMPANY, roleId, resouce);
}catch(Exception e){
e.printStackTrace();
}
}

}


Reading checked values in controller:

<aui:input name="resourcePermission" type="checkbox" onClick="setSelectedResources" checked="<%= (Validator.isNotNull(currentCompanyActions) && currentCompanyActions.contains(resource)) ? true : false %>" value="<%=resource %>" label="<%= LanguageUtil.get(themeDisplay.getLocale(), \"action.\"+resource) %>"></aui:input>

String[] resourceKeys = ParamUtil.getParameterValues(actionRequest, "resourcePermissionCheckbox");


Retrieve Saved portlet Permissions :

Portlet portlet = null;
String portletId = ParamUtil.getString(renderRequest, "portletId");
String roleId = ParamUtil.getString(renderRequest, "roleId");
Role role = RoleLocalServiceUtil.getRole(Long.parseLong(roleId));
portlet = PortletLocalServiceUtil.getPortletById(portletId);
List<String> currentCompanyActions = new ArrayList<String>();
List<String> portletResources = ResourceActionsUtil.getPortletResourceActions(portlet);
try {

Resource resource = ResourceLocalServiceUtil.getResource(role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY, String.valueOf(role.getCompanyId()));
currentCompanyActions.addAll(ResourcePermissionLocalServiceUtil.getAvailableResourcePermissionActionIds(
resource.getCompanyId(), resource.getName(),
ResourceConstants.SCOPE_COMPANY,
String.valueOf(resource.getCompanyId()),
role.getRoleId(), portletResources));

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

Rate Me:

0 Discussion: