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