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                            verifyLayout(role);
093                    }
094            }
095    
096            protected void verifyLayout(Role role) throws Exception {
097                    List<Layout> layouts = LayoutLocalServiceUtil.getNoPermissionLayouts(
098                            role.getRoleId());
099    
100                    for (Layout layout : layouts) {
101                            verifyModel(
102                                    role.getCompanyId(), Layout.class.getName(), layout.getPlid(),
103                                    role, 0);
104                    }
105            }
106    
107            protected void verifyModel(
108                            long companyId, String name, long primKey, Role role, long ownerId)
109                    throws Exception {
110    
111                    ResourcePermission resourcePermission =
112                            ResourcePermissionLocalServiceUtil.fetchResourcePermission(
113                                    companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
114                                    String.valueOf(primKey), role.getRoleId());
115    
116                    if (resourcePermission == null) {
117                            if (_log.isDebugEnabled()) {
118                                    _log.debug(
119                                            "No resource found for {" + companyId + ", " + name + ", " +
120                                                    ResourceConstants.SCOPE_INDIVIDUAL + ", " + primKey +
121                                                            ", " + role.getRoleId() + "}");
122                            }
123    
124                            ResourceLocalServiceUtil.addResources(
125                                    companyId, 0, ownerId, name, String.valueOf(primKey), false,
126                                    false, false);
127                    }
128    
129                    if (resourcePermission == null) {
130                            resourcePermission =
131                                    ResourcePermissionLocalServiceUtil.fetchResourcePermission(
132                                            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
133                                            String.valueOf(primKey), role.getRoleId());
134    
135                            if (resourcePermission == null) {
136                                    return;
137                            }
138                    }
139    
140                    if (name.equals(User.class.getName())) {
141                            User user = UserLocalServiceUtil.getUserById(ownerId);
142    
143                            Contact contact = ContactLocalServiceUtil.getContact(
144                                    user.getContactId());
145    
146                            ownerId = contact.getUserId();
147                    }
148    
149                    if (ownerId != resourcePermission.getOwnerId()) {
150                            resourcePermission.setOwnerId(ownerId);
151    
152                            ResourcePermissionLocalServiceUtil.updateResourcePermission(
153                                    resourcePermission);
154                    }
155    
156                    if (_log.isInfoEnabled() &&
157                            ((resourcePermission.getResourcePermissionId() % 100) == 0)) {
158    
159                            _log.info("Processed 100 resource permissions for " + name);
160                    }
161            }
162    
163            protected void verifyModel(
164                            Role role, String name, String modelName, String pkColumnName)
165                    throws Exception {
166    
167                    Connection con = null;
168                    PreparedStatement ps = null;
169                    ResultSet rs = null;
170    
171                    try {
172                            con = DataAccess.getUpgradeOptimizedConnection();
173    
174                            ps = con.prepareStatement(
175                                    "select " + pkColumnName + ", userId AS ownerId " +
176                                            "from " + modelName + " where companyId = " +
177                                                    role.getCompanyId());
178    
179                            rs = ps.executeQuery();
180    
181                            while (rs.next()) {
182                                    long primKey = rs.getLong(pkColumnName);
183                                    long ownerId = rs.getLong("ownerId");
184    
185                                    verifyModel(role.getCompanyId(), name, primKey, role, ownerId);
186                            }
187                    }
188                    finally {
189                            DataAccess.cleanUp(con, ps, rs);
190                    }
191            }
192    
193            private static final String[][] _MODELS = new String[][] {
194                    new String[] {
195                            AnnouncementsEntry.class.getName(), "AnnouncementsEntry", "entryId"
196                    },
197                    new String[] {
198                            AssetCategory.class.getName(), "AssetCategory", "categoryId"
199                    },
200                    new String[] {
201                            AssetTag.class.getName(), "AssetTag", "tagId"
202                    },
203                    new String[] {
204                            AssetVocabulary.class.getName(), "AssetVocabulary", "vocabularyId"
205                    },
206                    new String[] {
207                            BlogsEntry.class.getName(), "BlogsEntry", "entryId"
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                            Layout.class.getName(), "Layout", "plid"
232                    },
233                    new String[] {
234                            LayoutSetBranch.class.getName(), "LayoutSetBranch",
235                            "layoutSetBranchId"
236                    },
237                    new String[] {
238                            MBCategory.class.getName(), "MBCategory", "categoryId"
239                    },
240                    new String[] {
241                            MBMessage.class.getName(), "MBMessage", "messageId"
242                    },
243                    new String[] {
244                            PasswordPolicy.class.getName(), "PasswordPolicy", "passwordPolicyId"
245                    },
246                    new String[] {
247                            PollsQuestion.class.getName(), "PollsQuestion", "questionId"
248                    },
249                    new String[] {
250                            SCFrameworkVersion.class.getName(), "SCFrameworkVersion",
251                            "frameworkVersionId"
252                    },
253                    new String[] {
254                            SCProductEntry.class.getName(), "SCProductEntry", "productEntryId"
255                    },
256                    new String[] {
257                            ShoppingCategory.class.getName(), "ShoppingCategory", "categoryId"
258                    },
259                    new String[] {
260                            ShoppingItem.class.getName(), "ShoppingItem", "itemId"
261                    },
262                    new String[] {
263                            Team.class.getName(), "Team", "teamId"
264                    },
265                    new String[] {
266                            User.class.getName(), "User_", "userId"
267                    },
268                    new String[] {
269                            WikiNode.class.getName(), "WikiNode", "nodeId"
270                    },
271                    new String[] {
272                            WikiPage.class.getName(), "WikiPage", "resourcePrimKey"
273                    }
274            };
275    
276            private static Log _log = LogFactoryUtil.getLog(
277                    VerifyResourcePermissions.class);
278    
279            private class VerifyResourcedModelRunnable extends ThrowableAwareRunnable {
280    
281                    private VerifyResourcedModelRunnable(
282                            Role role, String name, String modelName, String pkColumnName) {
283    
284                            _modelName = modelName;
285                            _name = name;
286                            _pkColumnName = pkColumnName;
287                            _role = role;
288                    }
289    
290                    @Override
291                    protected void doRun() throws Exception {
292                            verifyModel(_role, _name, _modelName, _pkColumnName);
293                    }
294    
295                    private final String _modelName;
296                    private final String _name;
297                    private final String _pkColumnName;
298                    private final Role _role;
299    
300            }
301    
302    }