001    /**
002     * Copyright (c) 2000-2013 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.NoSuchTeamException;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.KeyValuePair;
022    import com.liferay.portal.kernel.util.LocalizationUtil;
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.model.RoleConstants;
031    import com.liferay.portal.model.Team;
032    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
033    import com.liferay.portal.service.RoleLocalServiceUtil;
034    import com.liferay.portal.service.TeamLocalServiceUtil;
035    import com.liferay.portal.service.permission.PortletPermissionUtil;
036    
037    import java.util.ArrayList;
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Locale;
041    import java.util.Map;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Joel Kozikowski
046     * @author Charles May
047     * @author Raymond Augé
048     * @author Jorge Ferrer
049     * @author Bruno Farache
050     * @author Wesley Gong
051     * @author Zsigmond Rab
052     * @author Douglas Wong
053     */
054    public class PermissionImporter {
055    
056            protected List<String> getActions(Element element) {
057                    List<String> actions = new ArrayList<String>();
058    
059                    List<Element> actionKeyElements = element.elements("action-key");
060    
061                    for (Element actionKeyElement : actionKeyElements) {
062                            actions.add(actionKeyElement.getText());
063                    }
064    
065                    return actions;
066            }
067    
068            protected void importLayoutPermissions(
069                            LayoutCache layoutCache, long companyId, long groupId, long userId,
070                            Layout layout, Element layoutElement, Element parentElement)
071                    throws Exception {
072    
073                    Element permissionsElement = layoutElement.element("permissions");
074    
075                    if (permissionsElement != null) {
076                            String resourceName = Layout.class.getName();
077                            String resourcePrimKey = String.valueOf(layout.getPlid());
078    
079                            importPermissions(
080                                    layoutCache, companyId, groupId, userId, layout, resourceName,
081                                    resourcePrimKey, permissionsElement, false);
082                    }
083            }
084    
085            protected void importPermissions(
086                            LayoutCache layoutCache, long companyId, long groupId, long userId,
087                            Layout layout, String resourceName, String resourcePrimKey,
088                            Element permissionsElement, boolean portletActions)
089                    throws Exception {
090    
091                    Map<Long, String[]> roleIdsToActionIds = new HashMap<Long, String[]>();
092    
093                    List<Element> roleElements = permissionsElement.elements("role");
094    
095                    for (Element roleElement : roleElements) {
096                            String name = roleElement.attributeValue("name");
097    
098                            Role role = null;
099    
100                            if (name.startsWith(PermissionExporter.ROLE_TEAM_PREFIX)) {
101                                    name = name.substring(
102                                            PermissionExporter.ROLE_TEAM_PREFIX.length());
103    
104                                    String description = roleElement.attributeValue("description");
105    
106                                    Team team = null;
107    
108                                    try {
109                                            team = TeamLocalServiceUtil.getTeam(groupId, name);
110                                    }
111                                    catch (NoSuchTeamException nste) {
112                                            team = TeamLocalServiceUtil.addTeam(
113                                                    userId, groupId, name, description);
114                                    }
115    
116                                    role = RoleLocalServiceUtil.getTeamRole(
117                                            companyId, team.getTeamId());
118                            }
119                            else {
120                                    role = layoutCache.getRole(companyId, name);
121                            }
122    
123                            if (role == null) {
124                                    String title = roleElement.attributeValue("title");
125    
126                                    Map<Locale, String> titleMap =
127                                            LocalizationUtil.getLocalizationMap(title);
128    
129                                    String description = roleElement.attributeValue("description");
130    
131                                    Map<Locale, String> descriptionMap =
132                                            LocalizationUtil.getLocalizationMap(description);
133    
134                                    int type = GetterUtil.getInteger(
135                                            roleElement.attributeValue("type"));
136                                    String subType = roleElement.attributeValue("subType");
137    
138                                    role = RoleLocalServiceUtil.addRole(
139                                            userId, null, 0, name, titleMap, descriptionMap, type,
140                                            subType, null);
141                            }
142    
143                            String roleName = role.getName();
144    
145                            if (!layout.isPrivateLayout() ||
146                                    !roleName.equals(RoleConstants.GUEST)) {
147    
148                                    List<String> actions = getActions(roleElement);
149    
150                                    roleIdsToActionIds.put(
151                                            role.getRoleId(),
152                                            actions.toArray(new String[actions.size()]));
153                            }
154                    }
155    
156                    if (roleIdsToActionIds.isEmpty()) {
157                            return;
158                    }
159    
160                    ResourcePermissionLocalServiceUtil.setResourcePermissions(
161                            companyId, resourceName, ResourceConstants.SCOPE_INDIVIDUAL,
162                            resourcePrimKey, roleIdsToActionIds);
163            }
164    
165            protected void importPortletPermissions(
166                            LayoutCache layoutCache, long companyId, long groupId, long userId,
167                            Layout layout, Element portletElement, String portletId)
168                    throws Exception {
169    
170                    Element permissionsElement = portletElement.element("permissions");
171    
172                    if (permissionsElement != null) {
173                            String resourceName = PortletConstants.getRootPortletId(portletId);
174    
175                            String resourcePrimKey = PortletPermissionUtil.getPrimaryKey(
176                                    layout.getPlid(), portletId);
177    
178                            importPermissions(
179                                    layoutCache, companyId, groupId, userId, layout, resourceName,
180                                    resourcePrimKey, permissionsElement, true);
181                    }
182            }
183    
184            protected void readPortletDataPermissions(
185                            PortletDataContext portletDataContext)
186                    throws Exception {
187    
188                    String xml = portletDataContext.getZipEntryAsString(
189                            ExportImportPathUtil.getSourceRootPath(portletDataContext) +
190                                    "/portlet-data-permissions.xml");
191    
192                    if (xml == null) {
193                            return;
194                    }
195    
196                    Document document = SAXReaderUtil.read(xml);
197    
198                    Element rootElement = document.getRootElement();
199    
200                    List<Element> portletDataElements = rootElement.elements(
201                            "portlet-data");
202    
203                    for (Element portletDataElement : portletDataElements) {
204                            String resourceName = portletDataElement.attributeValue(
205                                    "resource-name");
206                            long resourcePK = GetterUtil.getLong(
207                                    portletDataElement.attributeValue("resource-pk"));
208    
209                            List<KeyValuePair> permissions = new ArrayList<KeyValuePair>();
210    
211                            List<Element> permissionsElements = portletDataElement.elements(
212                                    "permissions");
213    
214                            for (Element permissionsElement : permissionsElements) {
215                                    String roleName = permissionsElement.attributeValue(
216                                            "role-name");
217                                    String actions = permissionsElement.attributeValue("actions");
218    
219                                    KeyValuePair permission = new KeyValuePair(roleName, actions);
220    
221                                    permissions.add(permission);
222                            }
223    
224                            portletDataContext.addPermissions(
225                                    resourceName, resourcePK, permissions);
226                    }
227            }
228    
229    }