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.kernel.util.GetterUtil;
022    import com.liferay.portal.model.Contact;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutSetBranch;
025    import com.liferay.portal.model.PasswordPolicy;
026    import com.liferay.portal.model.ResourceConstants;
027    import com.liferay.portal.model.ResourcePermission;
028    import com.liferay.portal.model.Role;
029    import com.liferay.portal.model.RoleConstants;
030    import com.liferay.portal.model.Team;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.service.ContactLocalServiceUtil;
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.portal.util.PropsValues;
039    import com.liferay.portlet.announcements.model.AnnouncementsEntry;
040    import com.liferay.portlet.asset.model.AssetCategory;
041    import com.liferay.portlet.asset.model.AssetTag;
042    import com.liferay.portlet.asset.model.AssetVocabulary;
043    import com.liferay.portlet.blogs.model.BlogsEntry;
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                    for (String[] portletAndActionId : _PORTLET_ACTION_IDS) {
080                            verifyActionIds(portletAndActionId[0], portletAndActionId[1]);
081                    }
082    
083                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
084    
085                    for (long companyId : companyIds) {
086                            Role role = RoleLocalServiceUtil.getRole(
087                                    companyId, RoleConstants.OWNER);
088    
089                            for (String[] model : _MODELS) {
090                                    verifyModel(role, model[0], model[1], model[2]);
091                            }
092                    }
093            }
094    
095            protected void verifyActionIds(String portlet, String actionId)
096                    throws Exception {
097    
098                    Connection con = null;
099                    PreparedStatement ps = null;
100    
101                    try {
102                            con = DataAccess.getUpgradeOptimizedConnection();
103    
104                            ps = con.prepareStatement(
105                                    "update ResourcePermission set actionIds = ? where name = ? " +
106                                            "and roleId in (select roleId from Role_ where name = ?) " +
107                                                    "and primKey != '0'");
108    
109                            ps.setLong(1, GetterUtil.getLong(actionId));
110                            ps.setString(2, portlet);
111                            ps.setString(3, "Site Member");
112    
113                            ps.executeUpdate();
114                    }
115                    finally {
116                            DataAccess.cleanUp(con, ps);
117                    }
118            }
119    
120            protected void verifyModel(
121                            long companyId, String name, long primKey, Role role, long ownerId)
122                    throws Exception {
123    
124                    ResourcePermission resourcePermission = null;
125    
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                            if (_log.isDebugEnabled()) {
134                                    _log.debug(
135                                            "No resource found for {" + companyId + ", " + name + ", " +
136                                                    ResourceConstants.SCOPE_INDIVIDUAL + ", " + primKey +
137                                                            ", " + role.getRoleId() + "}");
138                            }
139    
140                            ResourceLocalServiceUtil.addResources(
141                                    companyId, 0, ownerId, name, String.valueOf(primKey), false,
142                                    false, false);
143                    }
144    
145                    if (resourcePermission == null) {
146                            try {
147                                    resourcePermission =
148                                            ResourcePermissionLocalServiceUtil.getResourcePermission(
149                                                    companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
150                                                    String.valueOf(primKey), role.getRoleId());
151                            }
152                            catch (NoSuchResourcePermissionException nsrpe) {
153                                    return;
154                            }
155                    }
156    
157                    if (name.equals(User.class.getName())) {
158                            User user = UserLocalServiceUtil.getUserById(ownerId);
159    
160                            Contact contact = ContactLocalServiceUtil.getContact(
161                                    user.getContactId());
162    
163                            ownerId = contact.getUserId();
164                    }
165    
166                    if (ownerId != resourcePermission.getOwnerId()) {
167                            resourcePermission.setOwnerId(ownerId);
168    
169                            ResourcePermissionLocalServiceUtil.updateResourcePermission(
170                                    resourcePermission);
171                    }
172    
173                    if (_log.isInfoEnabled() &&
174                            ((resourcePermission.getResourcePermissionId() % 100) == 0)) {
175    
176                            _log.info("Processed 100 resource permissions for " + name);
177                    }
178            }
179    
180            protected void verifyModel(
181                            Role role, String name, String modelName, String pkColumnName)
182                    throws Exception {
183    
184                    Connection con = null;
185                    PreparedStatement ps = null;
186                    ResultSet rs = null;
187    
188                    try {
189                            con = DataAccess.getUpgradeOptimizedConnection();
190    
191                            if (modelName.equals("Layout")) {
192                                    ps = con.prepareStatement(
193                                            "select " + pkColumnName + ", 0 AS ownerId " +
194                                                    "from " + modelName + " where companyId = " +
195                                                            role.getCompanyId());
196                            }
197                            else {
198                                    ps = con.prepareStatement(
199                                            "select " + pkColumnName + ", userId AS ownerId " +
200                                                    "from " + modelName + " where companyId = " +
201                                                            role.getCompanyId());
202                            }
203    
204                            rs = ps.executeQuery();
205    
206                            while (rs.next()) {
207                                    long primKey = rs.getLong(pkColumnName);
208                                    long ownerId = rs.getLong("ownerId");
209    
210                                    verifyModel(role.getCompanyId(), name, primKey, role, ownerId);
211                            }
212                    }
213                    finally {
214                            DataAccess.cleanUp(con, ps, rs);
215                    }
216            }
217    
218            private static final String[][] _MODELS = new String[][] {
219                    new String[] {
220                            AnnouncementsEntry.class.getName(), "AnnouncementsEntry", "entryId"
221                    },
222                    new String[] {
223                            AssetCategory.class.getName(), "AssetCategory", "categoryId"
224                    },
225                    new String[] {
226                            AssetTag.class.getName(), "AssetTag", "tagId"
227                    },
228                    new String[] {
229                            AssetVocabulary.class.getName(), "AssetVocabulary", "vocabularyId"
230                    },
231                    new String[] {
232                            BlogsEntry.class.getName(), "BlogsEntry", "entryId"
233                    },
234                    new String[] {
235                            CalEvent.class.getName(), "CalEvent", "eventId"
236                    },
237                    new String[] {
238                            DDMStructure.class.getName(), "DDMStructure", "structureId"
239                    },
240                    new String[] {
241                            DDMTemplate.class.getName(), "DDMTemplate", "templateId"
242                    },
243                    new String[] {
244                            DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId"
245                    },
246                    new String[] {
247                            DLFileShortcut.class.getName(), "DLFileShortcut", "fileShortcutId"
248                    },
249                    new String[] {
250                            DLFolder.class.getName(), "DLFolder", "folderId"
251                    },
252                    new String[] {
253                            JournalArticle.class.getName(), "JournalArticle", "resourcePrimKey"
254                    },
255                    new String[] {
256                            JournalFeed.class.getName(), "JournalFeed", "id_"
257                    },
258                    new String[] {
259                            JournalStructure.class.getName(), "JournalStructure", "id_"
260                    },
261                    new String[] {
262                            JournalTemplate.class.getName(), "JournalTemplate", "id_"
263                    },
264                    new String[] {
265                            Layout.class.getName(), "Layout", "plid"
266                    },
267                    new String[] {
268                            LayoutSetBranch.class.getName(), "LayoutSetBranch",
269                            "layoutSetBranchId"
270                    },
271                    new String[] {
272                            MBCategory.class.getName(), "MBCategory", "categoryId"
273                    },
274                    new String[] {
275                            MBMessage.class.getName(), "MBMessage", "messageId"
276                    },
277                    new String[] {
278                            PasswordPolicy.class.getName(), "PasswordPolicy", "passwordPolicyId"
279                    },
280                    new String[] {
281                            PollsQuestion.class.getName(), "PollsQuestion", "questionId"
282                    },
283                    new String[] {
284                            SCFrameworkVersion.class.getName(), "SCFrameworkVersion",
285                            "frameworkVersionId"
286                    },
287                    new String[] {
288                            SCProductEntry.class.getName(), "SCProductEntry", "productEntryId"
289                    },
290                    new String[] {
291                            ShoppingCategory.class.getName(), "ShoppingCategory", "categoryId"
292                    },
293                    new String[] {
294                            ShoppingItem.class.getName(), "ShoppingItem", "itemId"
295                    },
296                    new String[] {
297                            Team.class.getName(), "Team", "teamId"
298                    },
299                    new String[] {
300                            User.class.getName(), "User_", "userId"
301                    },
302                    new String[] {
303                            WikiNode.class.getName(), "WikiNode", "nodeId"
304                    },
305                    new String[] {
306                            WikiPage.class.getName(), "WikiPage", "resourcePrimKey"
307                    }
308            };
309    
310            private static final String[][] _PORTLET_ACTION_IDS = new String[][] {
311                    new String[] {
312                            "com.liferay.portlet.bookmarks", "17"
313                    },
314                    new String[] {
315                            "com.liferay.portlet.documentlibrary", "513"
316                    },
317            };
318    
319            private static Log _log = LogFactoryUtil.getLog(
320                    VerifyResourcePermissions.class);
321    
322    }