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