001
014
015 package com.liferay.portal.upgrade.v6_0_3;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.StringBundler;
021 import com.liferay.portal.model.Company;
022 import com.liferay.portal.model.Group;
023 import com.liferay.portal.model.Organization;
024 import com.liferay.portal.model.Role;
025 import com.liferay.portal.model.RoleConstants;
026 import com.liferay.portal.util.PortalInstances;
027 import com.liferay.portal.util.PortalUtil;
028
029 import java.sql.Connection;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032
033
036 public class UpgradePermission extends UpgradeProcess {
037
038 protected void addRole(
039 long roleId, long companyId, long classNameId, long classPK,
040 String name, int type)
041 throws Exception {
042
043 Connection con = null;
044 PreparedStatement ps = null;
045
046 try {
047 con = DataAccess.getUpgradeOptimizedConnection();
048
049 ps = con.prepareStatement(
050 "insert into Role_ (roleId, companyId, classNameId, classPK, " +
051 "name, type_) values (?, ?, ?, ?, ?, ?)");
052
053 ps.setLong(1, roleId);
054 ps.setLong(2, companyId);
055 ps.setLong(3, classNameId);
056 ps.setLong(4, classPK);
057 ps.setString(5, name);
058 ps.setInt(6, type);
059
060 ps.executeUpdate();
061 }
062 finally {
063 DataAccess.cleanUp(con, ps);
064 }
065 }
066
067 protected void addSingleApproverWorkflowRoles() throws Exception {
068 long[] companyIds = PortalInstances.getCompanyIdsBySQL();
069
070 for (long companyId : companyIds) {
071 addSingleApproverWorkflowRoles(companyId);
072 }
073 }
074
075 protected void addSingleApproverWorkflowRoles(long companyId)
076 throws Exception {
077
078 long classNameId = PortalUtil.getClassNameId(Role.class.getName());
079 long roleId = increment();
080
081 addRole(
082 roleId, companyId, classNameId, roleId,
083 _ROLE_COMMUNITY_CONTENT_REVIEWER, RoleConstants.TYPE_SITE);
084
085 classNameId = PortalUtil.getClassNameId(Organization.class.getName());
086 roleId = increment();
087
088 addRole(
089 roleId, companyId, classNameId, roleId,
090 _ROLE_ORGANIZATION_CONTENT_REVIEWER,
091 RoleConstants.TYPE_ORGANIZATION);
092
093 classNameId = PortalUtil.getClassNameId(Company.class.getName());
094 roleId = increment();
095
096 addRole(
097 roleId, companyId, classNameId, roleId,
098 _ROLE_PORTAL_CONTENT_REVIEWER, RoleConstants.TYPE_REGULAR);
099 }
100
101 protected void addUserGroupRole(long userId, long groupId, long roleId)
102 throws Exception {
103
104 if (hasUserGroupRole(userId, groupId, roleId)) {
105 return;
106 }
107
108 Connection con = null;
109 PreparedStatement ps = null;
110
111 try {
112 con = DataAccess.getUpgradeOptimizedConnection();
113
114 ps = con.prepareStatement(
115 "insert into UserGroupRole (userId, groupId, roleId) values " +
116 "(?, ?, ?)");
117
118 ps.setLong(1, userId);
119 ps.setLong(2, groupId);
120 ps.setLong(3, roleId);
121
122 ps.executeUpdate();
123 }
124 finally {
125 DataAccess.cleanUp(con, ps);
126 }
127 }
128
129 protected void addUserRole(long userId, long roleId) throws Exception {
130 if (hasUserRole(userId, roleId)) {
131 return;
132 }
133
134 Connection con = null;
135 PreparedStatement ps = null;
136
137 try {
138 con = DataAccess.getUpgradeOptimizedConnection();
139
140 ps = con.prepareStatement(
141 "insert into Users_Roles (userId, roleId) values (?, ?)");
142
143 ps.setLong(1, userId);
144 ps.setLong(2, roleId);
145
146 ps.executeUpdate();
147 }
148 finally {
149 DataAccess.cleanUp(con, ps);
150 }
151 }
152
153 protected void assignSingleApproverWorkflowRoles(
154 long companyId, long roleId, long groupId)
155 throws Exception {
156
157 Connection con = null;
158 PreparedStatement ps = null;
159 ResultSet rs = null;
160
161 try {
162 con = DataAccess.getUpgradeOptimizedConnection();
163
164 ps = con.prepareStatement(
165 "select classNameId from Group_ where groupId = ?");
166
167 ps.setLong(1, groupId);
168
169 rs = ps.executeQuery();
170
171 long classNameId = 0;
172
173 if (rs.next()) {
174 classNameId = rs.getLong("classNameId");
175 }
176
177 String className = PortalUtil.getClassName(classNameId);
178
179 long communityContentReviewerRoleId = getRoleId(
180 companyId, _ROLE_COMMUNITY_CONTENT_REVIEWER);
181 long organizationContentReviewerRoleId = getRoleId(
182 companyId, _ROLE_ORGANIZATION_CONTENT_REVIEWER);
183 long portalContentReviewerRoleId = getRoleId(
184 companyId, _ROLE_PORTAL_CONTENT_REVIEWER);
185
186 StringBundler sb = new StringBundler();
187
188 sb.append("(select User_.* from User_, Users_Roles where ");
189 sb.append("User_.userId = Users_Roles.userId and ");
190 sb.append("Users_Roles.roleId = ?) union all (select User_.* ");
191 sb.append("from User_, UserGroupRole where User_.userId = ");
192 sb.append("UserGroupRole.userId and UserGroupRole.roleId = ?)");
193
194 ps = con.prepareStatement(sb.toString());
195
196 ps.setLong(1, roleId);
197 ps.setLong(2, roleId);
198
199 rs = ps.executeQuery();
200
201 while (rs.next()) {
202 long userId = rs.getLong("userId");
203
204 if (className.equals(Company.class.getName())) {
205 addUserRole(userId, portalContentReviewerRoleId);
206 }
207 else if (className.equals(Group.class.getName())) {
208 addUserGroupRole(
209 userId, groupId, communityContentReviewerRoleId);
210 }
211 else if (className.equals(Organization.class.getName())) {
212 addUserGroupRole(
213 userId, groupId, organizationContentReviewerRoleId);
214 }
215 }
216 }
217 finally {
218 DataAccess.cleanUp(con, ps, rs);
219 }
220 }
221
222 @Override
223 protected void doUpgrade() throws Exception {
224 addSingleApproverWorkflowRoles();
225
226 updatePermissions();
227 }
228
229 protected long getRoleId(long companyId, String name) throws Exception {
230 Connection con = null;
231 PreparedStatement ps = null;
232 ResultSet rs = null;
233
234 try {
235 con = DataAccess.getUpgradeOptimizedConnection();
236
237 ps = con.prepareStatement(
238 "select roleId from Role_ where companyId = ? and name = ?");
239
240 ps.setLong(1, companyId);
241 ps.setString(2, name);
242
243 rs = ps.executeQuery();
244
245 if (rs.next()) {
246 return rs.getLong("roleId");
247 }
248
249 return 0;
250 }
251 finally {
252 DataAccess.cleanUp(con, ps, rs);
253 }
254 }
255
256 protected boolean hasUserGroupRole(long userId, long groupId, long roleId)
257 throws Exception {
258
259 Connection con = null;
260 PreparedStatement ps = null;
261 ResultSet rs = null;
262
263 try {
264 con = DataAccess.getUpgradeOptimizedConnection();
265
266 ps = con.prepareStatement(
267 "select count(*) from UserGroupRole where userId = ? and " +
268 "groupId = ? and roleId = ?");
269
270 ps.setLong(1, userId);
271 ps.setLong(2, groupId);
272 ps.setLong(3, roleId);
273
274 rs = ps.executeQuery();
275
276 if (rs.next()) {
277 int count = rs.getInt(1);
278
279 if (count > 0) {
280 return true;
281 }
282 }
283
284 return false;
285 }
286 finally {
287 DataAccess.cleanUp(con, ps, rs);
288 }
289 }
290
291 protected boolean hasUserRole(long userId, long roleId) throws Exception {
292 Connection con = null;
293 PreparedStatement ps = null;
294 ResultSet rs = null;
295
296 try {
297 con = DataAccess.getUpgradeOptimizedConnection();
298
299 ps = con.prepareStatement(
300 "select count(*) from Users_Roles where userId = ? and " +
301 "roleId = ?");
302
303 ps.setLong(1, userId);
304 ps.setLong(2, roleId);
305
306 rs = ps.executeQuery();
307
308 if (rs.next()) {
309 int count = rs.getInt(1);
310
311 if (count > 0) {
312 return true;
313 }
314 }
315
316 return false;
317 }
318 finally {
319 DataAccess.cleanUp(con, ps, rs);
320 }
321 }
322
323 protected void updatePermissions() throws Exception {
324 Connection con = null;
325 PreparedStatement ps = null;
326 ResultSet rs = null;
327
328 try {
329 con = DataAccess.getUpgradeOptimizedConnection();
330
331 StringBundler sb = new StringBundler();
332
333 sb.append("select ResourcePermission.companyId, ");
334 sb.append("ResourcePermission.roleId, ResourcePermission.primKey ");
335 sb.append("from ResourcePermission, ResourceAction where ");
336 sb.append("ResourceAction.name = 'com.liferay.portlet.journal' ");
337 sb.append("and ResourceAction.name = ResourcePermission.name and ");
338 sb.append("ResourceAction.actionId = 'APPROVE_ARTICLE' and ");
339 sb.append("ResourcePermission.scope = 4 and ");
340 sb.append("ResourcePermission.actionIds >= ");
341 sb.append("ResourceAction.bitwiseValue and ");
342 sb.append("mod((ResourcePermission.actionIds / ");
343 sb.append("ResourceAction.bitwiseValue), 2) = 1");
344
345 ps = con.prepareStatement(sb.toString());
346
347 rs = ps.executeQuery();
348
349 while (rs.next()) {
350 long companyId = rs.getLong("companyId");
351 long roleId = rs.getLong("roleId");
352 long groupId = GetterUtil.getLong(rs.getString("primKey"));
353
354 assignSingleApproverWorkflowRoles(companyId, roleId, groupId);
355 }
356 }
357 finally {
358 DataAccess.cleanUp(con, ps, rs);
359 }
360 }
361
362 private static final String _ROLE_COMMUNITY_CONTENT_REVIEWER =
363 "Community Content Reviewer";
364
365 private static final String _ROLE_ORGANIZATION_CONTENT_REVIEWER =
366 "Organization Content Reviewer";
367
368 private static final String _ROLE_PORTAL_CONTENT_REVIEWER =
369 "Portal Content Reviewer";
370
371 }