001
014
015 package com.liferay.portal.verify;
016
017 import com.liferay.portal.NoSuchResourcePermissionException;
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.List;
065
066
070 public class VerifyResourcePermissions extends VerifyProcess {
071
072 @Override
073 protected void doVerify() throws Exception {
074 long[] companyIds = PortalInstances.getCompanyIdsBySQL();
075
076 for (long companyId : companyIds) {
077 Role role = RoleLocalServiceUtil.getRole(
078 companyId, RoleConstants.OWNER);
079
080 for (String[] model : _MODELS) {
081 verifyModel(role, model[0], model[1], model[2]);
082 }
083
084 verifyLayout(role);
085 }
086 }
087
088 protected void verifyLayout(Role role) throws Exception {
089 List<Layout> layouts = LayoutLocalServiceUtil.getNoPermissionLayouts(
090 role.getRoleId());
091
092 for (Layout layout : layouts) {
093 verifyModel(
094 role.getCompanyId(), Layout.class.getName(), layout.getPlid(),
095 role, 0);
096 }
097 }
098
099 protected void verifyModel(
100 long companyId, String name, long primKey, Role role, long ownerId)
101 throws Exception {
102
103 ResourcePermission resourcePermission = null;
104
105 try {
106 resourcePermission =
107 ResourcePermissionLocalServiceUtil.getResourcePermission(
108 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
109 String.valueOf(primKey), role.getRoleId());
110 }
111 catch (NoSuchResourcePermissionException nsrpe) {
112 if (_log.isDebugEnabled()) {
113 _log.debug(
114 "No resource found for {" + companyId + ", " + name + ", " +
115 ResourceConstants.SCOPE_INDIVIDUAL + ", " + primKey +
116 ", " + role.getRoleId() + "}");
117 }
118
119 ResourceLocalServiceUtil.addResources(
120 companyId, 0, ownerId, name, String.valueOf(primKey), false,
121 false, false);
122 }
123
124 if (resourcePermission == null) {
125 try {
126 resourcePermission =
127 ResourcePermissionLocalServiceUtil.getResourcePermission(
128 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
129 String.valueOf(primKey), role.getRoleId());
130 }
131 catch (NoSuchResourcePermissionException nsrpe) {
132 return;
133 }
134 }
135
136 if (name.equals(User.class.getName())) {
137 User user = UserLocalServiceUtil.getUserById(ownerId);
138
139 Contact contact = ContactLocalServiceUtil.getContact(
140 user.getContactId());
141
142 ownerId = contact.getUserId();
143 }
144
145 if (ownerId != resourcePermission.getOwnerId()) {
146 resourcePermission.setOwnerId(ownerId);
147
148 ResourcePermissionLocalServiceUtil.updateResourcePermission(
149 resourcePermission);
150 }
151
152 if (_log.isInfoEnabled() &&
153 ((resourcePermission.getResourcePermissionId() % 100) == 0)) {
154
155 _log.info("Processed 100 resource permissions for " + name);
156 }
157 }
158
159 protected void verifyModel(
160 Role role, String name, String modelName, String pkColumnName)
161 throws Exception {
162
163 Connection con = null;
164 PreparedStatement ps = null;
165 ResultSet rs = null;
166
167 try {
168 con = DataAccess.getUpgradeOptimizedConnection();
169
170 ps = con.prepareStatement(
171 "select " + pkColumnName + ", userId AS ownerId " +
172 "from " + modelName + " where companyId = " +
173 role.getCompanyId());
174
175 rs = ps.executeQuery();
176
177 while (rs.next()) {
178 long primKey = rs.getLong(pkColumnName);
179 long ownerId = rs.getLong("ownerId");
180
181 verifyModel(role.getCompanyId(), name, primKey, role, ownerId);
182 }
183 }
184 finally {
185 DataAccess.cleanUp(con, ps, rs);
186 }
187 }
188
189 private static final String[][] _MODELS = new String[][] {
190 new String[] {
191 AnnouncementsEntry.class.getName(), "AnnouncementsEntry", "entryId"
192 },
193 new String[] {
194 AssetCategory.class.getName(), "AssetCategory", "categoryId"
195 },
196 new String[] {
197 AssetTag.class.getName(), "AssetTag", "tagId"
198 },
199 new String[] {
200 AssetVocabulary.class.getName(), "AssetVocabulary", "vocabularyId"
201 },
202 new String[] {
203 BlogsEntry.class.getName(), "BlogsEntry", "entryId"
204 },
205 new String[] {
206 DDMStructure.class.getName(), "DDMStructure", "structureId"
207 },
208 new String[] {
209 DDMTemplate.class.getName(), "DDMTemplate", "templateId"
210 },
211 new String[] {
212 DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId"
213 },
214 new String[] {
215 DLFileShortcut.class.getName(), "DLFileShortcut", "fileShortcutId"
216 },
217 new String[] {
218 DLFolder.class.getName(), "DLFolder", "folderId"
219 },
220 new String[] {
221 JournalArticle.class.getName(), "JournalArticle", "resourcePrimKey"
222 },
223 new String[] {
224 JournalFeed.class.getName(), "JournalFeed", "id_"
225 },
226 new String[] {
227 Layout.class.getName(), "Layout", "plid"
228 },
229 new String[] {
230 LayoutSetBranch.class.getName(), "LayoutSetBranch",
231 "layoutSetBranchId"
232 },
233 new String[] {
234 MBCategory.class.getName(), "MBCategory", "categoryId"
235 },
236 new String[] {
237 MBMessage.class.getName(), "MBMessage", "messageId"
238 },
239 new String[] {
240 PasswordPolicy.class.getName(), "PasswordPolicy", "passwordPolicyId"
241 },
242 new String[] {
243 PollsQuestion.class.getName(), "PollsQuestion", "questionId"
244 },
245 new String[] {
246 SCFrameworkVersion.class.getName(), "SCFrameworkVersion",
247 "frameworkVersionId"
248 },
249 new String[] {
250 SCProductEntry.class.getName(), "SCProductEntry", "productEntryId"
251 },
252 new String[] {
253 ShoppingCategory.class.getName(), "ShoppingCategory", "categoryId"
254 },
255 new String[] {
256 ShoppingItem.class.getName(), "ShoppingItem", "itemId"
257 },
258 new String[] {
259 Team.class.getName(), "Team", "teamId"
260 },
261 new String[] {
262 User.class.getName(), "User_", "userId"
263 },
264 new String[] {
265 WikiNode.class.getName(), "WikiNode", "nodeId"
266 },
267 new String[] {
268 WikiPage.class.getName(), "WikiPage", "resourcePrimKey"
269 }
270 };
271
272 private static Log _log = LogFactoryUtil.getLog(
273 VerifyResourcePermissions.class);
274
275 }