001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.exportimport.lar;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.KeyValuePair;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.xml.Document;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.SAXReaderUtil;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.PortletConstants;
026    import com.liferay.portal.model.ResourceConstants;
027    import com.liferay.portal.model.Role;
028    import com.liferay.portal.security.permission.ResourceActionsUtil;
029    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
030    import com.liferay.portal.service.RoleLocalServiceUtil;
031    import com.liferay.portal.service.permission.PortletPermissionUtil;
032    
033    import java.util.List;
034    import java.util.Map;
035    import java.util.Set;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Joel Kozikowski
040     * @author Charles May
041     * @author Raymond Aug??
042     * @author Jorge Ferrer
043     * @author Bruno Farache
044     * @author Zsigmond Rab
045     * @author Douglas Wong
046     */
047    public class PermissionExporter {
048    
049            public static final String ROLE_TEAM_PREFIX = "ROLE_TEAM_,*";
050    
051            public static PermissionExporter getInstance() {
052                    return _instance;
053            }
054    
055            protected void exportPermissions(
056                            PortletDataContext portletDataContext, String resourceName,
057                            String resourcePrimKey, Element permissionsElement)
058                    throws Exception {
059    
060                    List<String> actionIds = ResourceActionsUtil.getPortletResourceActions(
061                            resourceName);
062    
063                    Map<Long, Set<String>> roleToActionIds =
064                            ResourcePermissionLocalServiceUtil.
065                                    getAvailableResourcePermissionActionIds(
066                                            portletDataContext.getCompanyId(), resourceName,
067                                            ResourceConstants.SCOPE_INDIVIDUAL, resourcePrimKey,
068                                            actionIds);
069    
070                    for (Map.Entry<Long, Set<String>> entry : roleToActionIds.entrySet()) {
071                            long roleId = entry.getKey();
072    
073                            Role role = RoleLocalServiceUtil.fetchRole(roleId);
074    
075                            Element roleElement = permissionsElement.addElement("role");
076    
077                            roleElement.addAttribute("name", role.getName());
078                            roleElement.addAttribute("title", role.getTitle());
079                            roleElement.addAttribute("description", role.getDescription());
080                            roleElement.addAttribute("type", String.valueOf(role.getType()));
081                            roleElement.addAttribute("subtype", role.getSubtype());
082    
083                            Set<String> availableActionIds = entry.getValue();
084    
085                            for (String actionId : availableActionIds) {
086                                    Element actionKeyElement = roleElement.addElement("action-key");
087    
088                                    actionKeyElement.addText(actionId);
089                            }
090                    }
091            }
092    
093            protected void exportPortletDataPermissions(
094                            PortletDataContext portletDataContext)
095                    throws Exception {
096    
097                    Document document = SAXReaderUtil.createDocument();
098    
099                    Element rootElement = document.addElement("portlet-data-permissions");
100    
101                    Map<String, List<KeyValuePair>> permissionsMap =
102                            portletDataContext.getPermissions();
103    
104                    for (Map.Entry<String, List<KeyValuePair>> entry :
105                                    permissionsMap.entrySet()) {
106    
107                            String[] permissionParts = StringUtil.split(
108                                    entry.getKey(), CharPool.POUND);
109    
110                            String resourceName = permissionParts[0];
111                            long resourcePK = GetterUtil.getLong(permissionParts[1]);
112    
113                            Element portletDataElement = rootElement.addElement("portlet-data");
114    
115                            portletDataElement.addAttribute("resource-name", resourceName);
116                            portletDataElement.addAttribute(
117                                    "resource-pk", String.valueOf(resourcePK));
118    
119                            List<KeyValuePair> permissions = entry.getValue();
120    
121                            for (KeyValuePair permission : permissions) {
122                                    String roleName = permission.getKey();
123                                    String actions = permission.getValue();
124    
125                                    Element permissionsElement = portletDataElement.addElement(
126                                            "permissions");
127    
128                                    permissionsElement.addAttribute("role-name", roleName);
129                                    permissionsElement.addAttribute("actions", actions);
130                            }
131                    }
132    
133                    portletDataContext.addZipEntry(
134                            ExportImportPathUtil.getRootPath(portletDataContext) +
135                                    "/portlet-data-permissions.xml",
136                            document.formattedString());
137            }
138    
139            protected void exportPortletPermissions(
140                            PortletDataContext portletDataContext, String portletId,
141                            Layout layout, Element portletElement)
142                    throws Exception {
143    
144                    String resourceName = PortletConstants.getRootPortletId(portletId);
145                    String resourcePrimKey = PortletPermissionUtil.getPrimaryKey(
146                            layout.getPlid(), portletId);
147    
148                    Element permissionsElement = portletElement.addElement("permissions");
149    
150                    exportPermissions(
151                            portletDataContext, resourceName, resourcePrimKey,
152                            permissionsElement);
153            }
154    
155            private PermissionExporter() {
156            }
157    
158            private static final PermissionExporter _instance =
159                    new PermissionExporter();
160    
161    }