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