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