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