Many a times, it is required to have Import/Export functionality for Roles.
This functionality is already supported in Liferay 6.2 where we can do lar import/export for roles and organizations as well.
Below is the code snippet for Exporting Roles along with Permissions in the form of xml in Liferay 6.1.
Create a hook in liferay eg, RolesMgmt hook. Create a class called as ExportRolesAction which extends BaseStrutsPortletAction.
Refer below code for the same:
Now, you can override view.jsp from path - /portal-web/docroot/html/portlet/role_admin/view.jsp
and add code to display buttons for Import Roles and Export Roles on front end.
So in addition to the existing code of view.jsp, you can add below code snippet for the import/export:
You can also add code for validating the checkboxes like disabling the checkboxes of default role types. In the next post I shall be giving sample code for Roles Import.
Thanks for reading!
Happy Learning!
This functionality is already supported in Liferay 6.2 where we can do lar import/export for roles and organizations as well.
Below is the code snippet for Exporting Roles along with Permissions in the form of xml in Liferay 6.1.
Create a hook in liferay eg, RolesMgmt hook. Create a class called as ExportRolesAction which extends BaseStrutsPortletAction.
Refer below code for the same:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.liferay.portal.kernel.exception.PortalException; | |
import com.liferay.portal.kernel.exception.SystemException; | |
import com.liferay.portal.kernel.servlet.ServletResponseUtil; | |
import com.liferay.portal.kernel.struts.BaseStrutsPortletAction; | |
import com.liferay.portal.kernel.struts.StrutsPortletAction; | |
import com.liferay.portal.kernel.util.ContentTypes; | |
import com.liferay.portal.kernel.util.ParamUtil; | |
import com.liferay.portal.kernel.util.StringUtil; | |
import com.liferay.portal.model.ResourceAction; | |
import com.liferay.portal.model.ResourcePermission; | |
import com.liferay.portal.model.Role; | |
import com.liferay.portal.service.ResourceActionLocalServiceUtil; | |
import com.liferay.portal.service.ResourcePermissionLocalServiceUtil; | |
import com.liferay.portal.service.RoleLocalServiceUtil; | |
import com.liferay.portal.util.PortalUtil; | |
import com.liferay.portal.kernel.util.WebKeys; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.List; | |
import javax.portlet.ActionRequest; | |
import javax.portlet.ActionResponse; | |
import javax.portlet.PortletConfig; | |
import javax.portlet.PortletRequest; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.dom4j.Document; | |
import org.dom4j.DocumentHelper; | |
import org.dom4j.Element; | |
public class ExportRolesAction extends BaseStrutsPortletAction { | |
@Override | |
/** | |
* This method is called when user clicks on export roles. It extract the exported role ids from request | |
* and calls the exprotRoles function to generate the XML. | |
*/ | |
public void processAction(StrutsPortletAction aOriginalStrutsPortletAction, | |
PortletConfig aPortletConfig, ActionRequest aActionRequest, | |
ActionResponse aActionResponse) throws Exception { | |
try { | |
long[] ids = StringUtil.split( | |
ParamUtil.getString(aActionRequest, "addCommIds"), 0L); | |
String xmlResult = exportRoles(ids); | |
String fileName = "roles.xml"; | |
byte[] bytes = xmlResult.getBytes(); | |
HttpServletRequest request = PortalUtil | |
.getHttpServletRequest(aActionRequest); | |
HttpServletResponse response = PortalUtil | |
.getHttpServletResponse(aActionResponse); | |
ServletResponseUtil.sendFile(request, response, fileName, bytes, | |
ContentTypes.TEXT_XML_UTF8); | |
setForward(aActionRequest, "/common/null.jsp"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Sets the forward key in request. | |
* | |
* @param portletRequest | |
* @param forward | |
*/ | |
protected void setForward(PortletRequest portletRequest, String forward) { | |
String portletId = (String) portletRequest | |
.getAttribute(WebKeys.PORTLET_ID); | |
String portletNamespace = PortalUtil.getPortletNamespace(portletId); | |
String forwardKey = portletNamespace.concat("PORTLET_STRUTS_FORWARD"); | |
portletRequest.setAttribute(forwardKey, forward); | |
} | |
/** | |
* It removes out all roles which are not custom. | |
* | |
* @param roles | |
*/ | |
private void validateRoles(List<Role> roles) | |
{ | |
// Regular roles | |
String[] systemRoles = PortalUtil.getSystemRoles(); | |
List<String> systemRoleNames = Arrays.asList(systemRoles); | |
// Organization roles | |
String[] systemOrganizationRoles = PortalUtil | |
.getSystemOrganizationRoles(); | |
List<String> systemOrganizationRoleNames = Arrays | |
.asList(systemOrganizationRoles); | |
// Site roles | |
String[] systemSiteRoles = PortalUtil.getSystemSiteRoles(); | |
List<String> systemSiteRoleNames = Arrays.asList(systemSiteRoles); | |
for (Iterator iterator = roles.iterator(); iterator.hasNext();) { | |
Role role = (Role) iterator.next(); | |
if (systemRoleNames.contains(role.getName()) | |
|| systemOrganizationRoleNames.contains(role.getName()) | |
|| systemSiteRoleNames.contains(role.getName()) | |
|| role.getName().contains("organization content author") | |
|| role.getName().contains("organization content admin")) | |
iterator.remove(); | |
} | |
} | |
/** | |
* This method used liferay roles API to get the roles details of the | |
* exported ids and transform the information into XML. | |
* | |
* @param ids | |
* @return | |
* @throws IOException | |
* @throws SystemException | |
* @throws PortalException | |
*/ | |
public String exportRoles(long[] ids) throws IOException, SystemException, | |
PortalException { | |
List<Role> roles = RoleLocalServiceUtil.getRoles(ids); | |
validateRoles(roles); | |
Document document = DocumentHelper.createDocument(); | |
Element rootElement = document.addElement("root"); | |
Element rolesElement = rootElement.addElement("roles"); | |
for (Role role : roles) { | |
Element roleElement = rolesElement.addElement("role"); | |
roleElement | |
.addAttribute("roleId", String.valueOf(role.getRoleId())); | |
roleElement.addAttribute("companyId", | |
String.valueOf(role.getCompanyId())); | |
roleElement.addAttribute("name", role.getName()); | |
roleElement.addAttribute("title", role.getTitleCurrentValue()); | |
roleElement.addAttribute("description", | |
role.getDescriptionCurrentValue()); | |
roleElement.addAttribute("type", String.valueOf(role.getType())); | |
roleElement.addAttribute("subtype", | |
String.valueOf(role.getSubtype())); | |
Element permissionsElement = roleElement.addElement("permissions"); | |
List<ResourcePermission> resourcePermissions = ResourcePermissionLocalServiceUtil | |
.getRoleResourcePermissions(role.getRoleId()); | |
for (ResourcePermission resourcePermission : resourcePermissions) { | |
List<ResourceAction> resourceActions = ResourceActionLocalServiceUtil | |
.getResourceActions(resourcePermission.getName()); | |
for (ResourceAction resourceAction : resourceActions) { | |
if (ResourcePermissionLocalServiceUtil.hasActionId( | |
resourcePermission, resourceAction)) { | |
Element resourcePermissionElement = permissionsElement | |
.addElement("permission"); | |
resourcePermissionElement.addAttribute("name", | |
resourcePermission.getName()); | |
resourcePermissionElement.addAttribute("companyid", | |
String.valueOf(resourcePermission | |
.getCompanyId())); | |
resourcePermissionElement.addAttribute("roleId", | |
String.valueOf(resourcePermission.getRoleId())); | |
resourcePermissionElement.addAttribute("actionId", | |
String.valueOf(resourceAction.getActionId())); | |
resourcePermissionElement.addAttribute("scopeId", | |
String.valueOf(resourcePermission.getScope())); | |
resourcePermissionElement | |
.addAttribute("ownerId", String | |
.valueOf(resourcePermission | |
.getOwnerId())); | |
resourcePermissionElement | |
.addAttribute("primKey", String | |
.valueOf(resourcePermission | |
.getPrimKey())); | |
} | |
} | |
} | |
} | |
return document.asXML(); | |
} | |
} |
Now, you can override view.jsp from path - /portal-web/docroot/html/portlet/role_admin/view.jsp
and add code to display buttons for Import Roles and Export Roles on front end.
So in addition to the existing code of view.jsp, you can add below code snippet for the import/export:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
Liferay.provide( | |
window, | |
'<portlet:namespace />exportRoles', | |
function() { | |
document.<portlet:namespace />fm.<portlet:namespace />addCommIds.value = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm1, "<portlet:namespace />allRowIds"); | |
if(document.<portlet:namespace />fm.<portlet:namespace />addCommIds.value.length) | |
{ | |
document.<portlet:namespace />fm.method = "post"; | |
submitForm(document.<portlet:namespace />fm, | |
"<portlet:actionURL> <portlet:param name="struts_action" value="/roles_admin/export_role" /></portlet:actionURL>&etag=0&strip=0&compress=0", false); | |
}else { | |
alert("No rows selected."); | |
} | |
}, | |
['liferay-util-list-fields'] | |
); | |
Liferay.provide( | |
window, | |
'<portlet:namespace />importRoles', | |
function() { | |
if(document.<portlet:namespace />fm.importText.value==null || document.<portlet:namespace />fm.importText.value=='') | |
{ | |
alert('Please select a valid file'); | |
return false; | |
} | |
else if (confirm('<%= UnicodeLanguageUtil.get(pageContext, "are-you-sure-you-want-to-upload-file") %>')) { | |
submitForm(document.<portlet:namespace />fm, | |
"<portlet:actionURL> <portlet:param name="struts_action" value="/roles_admin/import_role" /></portlet:actionURL>&etag=0&strip=0&compress=0", false); | |
} | |
}, | |
['liferay-util-list-fields'] | |
); | |
</script> |
Thanks for reading!
Happy Learning!
Very informative post within your coding share in this post. Thanks for the share.
ReplyDeletePortal solutions in coimbatore | Best e-commerce service provider