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