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.portal.lar;
016    
017    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.KeyValuePair;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.PortletConstants;
028    import com.liferay.portal.model.ResourceConstants;
029    import com.liferay.portal.model.Role;
030    import com.liferay.portal.security.permission.ResourceActionsUtil;
031    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
032    import com.liferay.portal.service.RoleLocalServiceUtil;
033    import com.liferay.portal.service.permission.PortletPermissionUtil;
034    
035    import java.util.List;
036    import java.util.Map;
037    import java.util.Set;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Joel Kozikowski
042     * @author Charles May
043     * @author Raymond Aug??
044     * @author Jorge Ferrer
045     * @author Bruno Farache
046     * @author Zsigmond Rab
047     * @author Douglas Wong
048     */
049    public class PermissionExporter {
050    
051            public static final String ROLE_TEAM_PREFIX = "ROLE_TEAM_,*";
052    
053            public static PermissionExporter getInstance() {
054                    return _instance;
055            }
056    
057            protected void exportPermissions(
058                            PortletDataContext portletDataContext, String resourceName,
059                            String resourcePrimKey, Element permissionsElement)
060                    throws Exception {
061    
062                    List<String> actionIds = ResourceActionsUtil.getPortletResourceActions(
063                            resourceName);
064    
065                    Map<Long, Set<String>> roleToActionIds =
066                            ResourcePermissionLocalServiceUtil.
067                                    getAvailableResourcePermissionActionIds(
068                                            portletDataContext.getCompanyId(), resourceName,
069                                            ResourceConstants.SCOPE_INDIVIDUAL, resourcePrimKey,
070                                            actionIds);
071    
072                    for (Map.Entry<Long, Set<String>> entry : roleToActionIds.entrySet()) {
073                            long roleId = entry.getKey();
074    
075                            Role role = RoleLocalServiceUtil.fetchRole(roleId);
076    
077                            Element roleElement = permissionsElement.addElement("role");
078    
079                            roleElement.addAttribute("name", role.getName());
080                            roleElement.addAttribute("title", role.getTitle());
081                            roleElement.addAttribute("description", role.getDescription());
082                            roleElement.addAttribute("type", String.valueOf(role.getType()));
083                            roleElement.addAttribute("subtype", role.getSubtype());
084    
085                            Set<String> availableActionIds = entry.getValue();
086    
087                            for (String actionId : availableActionIds) {
088                                    Element actionKeyElement = roleElement.addElement("action-key");
089    
090                                    actionKeyElement.addText(actionId);
091                            }
092                    }
093            }
094    
095            protected void exportPortletDataPermissions(
096                            PortletDataContext portletDataContext)
097                    throws Exception {
098    
099                    Document document = SAXReaderUtil.createDocument();
100    
101                    Element rootElement = document.addElement("portlet-data-permissions");
102    
103                    Map<String, List<KeyValuePair>> permissionsMap =
104                            portletDataContext.getPermissions();
105    
106                    for (Map.Entry<String, List<KeyValuePair>> entry :
107                                    permissionsMap.entrySet()) {
108    
109                            String[] permissionParts = StringUtil.split(
110                                    entry.getKey(), CharPool.POUND);
111    
112                            String resourceName = permissionParts[0];
113                            long resourcePK = GetterUtil.getLong(permissionParts[1]);
114    
115                            Element portletDataElement = rootElement.addElement("portlet-data");
116    
117                            portletDataElement.addAttribute("resource-name", resourceName);
118                            portletDataElement.addAttribute(
119                                    "resource-pk", String.valueOf(resourcePK));
120    
121                            List<KeyValuePair> permissions = entry.getValue();
122    
123                            for (KeyValuePair permission : permissions) {
124                                    String roleName = permission.getKey();
125                                    String actions = permission.getValue();
126    
127                                    Element permissionsElement = portletDataElement.addElement(
128                                            "permissions");
129    
130                                    permissionsElement.addAttribute("role-name", roleName);
131                                    permissionsElement.addAttribute("actions", actions);
132                            }
133                    }
134    
135                    portletDataContext.addZipEntry(
136                            ExportImportPathUtil.getRootPath(portletDataContext) +
137                                    "/portlet-data-permissions.xml",
138                            document.formattedString());
139            }
140    
141            protected void exportPortletPermissions(
142                            PortletDataContext portletDataContext, String portletId,
143                            Layout layout, Element portletElement)
144                    throws Exception {
145    
146                    String resourceName = PortletConstants.getRootPortletId(portletId);
147                    String resourcePrimKey = PortletPermissionUtil.getPrimaryKey(
148                            layout.getPlid(), portletId);
149    
150                    Element permissionsElement = portletElement.addElement("permissions");
151    
152                    exportPermissions(
153                            portletDataContext, resourceName, resourcePrimKey,
154                            permissionsElement);
155            }
156    
157            private PermissionExporter() {
158            }
159    
160            private static final PermissionExporter _instance =
161                    new PermissionExporter();
162    
163    }