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