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