001
014
015 package com.liferay.portal.verify;
016
017 import com.liferay.portal.kernel.concurrent.ThrowableAwareRunnable;
018 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019 import com.liferay.portal.kernel.dao.shard.ShardUtil;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.model.Contact;
023 import com.liferay.portal.model.Layout;
024 import com.liferay.portal.model.LayoutSetBranch;
025 import com.liferay.portal.model.PasswordPolicy;
026 import com.liferay.portal.model.ResourceConstants;
027 import com.liferay.portal.model.ResourcePermission;
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.model.User;
032 import com.liferay.portal.service.ContactLocalServiceUtil;
033 import com.liferay.portal.service.LayoutLocalServiceUtil;
034 import com.liferay.portal.service.ResourceLocalServiceUtil;
035 import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
036 import com.liferay.portal.service.RoleLocalServiceUtil;
037 import com.liferay.portal.service.UserLocalServiceUtil;
038 import com.liferay.portal.util.PortalInstances;
039 import com.liferay.portlet.announcements.model.AnnouncementsEntry;
040 import com.liferay.portlet.asset.model.AssetCategory;
041 import com.liferay.portlet.asset.model.AssetTag;
042 import com.liferay.portlet.asset.model.AssetVocabulary;
043 import com.liferay.portlet.blogs.model.BlogsEntry;
044 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
045 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
046 import com.liferay.portlet.documentlibrary.model.DLFolder;
047 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
048 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
049 import com.liferay.portlet.journal.model.JournalArticle;
050 import com.liferay.portlet.journal.model.JournalFeed;
051 import com.liferay.portlet.messageboards.model.MBCategory;
052 import com.liferay.portlet.messageboards.model.MBMessage;
053 import com.liferay.portlet.polls.model.PollsQuestion;
054 import com.liferay.portlet.shopping.model.ShoppingCategory;
055 import com.liferay.portlet.shopping.model.ShoppingItem;
056 import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
057 import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
058 import com.liferay.portlet.wiki.model.WikiNode;
059 import com.liferay.portlet.wiki.model.WikiPage;
060
061 import java.sql.Connection;
062 import java.sql.PreparedStatement;
063 import java.sql.ResultSet;
064
065 import java.util.ArrayList;
066 import java.util.List;
067
068
072 public class VerifyResourcePermissions extends VerifyProcess {
073
074 @Override
075 protected void doVerify() throws Exception {
076 long[] companyIds = PortalInstances.getCompanyIdsBySQL();
077
078 for (long companyId : companyIds) {
079 Role role = RoleLocalServiceUtil.getRole(
080 companyId, RoleConstants.OWNER);
081
082 List<VerifyResourcedModelRunnable> verifyResourcedModelRunnables =
083 new ArrayList<VerifyResourcedModelRunnable>(_MODELS.length);
084
085 for (String[] model : _MODELS) {
086 VerifyResourcedModelRunnable verifyResourcedModelRunnable =
087 new VerifyResourcedModelRunnable(
088 ShardUtil.getCurrentShardName(), role, model[0],
089 model[1], model[2]);
090
091 verifyResourcedModelRunnables.add(verifyResourcedModelRunnable);
092 }
093
094 doVerify(verifyResourcedModelRunnables);
095
096 verifyLayout(role);
097 }
098 }
099
100 protected void verifyLayout(Role role) throws Exception {
101 List<Layout> layouts = LayoutLocalServiceUtil.getNoPermissionLayouts(
102 role.getRoleId());
103
104 for (Layout layout : layouts) {
105 verifyModel(
106 role.getCompanyId(), Layout.class.getName(), layout.getPlid(),
107 role, 0);
108 }
109 }
110
111 protected void verifyModel(
112 long companyId, String name, long primKey, Role role, long ownerId)
113 throws Exception {
114
115 ResourcePermission resourcePermission =
116 ResourcePermissionLocalServiceUtil.fetchResourcePermission(
117 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
118 String.valueOf(primKey), role.getRoleId());
119
120 if (resourcePermission == null) {
121 if (_log.isDebugEnabled()) {
122 _log.debug(
123 "No resource found for {" + companyId + ", " + name + ", " +
124 ResourceConstants.SCOPE_INDIVIDUAL + ", " + primKey +
125 ", " + role.getRoleId() + "}");
126 }
127
128 ResourceLocalServiceUtil.addResources(
129 companyId, 0, ownerId, name, String.valueOf(primKey), false,
130 false, false);
131 }
132
133 if (resourcePermission == null) {
134 resourcePermission =
135 ResourcePermissionLocalServiceUtil.fetchResourcePermission(
136 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
137 String.valueOf(primKey), role.getRoleId());
138
139 if (resourcePermission == null) {
140 return;
141 }
142 }
143
144 if (name.equals(User.class.getName())) {
145 User user = UserLocalServiceUtil.fetchUserById(ownerId);
146
147 if (user == null) {
148 return;
149 }
150
151 Contact contact = ContactLocalServiceUtil.fetchContact(
152 user.getContactId());
153
154 if (contact == null) {
155 return;
156 }
157
158 ownerId = contact.getUserId();
159 }
160
161 if (ownerId != resourcePermission.getOwnerId()) {
162 resourcePermission.setOwnerId(ownerId);
163
164 ResourcePermissionLocalServiceUtil.updateResourcePermission(
165 resourcePermission);
166 }
167
168 if (_log.isInfoEnabled() &&
169 ((resourcePermission.getResourcePermissionId() % 100) == 0)) {
170
171 _log.info("Processed 100 resource permissions for " + name);
172 }
173 }
174
175 protected void verifyModel(
176 Role role, String name, String modelName, String pkColumnName)
177 throws Exception {
178
179 Connection con = null;
180 PreparedStatement ps = null;
181 ResultSet rs = null;
182
183 try {
184 con = DataAccess.getUpgradeOptimizedConnection();
185
186 ps = con.prepareStatement(
187 "select " + pkColumnName + ", userId AS ownerId " +
188 "from " + modelName + " where companyId = " +
189 role.getCompanyId());
190
191 rs = ps.executeQuery();
192
193 while (rs.next()) {
194 long primKey = rs.getLong(pkColumnName);
195 long ownerId = rs.getLong("ownerId");
196
197 verifyModel(role.getCompanyId(), name, primKey, role, ownerId);
198 }
199 }
200 finally {
201 DataAccess.cleanUp(con, ps, rs);
202 }
203 }
204
205 private static final String[][] _MODELS = new String[][] {
206 new String[] {
207 AnnouncementsEntry.class.getName(), "AnnouncementsEntry", "entryId"
208 },
209 new String[] {
210 AssetCategory.class.getName(), "AssetCategory", "categoryId"
211 },
212 new String[] {
213 AssetTag.class.getName(), "AssetTag", "tagId"
214 },
215 new String[] {
216 AssetVocabulary.class.getName(), "AssetVocabulary", "vocabularyId"
217 },
218 new String[] {
219 BlogsEntry.class.getName(), "BlogsEntry", "entryId"
220 },
221 new String[] {
222 DDMStructure.class.getName(), "DDMStructure", "structureId"
223 },
224 new String[] {
225 DDMTemplate.class.getName(), "DDMTemplate", "templateId"
226 },
227 new String[] {
228 DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId"
229 },
230 new String[] {
231 DLFileShortcut.class.getName(), "DLFileShortcut", "fileShortcutId"
232 },
233 new String[] {
234 DLFolder.class.getName(), "DLFolder", "folderId"
235 },
236 new String[] {
237 JournalArticle.class.getName(), "JournalArticle", "resourcePrimKey"
238 },
239 new String[] {
240 JournalFeed.class.getName(), "JournalFeed", "id_"
241 },
242 new String[] {
243 Layout.class.getName(), "Layout", "plid"
244 },
245 new String[] {
246 LayoutSetBranch.class.getName(), "LayoutSetBranch",
247 "layoutSetBranchId"
248 },
249 new String[] {
250 MBCategory.class.getName(), "MBCategory", "categoryId"
251 },
252 new String[] {
253 MBMessage.class.getName(), "MBMessage", "messageId"
254 },
255 new String[] {
256 PasswordPolicy.class.getName(), "PasswordPolicy", "passwordPolicyId"
257 },
258 new String[] {
259 PollsQuestion.class.getName(), "PollsQuestion", "questionId"
260 },
261 new String[] {
262 SCFrameworkVersion.class.getName(), "SCFrameworkVersion",
263 "frameworkVersionId"
264 },
265 new String[] {
266 SCProductEntry.class.getName(), "SCProductEntry", "productEntryId"
267 },
268 new String[] {
269 ShoppingCategory.class.getName(), "ShoppingCategory", "categoryId"
270 },
271 new String[] {
272 ShoppingItem.class.getName(), "ShoppingItem", "itemId"
273 },
274 new String[] {
275 Team.class.getName(), "Team", "teamId"
276 },
277 new String[] {
278 User.class.getName(), "User_", "userId"
279 },
280 new String[] {
281 WikiNode.class.getName(), "WikiNode", "nodeId"
282 },
283 new String[] {
284 WikiPage.class.getName(), "WikiPage", "resourcePrimKey"
285 }
286 };
287
288 private static Log _log = LogFactoryUtil.getLog(
289 VerifyResourcePermissions.class);
290
291 private class VerifyResourcedModelRunnable extends ThrowableAwareRunnable {
292
293 private VerifyResourcedModelRunnable(
294 String shardName, Role role, String name, String modelName,
295 String pkColumnName) {
296
297 super(shardName);
298
299 _modelName = modelName;
300 _name = name;
301 _pkColumnName = pkColumnName;
302 _role = role;
303 }
304
305 @Override
306 protected void doRun() throws Exception {
307 verifyModel(_role, _name, _modelName, _pkColumnName);
308 }
309
310 private final String _modelName;
311 private final String _name;
312 private final String _pkColumnName;
313 private final Role _role;
314
315 }
316
317 }