001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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.LayoutSetBranch;
023    import com.liferay.portal.model.PasswordPolicy;
024    import com.liferay.portal.model.ResourceConstants;
025    import com.liferay.portal.model.ResourcePermission;
026    import com.liferay.portal.model.Role;
027    import com.liferay.portal.model.RoleConstants;
028    import com.liferay.portal.model.Team;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.service.ContactLocalServiceUtil;
031    import com.liferay.portal.service.ResourceLocalServiceUtil;
032    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
033    import com.liferay.portal.service.RoleLocalServiceUtil;
034    import com.liferay.portal.service.UserLocalServiceUtil;
035    import com.liferay.portal.util.PortalInstances;
036    import com.liferay.portal.util.PropsValues;
037    import com.liferay.portlet.announcements.model.AnnouncementsEntry;
038    import com.liferay.portlet.asset.model.AssetCategory;
039    import com.liferay.portlet.asset.model.AssetTag;
040    import com.liferay.portlet.asset.model.AssetVocabulary;
041    import com.liferay.portlet.blogs.model.BlogsEntry;
042    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
043    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
044    import com.liferay.portlet.calendar.model.CalEvent;
045    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
046    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
047    import com.liferay.portlet.documentlibrary.model.DLFolder;
048    import com.liferay.portlet.dynamicdatamapping.model.DDMContent;
049    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
050    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
051    import com.liferay.portlet.journal.model.JournalArticle;
052    import com.liferay.portlet.journal.model.JournalFeed;
053    import com.liferay.portlet.journal.model.JournalStructure;
054    import com.liferay.portlet.journal.model.JournalTemplate;
055    import com.liferay.portlet.messageboards.model.MBCategory;
056    import com.liferay.portlet.messageboards.model.MBMessage;
057    import com.liferay.portlet.polls.model.PollsQuestion;
058    import com.liferay.portlet.shopping.model.ShoppingCategory;
059    import com.liferay.portlet.shopping.model.ShoppingItem;
060    import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
061    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
062    import com.liferay.portlet.wiki.model.WikiNode;
063    import com.liferay.portlet.wiki.model.WikiPage;
064    
065    import java.sql.Connection;
066    import java.sql.PreparedStatement;
067    import java.sql.ResultSet;
068    
069    /**
070     * @author Raymond Augé
071     */
072    public class VerifyResourcePermissions extends VerifyProcess {
073    
074            @Override
075            protected void doVerify() throws Exception {
076                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
077                            return;
078                    }
079    
080                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
081    
082                    for (long companyId : companyIds) {
083                            Role role = RoleLocalServiceUtil.getRole(
084                                    companyId, RoleConstants.OWNER);
085    
086                            for (String[] model : _MODELS) {
087                                    verifyModel(role, model[0], model[1], model[2]);
088                            }
089                    }
090            }
091    
092            protected void verifyModel(
093                            long companyId, String name, long primKey, Role role, long ownerId)
094                    throws Exception {
095    
096                    ResourcePermission resourcePermission = null;
097    
098                    try {
099                            resourcePermission =
100                                    ResourcePermissionLocalServiceUtil.getResourcePermission(
101                                            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
102                                            String.valueOf(primKey), role.getRoleId());
103                    }
104                    catch (NoSuchResourcePermissionException nsrpe) {
105                            if (_log.isDebugEnabled()) {
106                                    _log.debug(
107                                            "No resource found for {" + companyId + ", " + name + ", " +
108                                                    ResourceConstants.SCOPE_INDIVIDUAL + ", " + primKey +
109                                                            ", " + role.getRoleId() + "}");
110                            }
111    
112                            ResourceLocalServiceUtil.addResources(
113                                    companyId, 0, ownerId, name, String.valueOf(primKey), false,
114                                    false, false);
115                    }
116    
117                    if (resourcePermission == null) {
118                            try {
119                                    resourcePermission =
120                                            ResourcePermissionLocalServiceUtil.getResourcePermission(
121                                                    companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
122                                                    String.valueOf(primKey), role.getRoleId());
123                            }
124                            catch (NoSuchResourcePermissionException nsrpe) {
125                                    return;
126                            }
127                    }
128    
129                    if (name.equals(User.class.getName())) {
130                            User user = UserLocalServiceUtil.getUserById(ownerId);
131    
132                            Contact contact = ContactLocalServiceUtil.getContact(
133                                    user.getContactId());
134    
135                            ownerId = contact.getUserId();
136                    }
137    
138                    if (ownerId != resourcePermission.getOwnerId()) {
139                            resourcePermission.setOwnerId(ownerId);
140    
141                            ResourcePermissionLocalServiceUtil.updateResourcePermission(
142                                    resourcePermission);
143                    }
144    
145                    if (_log.isInfoEnabled() &&
146                            (resourcePermission.getResourcePermissionId() % 100 == 0)) {
147    
148                            _log.info("Processed 100 resource permissions for " + name);
149                    }
150            }
151    
152            protected void verifyModel(
153                            Role role, String name, String modelName, String pkColumnName)
154                    throws Exception {
155    
156                    Connection con = null;
157                    PreparedStatement ps = null;
158                    ResultSet rs = null;
159    
160                    try {
161                            con = DataAccess.getConnection();
162    
163                            ps = con.prepareStatement(
164                                    "select " + pkColumnName + ", userId AS ownerId " +
165                                            "from " + modelName + " where companyId = " +
166                                                    role.getCompanyId());
167    
168                            rs = ps.executeQuery();
169    
170                            while (rs.next()) {
171                                    long primKey = rs.getLong(pkColumnName);
172                                    long ownerId = rs.getLong("ownerId");
173    
174                                    verifyModel(role.getCompanyId(), name, primKey, role, ownerId);
175                            }
176                    }
177                    finally {
178                            DataAccess.cleanUp(con, ps, rs);
179                    }
180            }
181    
182            private static final String[][] _MODELS = new String[][] {
183                    new String[] {
184                            AnnouncementsEntry.class.getName(),
185                            "AnnouncementsEntry",
186                            "entryId"
187                    },
188                    new String[] {
189                            AssetCategory.class.getName(),
190                            "AssetCategory",
191                            "categoryId"
192                    },
193                    new String[] {
194                            AssetTag.class.getName(),
195                            "AssetTag",
196                            "tagId"
197                    },
198                    new String[] {
199                            AssetVocabulary.class.getName(),
200                            "AssetVocabulary",
201                            "vocabularyId"
202                    },
203                    new String[] {
204                            BlogsEntry.class.getName(),
205                            "BlogsEntry",
206                            "entryId"
207                    },
208                    new String[] {
209                            BookmarksEntry.class.getName(),
210                            "BookmarksEntry",
211                            "entryId"
212                    },
213                    new String[] {
214                            BookmarksFolder.class.getName(),
215                            "BookmarksFolder",
216                            "folderId"
217                    },
218                    new String[] {
219                            CalEvent.class.getName(),
220                            "CalEvent",
221                            "eventId"
222                    },
223                    new String[] {
224                            DDMContent.class.getName(),
225                            "DDMContent",
226                            "contentId"
227                    },
228                    new String[] {
229                            DDMStructure.class.getName(),
230                            "DDMStructure",
231                            "structureId"
232                    },
233                    new String[] {
234                            DDMTemplate.class.getName(),
235                            "DDMTemplate",
236                            "templateId"
237                    },
238                    new String[] {
239                            DLFileEntry.class.getName(),
240                            "DLFileEntry",
241                            "fileEntryId"
242                    },
243                    new String[] {
244                            DLFileShortcut.class.getName(),
245                            "DLFileShortcut",
246                            "fileShortcutId"
247                    },
248                    new String[] {
249                            DLFolder.class.getName(),
250                            "DLFolder",
251                            "folderId"
252                    },
253                    new String[] {
254                            JournalArticle.class.getName(),
255                            "JournalArticle",
256                            "resourcePrimKey"
257                    },
258                    new String[] {
259                            JournalFeed.class.getName(),
260                            "JournalFeed",
261                            "id_"
262                    },
263                    new String[] {
264                            JournalStructure.class.getName(),
265                            "JournalStructure",
266                            "id_"
267                    },
268                    new String[] {
269                            JournalTemplate.class.getName(),
270                            "JournalTemplate",
271                            "id_"
272                    },
273                    new String[] {
274                            LayoutSetBranch.class.getName(),
275                            "LayoutSetBranch",
276                            "layoutSetBranchId"
277                    },
278                    new String[] {
279                            MBCategory.class.getName(),
280                            "MBCategory",
281                            "categoryId"
282                    },
283                    new String[] {
284                            MBMessage.class.getName(),
285                            "MBMessage",
286                            "messageId"
287                    },
288                    new String[] {
289                            PasswordPolicy.class.getName(),
290                            "PasswordPolicy",
291                            "passwordPolicyId"
292                    },
293                    new String[] {
294                            PollsQuestion.class.getName(),
295                            "PollsQuestion",
296                            "questionId"
297                    },
298                    new String[] {
299                            SCFrameworkVersion.class.getName(),
300                            "SCFrameworkVersion",
301                            "frameworkVersionId"
302                    },
303                    new String[] {
304                            SCProductEntry.class.getName(),
305                            "SCProductEntry",
306                            "productEntryId"
307                    },
308                    new String[] {
309                            ShoppingCategory.class.getName(),
310                            "ShoppingCategory",
311                            "categoryId"
312                    },
313                    new String[] {
314                            ShoppingItem.class.getName(),
315                            "ShoppingItem",
316                            "itemId"
317                    },
318                    new String[] {
319                            Team.class.getName(),
320                            "Team",
321                            "teamId"
322                    },
323                    new String[] {
324                            User.class.getName(),
325                            "User_",
326                            "userId"
327                    },
328                    new String[] {
329                            WikiNode.class.getName(),
330                            "WikiNode",
331                            "nodeId"
332                    },
333                    new String[] {
334                            WikiPage.class.getName(),
335                            "WikiPage",
336                            "resourcePrimKey"
337                    }
338            };
339    
340            private static Log _log = LogFactoryUtil.getLog(
341                    VerifyResourcePermissions.class);
342    
343    }