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