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