001
014
015 package com.liferay.portal.upgrade.v4_4_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019 import com.liferay.portal.model.Group;
020 import com.liferay.portal.model.Location;
021 import com.liferay.portal.model.Organization;
022 import com.liferay.portal.model.ResourceConstants;
023 import com.liferay.portal.model.Role;
024 import com.liferay.portal.model.UserGroup;
025 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
026 import com.liferay.portlet.documentlibrary.model.DLFolder;
027 import com.liferay.portlet.imagegallery.model.IGFolder;
028 import com.liferay.portlet.messageboards.model.MBCategory;
029 import com.liferay.portlet.shopping.model.ShoppingCategory;
030
031 import java.sql.Connection;
032 import java.sql.PreparedStatement;
033 import java.sql.ResultSet;
034
035
039 public class UpgradePermission extends UpgradeProcess {
040
041 protected void deletePermissionByActionIdAndResourceName(
042 String actionId, String resourceName)
043 throws Exception {
044
045 Connection con = null;
046 PreparedStatement ps = null;
047 ResultSet rs = null;
048
049 try {
050 con = DataAccess.getConnection();
051
052 ps = con.prepareStatement(_GET_PERMISSION_IDS_1);
053
054 ps.setString(1, actionId);
055 ps.setString(2, resourceName);
056
057 rs = ps.executeQuery();
058
059 while (rs.next()) {
060 long permissionId = rs.getLong("permissionId");
061
062 deletePermissionByPermissionId(permissionId);
063 }
064 }
065 finally {
066 DataAccess.cleanUp(con, ps, rs);
067 }
068 }
069
070 protected void deletePermissionByPermissionId(long permissionId)
071 throws Exception {
072
073 runSQL(
074 "delete from Permission_ where permissionId = " + permissionId);
075 runSQL(
076 "delete from Groups_Permissions where permissionId = " +
077 permissionId);
078 runSQL(
079 "delete from Roles_Permissions where permissionId = " +
080 permissionId);
081 runSQL(
082 "delete from Users_Permissions where permissionId = " +
083 permissionId);
084 }
085
086 protected void deletePermissionByResourceId(long resourceId)
087 throws Exception {
088
089 Connection con = null;
090 PreparedStatement ps = null;
091 ResultSet rs = null;
092
093 try {
094 con = DataAccess.getConnection();
095
096 ps = con.prepareStatement(
097 "select permissionId from Permission_ where resourceId = ?");
098
099 ps.setLong(1, resourceId);
100
101 rs = ps.executeQuery();
102
103 while (rs.next()) {
104 long permissionId = rs.getLong("permissionId");
105
106 deletePermissionByPermissionId(permissionId);
107 }
108 }
109 finally {
110 DataAccess.cleanUp(con, ps, rs);
111 }
112 }
113
114 protected void deleteResource(long codeId) throws Exception {
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 resourceId from Resource_ where codeId = ?");
124
125 ps.setLong(1, codeId);
126
127 rs = ps.executeQuery();
128
129 while (rs.next()) {
130 long resourceId = rs.getLong("resourceId");
131
132 deletePermissionByResourceId(resourceId);
133
134 runSQL(
135 "delete from Resource_ where resourceId = " + resourceId);
136 }
137 }
138 finally {
139 DataAccess.cleanUp(con, ps, rs);
140 }
141 }
142
143 protected void deleteResourceCode(String resourceName)
144 throws Exception {
145
146 Connection con = null;
147 PreparedStatement ps = null;
148 ResultSet rs = null;
149
150 try {
151 con = DataAccess.getConnection();
152
153 ps = con.prepareStatement(
154 "select codeId from ResourceCode where name = ?");
155
156 ps.setString(1, resourceName);
157
158 rs = ps.executeQuery();
159
160 while (rs.next()) {
161 long codeId = rs.getLong("codeId");
162
163 deleteResource(codeId);
164
165 runSQL(
166 "delete from ResourceCode where name = '" + resourceName +
167 "'");
168 }
169 }
170 finally {
171 DataAccess.cleanUp(con, ps, rs);
172 }
173 }
174
175 protected void deleteRolesPermissions(String roleName) throws Exception {
176 Connection con = null;
177 PreparedStatement ps = null;
178 ResultSet rs = null;
179
180 try {
181 con = DataAccess.getConnection();
182
183 ps = con.prepareStatement(_GET_ROLE_IDS);
184
185 ps.setString(1, roleName);
186
187 rs = ps.executeQuery();
188
189 while (rs.next()) {
190 long roleId = rs.getLong("roleId");
191
192 runSQL(
193 "delete from Roles_Permissions where roleId = " + roleId);
194 }
195 }
196 finally {
197 DataAccess.cleanUp(con, ps, rs);
198 }
199 }
200
201 protected void deleteUsersPermissions(int scope) 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_PERMISSION_IDS_2);
210
211 ps.setLong(1, scope);
212
213 rs = ps.executeQuery();
214
215 while (rs.next()) {
216 long permissionId = rs.getLong("permissionId");
217
218 runSQL(
219 "delete from Users_Permissions where permissionId = " +
220 permissionId);
221 }
222 }
223 finally {
224 DataAccess.cleanUp(con, ps, rs);
225 }
226 }
227
228 protected void doUpgrade() throws Exception {
229 runSQL("delete from OrgGroupPermission");
230
231 for (int i = 0; i < _DELETE_PERMISSIONS.length; i++) {
232 Object[] permission = _DELETE_PERMISSIONS[i];
233
234 String actionId = (String)permission[0];
235 String resourceName = ((Class<?>)permission[1]).getName();
236
237 deletePermissionByActionIdAndResourceName(actionId, resourceName);
238 }
239
240 for (int i = 0; i < _UPDATE_PERMISSIONS.length; i++) {
241 Object[] permission = _UPDATE_PERMISSIONS[i];
242
243 String oldActionId = (String)permission[0];
244 String newActionId = (String)permission[1];
245 String resourceName = ((Class<?>)permission[2]).getName();
246
247 updatePermission(oldActionId, newActionId, resourceName);
248 }
249
250 deleteResourceCode("com.liferay.portlet.blogs.model.BlogsCategory");
251
252 deleteRolesPermissions("Community Administrator");
253 deleteRolesPermissions("Community Owner");
254 deleteRolesPermissions("Organization Administrator");
255
256 deleteUsersPermissions(ResourceConstants.SCOPE_GROUP);
257 }
258
259 protected void updatePermission(
260 String oldActionId, String newActionId, String resourceName)
261 throws Exception {
262
263 Connection con = null;
264 PreparedStatement ps = null;
265 ResultSet rs = null;
266
267 try {
268 con = DataAccess.getConnection();
269
270 ps = con.prepareStatement(_GET_PERMISSION_IDS_1);
271
272 ps.setString(1, oldActionId);
273 ps.setString(2, resourceName);
274
275 rs = ps.executeQuery();
276
277 while (rs.next()) {
278 long permissionId = rs.getLong("permissionId");
279
280 runSQL(
281 "update Permission_ set actionId = '" + newActionId +
282 "' where permissionId = " + permissionId);
283 }
284 }
285 finally {
286 DataAccess.cleanUp(con, ps, rs);
287 }
288 }
289
290 private static Object[][] _DELETE_PERMISSIONS = new Object[][] {
291 new Object[] {
292 "ADMINISTRATE", Group.class
293 },
294 new Object[] {
295 "ADD_USER", Location.class
296 },
297 new Object[] {
298 "ADD_USER", Organization.class
299 },
300 new Object[] {
301 "DELETE_USER", Location.class
302 },
303 new Object[] {
304 "DELETE_USER", Organization.class
305 },
306 new Object[] {
307 "PERMISSIONS_USER", Location.class
308 },
309 new Object[] {
310 "PERMISSIONS_USER", Organization.class
311 },
312 new Object[] {
313 "UPDATE_USER", Location.class
314 },
315 new Object[] {
316 "UPDATE_USER", Organization.class
317 },
318 new Object[] {
319 "VIEW_USER", Location.class
320 },
321 new Object[] {
322 "VIEW_USER", Organization.class
323 }
324 };
325
326 private static final String _GET_PERMISSION_IDS_1 =
327 "select Permission_.permissionId from Permission_ inner join " +
328 "Resource_ on Resource_.resourceId = Permission_.resourceId " +
329 "inner join ResourceCode on ResourceCode.codeId = " +
330 "Resource_.codeId where Permission_.actionId = ? and " +
331 "ResourceCode.name = ?";
332
333 private static final String _GET_PERMISSION_IDS_2 =
334 "select Users_Permissions.permissionId from Users_Permissions inner " +
335 "join Permission_ on Permission_.permissionId = " +
336 "Users_Permissions.permissionId inner join Resource_ on " +
337 "Resource_.resourceId = Permission_.resourceId inner " +
338 "join ResourceCode on ResourceCode.codeId = " +
339 "Resource_.codeId where ResourceCode.scope = ?";
340
341 private static final String _GET_ROLE_IDS =
342 "select Roles_Permissions.roleId from Roles_Permissions inner join " +
343 "Role_ on Role_.roleId = Roles_Permissions.roleId where " +
344 "Role_.name = ?";
345
346 private static Object[][] _UPDATE_PERMISSIONS = new Object[][] {
347 new Object[] {
348 "ADD_CATEGORY", "ADD_SUBCATEGORY", MBCategory.class
349 },
350 new Object[] {
351 "ADD_CATEGORY", "ADD_SUBCATEGORY", ShoppingCategory.class
352 },
353 new Object[] {
354 "ADD_FOLDER", "ADD_SUBFOLDER", DLFolder.class
355 },
356 new Object[] {
357 "ADD_FOLDER", "ADD_SUBFOLDER", IGFolder.class
358 },
359 new Object[] {
360 "ADD_FOLDER", "ADD_SUBFOLDER", BookmarksFolder.class
361 },
362 new Object[] {
363 "ADD_LOCATION", "MANAGE_SUBORGANIZATIONS", Organization.class
364 },
365 new Object[] {
366 "ADD_PERMISSIONS", "DEFINE_PERMISSIONS", Role.class
367 },
368 new Object[] {
369 "ADD_USER", "MANAGE_USERS", Location.class
370 },
371 new Object[] {
372 "ADD_USER", "MANAGE_USERS", Organization.class
373 },
374 new Object[] {
375 "ASSIGN_USERS", "ASSIGN_MEMBERS", Group.class
376 },
377 new Object[] {
378 "ASSIGN_USERS", "ASSIGN_MEMBERS", Role.class
379 },
380 new Object[] {
381 "ASSIGN_USERS", "ASSIGN_MEMBERS", UserGroup.class
382 }
383 };
384
385 }