1
22
23 package com.liferay.portal.upgrade.v4_4_0;
24
25 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.model.Group;
29 import com.liferay.portal.model.Location;
30 import com.liferay.portal.model.Organization;
31 import com.liferay.portal.model.ResourceConstants;
32 import com.liferay.portal.model.Role;
33 import com.liferay.portal.model.UserGroup;
34 import com.liferay.portal.upgrade.UpgradeException;
35 import com.liferay.portal.upgrade.UpgradeProcess;
36 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
37 import com.liferay.portlet.documentlibrary.model.DLFolder;
38 import com.liferay.portlet.imagegallery.model.IGFolder;
39 import com.liferay.portlet.messageboards.model.MBCategory;
40 import com.liferay.portlet.shopping.model.ShoppingCategory;
41
42 import java.sql.Connection;
43 import java.sql.PreparedStatement;
44 import java.sql.ResultSet;
45
46
54 public class UpgradePermission extends UpgradeProcess {
55
56 public void upgrade() throws UpgradeException {
57 _log.info("Upgrading");
58
59 try {
60 doUpgrade();
61 }
62 catch (Exception e) {
63 throw new UpgradeException(e);
64 }
65 }
66
67 protected void deletePermissionByActionIdAndResourceName(
68 String actionId, String resourceName)
69 throws Exception {
70
71 Connection con = null;
72 PreparedStatement ps = null;
73 ResultSet rs = null;
74
75 try {
76 con = DataAccess.getConnection();
77
78 ps = con.prepareStatement(_GET_PERMISSION_SQL);
79
80 ps.setString(1, actionId);
81 ps.setString(2, resourceName);
82
83 rs = ps.executeQuery();
84
85 while (rs.next()) {
86 long permissionId = rs.getLong("permissionId");
87
88 deletePermissionByPermissionId(permissionId);
89 }
90 }
91 finally {
92 DataAccess.cleanUp(con, ps, rs);
93 }
94 }
95
96 protected void deletePermissionByPermissionId(long permissionId)
97 throws Exception {
98
99 runSQL(
100 "delete from Permission_ where permissionId = " + permissionId);
101 runSQL(
102 "delete from Groups_Permissions where permissionId = " +
103 permissionId);
104 runSQL(
105 "delete from Roles_Permissions where permissionId = " +
106 permissionId);
107 runSQL(
108 "delete from Users_Permissions where permissionId = " +
109 permissionId);
110 }
111
112 protected void deletePermissionByResourceId(long resourceId)
113 throws Exception {
114
115 Connection con = null;
116 PreparedStatement ps = null;
117 ResultSet rs = null;
118
119 try {
120 con = DataAccess.getConnection();
121
122 ps = con.prepareStatement(
123 "select permissionId from Permission_ where resourceId = ?");
124
125 ps.setLong(1, resourceId);
126
127 rs = ps.executeQuery();
128
129 while (rs.next()) {
130 long permissionId = rs.getLong("permissionId");
131
132 deletePermissionByPermissionId(permissionId);
133 }
134 }
135 finally {
136 DataAccess.cleanUp(con, ps, rs);
137 }
138 }
139
140 protected void deleteResource(long codeId) throws Exception {
141 Connection con = null;
142 PreparedStatement ps = null;
143 ResultSet rs = null;
144
145 try {
146 con = DataAccess.getConnection();
147
148 ps = con.prepareStatement(
149 "select resourceId from Resource_ where codeId = ?");
150
151 ps.setLong(1, codeId);
152
153 rs = ps.executeQuery();
154
155 while (rs.next()) {
156 long resourceId = rs.getLong("resourceId");
157
158 deletePermissionByResourceId(resourceId);
159
160 runSQL(
161 "delete from Resource_ where resourceId = " + resourceId);
162 }
163 }
164 finally {
165 DataAccess.cleanUp(con, ps, rs);
166 }
167 }
168
169 protected void deleteResourceCode(String resourceName)
170 throws Exception {
171
172 Connection con = null;
173 PreparedStatement ps = null;
174 ResultSet rs = null;
175
176 try {
177 con = DataAccess.getConnection();
178
179 ps = con.prepareStatement(
180 "select codeId from ResourceCode where name = ?");
181
182 ps.setString(1, resourceName);
183
184 rs = ps.executeQuery();
185
186 while (rs.next()) {
187 long codeId = rs.getLong("codeId");
188
189 deleteResource(codeId);
190
191 runSQL(
192 "delete from ResourceCode where name = '" + resourceName +
193 "'");
194 }
195 }
196 finally {
197 DataAccess.cleanUp(con, ps, rs);
198 }
199 }
200
201 protected void deleteRolesPermissions(String roleName) throws Exception {
202 Connection con = null;
203 PreparedStatement ps = null;
204 ResultSet rs = null;
205
206 try {
207 con = DataAccess.getConnection();
208
209 ps = con.prepareStatement(_GET_ROLES_PERMISSIONS_SQL);
210
211 ps.setString(1, roleName);
212
213 rs = ps.executeQuery();
214
215 while (rs.next()) {
216 long roleId = rs.getLong("roleId");
217
218 runSQL(
219 "delete from Roles_Permissions where roleId = " + roleId);
220 }
221 }
222 finally {
223 DataAccess.cleanUp(con, ps, rs);
224 }
225 }
226
227 protected void deleteUsersPermissions(int scope) throws Exception {
228 Connection con = null;
229 PreparedStatement ps = null;
230 ResultSet rs = null;
231
232 try {
233 con = DataAccess.getConnection();
234
235 ps = con.prepareStatement(_GET_USERS_PERMISSIONS_SQL);
236
237 ps.setLong(1, scope);
238
239 rs = ps.executeQuery();
240
241 while (rs.next()) {
242 long permissionId = rs.getLong("permissionId");
243
244 runSQL(
245 "delete from Users_Permissions where permissionId = " +
246 permissionId);
247 }
248 }
249 finally {
250 DataAccess.cleanUp(con, ps, rs);
251 }
252 }
253
254 protected void doUpgrade() throws Exception {
255 runSQL("delete from OrgGroupPermission");
256
257 for (int i = 0; i < _DELETE_PERMISSIONS.length; i++) {
258 Object[] permission = _DELETE_PERMISSIONS[i];
259
260 String actionId = (String)permission[0];
261 String resourceName = ((Class<?>)permission[1]).getName();
262
263 deletePermissionByActionIdAndResourceName(actionId, resourceName);
264 }
265
266 for (int i = 0; i < _UPDATE_PERMISSIONS.length; i++) {
267 Object[] permission = _UPDATE_PERMISSIONS[i];
268
269 String oldActionId = (String)permission[0];
270 String newActionId = (String)permission[1];
271 String resourceName = ((Class<?>)permission[2]).getName();
272
273 updatePermission(oldActionId, newActionId, resourceName);
274 }
275
276 deleteResourceCode("com.liferay.portlet.blogs.model.BlogsCategory");
277
278 deleteRolesPermissions("Community Administrator");
279 deleteRolesPermissions("Community Owner");
280 deleteRolesPermissions("Organization Administrator");
281
282 deleteUsersPermissions(ResourceConstants.SCOPE_GROUP);
283 }
284
285 protected void updatePermission(
286 String oldActionId, String newActionId, String resourceName)
287 throws Exception {
288
289 Connection con = null;
290 PreparedStatement ps = null;
291 ResultSet rs = null;
292
293 try {
294 con = DataAccess.getConnection();
295
296 ps = con.prepareStatement(_GET_PERMISSION_SQL);
297
298 ps.setString(1, oldActionId);
299 ps.setString(2, resourceName);
300
301 rs = ps.executeQuery();
302
303 while (rs.next()) {
304 long permissionId = rs.getLong("permissionId");
305
306 runSQL(
307 "update Permission_ set actionId = '" + newActionId +
308 "' where permissionId = " + permissionId);
309 }
310 }
311 finally {
312 DataAccess.cleanUp(con, ps, rs);
313 }
314 }
315
316 private static final String _GET_PERMISSION_SQL =
317 "select Permission_.permissionId from Permission_ inner join " +
318 "Resource_ on Resource_.resourceId = Permission_.resourceId " +
319 "inner join ResourceCode on ResourceCode.codeId = " +
320 "Resource_.codeId where Permission_.actionId = ? and " +
321 "ResourceCode.name = ?";
322
323 private static final String _GET_ROLES_PERMISSIONS_SQL =
324 "select Roles_Permissions.roleId from Roles_Permissions inner join " +
325 "Role_ on Role_.roleId = Roles_Permissions.roleId where " +
326 "Role_.name = ?";
327
328 private static final String _GET_USERS_PERMISSIONS_SQL =
329 "select Users_Permissions.permissionId from Users_Permissions inner " +
330 "join Permission_ on Permission_.permissionId = " +
331 "Users_Permissions.permissionId inner join Resource_ on " +
332 "Resource_.resourceId = Permission_.resourceId inner " +
333 "join ResourceCode on ResourceCode.codeId = " +
334 "Resource_.codeId where ResourceCode.scope = ?";
335
336 private static Object[][] _DELETE_PERMISSIONS = new Object[][] {
337 new Object[] {
338 "ADMINISTRATE", Group.class
339 },
340 new Object[] {
341 "ADD_USER", Location.class
342 },
343 new Object[] {
344 "ADD_USER", Organization.class
345 },
346 new Object[] {
347 "DELETE_USER", Location.class
348 },
349 new Object[] {
350 "DELETE_USER", Organization.class
351 },
352 new Object[] {
353 "PERMISSIONS_USER", Location.class
354 },
355 new Object[] {
356 "PERMISSIONS_USER", Organization.class
357 },
358 new Object[] {
359 "UPDATE_USER", Location.class
360 },
361 new Object[] {
362 "UPDATE_USER", Organization.class
363 },
364 new Object[] {
365 "VIEW_USER", Location.class
366 },
367 new Object[] {
368 "VIEW_USER", Organization.class
369 }
370 };
371
372 private static Object[][] _UPDATE_PERMISSIONS = new Object[][] {
373 new Object[] {
374 "ADD_CATEGORY", "ADD_SUBCATEGORY", MBCategory.class
375 },
376 new Object[] {
377 "ADD_CATEGORY", "ADD_SUBCATEGORY", ShoppingCategory.class
378 },
379 new Object[] {
380 "ADD_FOLDER", "ADD_SUBFOLDER", DLFolder.class
381 },
382 new Object[] {
383 "ADD_FOLDER", "ADD_SUBFOLDER", IGFolder.class
384 },
385 new Object[] {
386 "ADD_FOLDER", "ADD_SUBFOLDER", BookmarksFolder.class
387 },
388 new Object[] {
389 "ADD_LOCATION", "MANAGE_SUBORGANIZATIONS", Organization.class
390 },
391 new Object[] {
392 "ADD_PERMISSIONS", "DEFINE_PERMISSIONS", Role.class
393 },
394 new Object[] {
395 "ADD_USER", "MANAGE_USERS", Location.class
396 },
397 new Object[] {
398 "ADD_USER", "MANAGE_USERS", Organization.class
399 },
400 new Object[] {
401 "ASSIGN_USERS", "ASSIGN_MEMBERS", Group.class
402 },
403 new Object[] {
404 "ASSIGN_USERS", "ASSIGN_MEMBERS", Role.class
405 },
406 new Object[] {
407 "ASSIGN_USERS", "ASSIGN_MEMBERS", UserGroup.class
408 }
409 };
410
411 private static Log _log = LogFactoryUtil.getLog(UpgradePermission.class);
412
413 }