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.portal.util.PropsValues;
037    import com.liferay.portlet.announcements.model.AnnouncementsEntry;
038    import com.liferay.portlet.asset.model.AssetCategory;
039    import com.liferay.portlet.asset.model.AssetTag;
040    import com.liferay.portlet.asset.model.AssetVocabulary;
041    import com.liferay.portlet.blogs.model.BlogsEntry;
042    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
043    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
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                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
080    
081                    for (long companyId : companyIds) {
082                            Role role = RoleLocalServiceUtil.getRole(
083                                    companyId, RoleConstants.OWNER);
084    
085                            for (String[] model : _MODELS) {
086                                    verifyModel(role, model[0], model[1], model[2]);
087                            }
088                    }
089            }
090    
091            protected void verifyModel(
092                            long companyId, String name, long primKey, Role role, long ownerId)
093                    throws Exception {
094    
095                    ResourcePermission resourcePermission = null;
096    
097                    try {
098                            resourcePermission =
099                                    ResourcePermissionLocalServiceUtil.getResourcePermission(
100                                            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
101                                            String.valueOf(primKey), role.getRoleId());
102                    }
103                    catch (NoSuchResourcePermissionException nsrpe) {
104                            if (_log.isDebugEnabled()) {
105                                    _log.debug(
106                                            "No resource found for {" + companyId + ", " + name + ", " +
107                                                    ResourceConstants.SCOPE_INDIVIDUAL + ", " + primKey +
108                                                            ", " + role.getRoleId() + "}");
109                            }
110    
111                            ResourceLocalServiceUtil.addResources(
112                                    companyId, 0, ownerId, name, String.valueOf(primKey), false,
113                                    false, false);
114                    }
115    
116                    if (resourcePermission == null) {
117                            try {
118                                    resourcePermission =
119                                            ResourcePermissionLocalServiceUtil.getResourcePermission(
120                                                    companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
121                                                    String.valueOf(primKey), role.getRoleId());
122                            }
123                            catch (NoSuchResourcePermissionException nsrpe) {
124                                    return;
125                            }
126                    }
127    
128                    if (name.equals(User.class.getName())) {
129                            User user = UserLocalServiceUtil.getUserById(ownerId);
130    
131                            Contact contact = ContactLocalServiceUtil.getContact(
132                                    user.getContactId());
133    
134                            ownerId = contact.getUserId();
135                    }
136    
137                    if (ownerId != resourcePermission.getOwnerId()) {
138                            resourcePermission.setOwnerId(ownerId);
139    
140                            ResourcePermissionLocalServiceUtil.updateResourcePermission(
141                                    resourcePermission);
142                    }
143    
144                    if (_log.isInfoEnabled() &&
145                            (resourcePermission.getResourcePermissionId() % 100 == 0)) {
146    
147                            _log.info("Processed 100 resource permissions for " + name);
148                    }
149            }
150    
151            protected void verifyModel(
152                            Role role, String name, String modelName, String pkColumnName)
153                    throws Exception {
154    
155                    Connection con = null;
156                    PreparedStatement ps = null;
157                    ResultSet rs = null;
158    
159                    try {
160                            con = DataAccess.getConnection();
161    
162                            ps = con.prepareStatement(
163                                    "select " + pkColumnName + ", userId AS ownerId " +
164                                            "from " + modelName + " where companyId = " +
165                                                    role.getCompanyId());
166    
167                            rs = ps.executeQuery();
168    
169                            while (rs.next()) {
170                                    long primKey = rs.getLong(pkColumnName);
171                                    long ownerId = rs.getLong("ownerId");
172    
173                                    verifyModel(role.getCompanyId(), name, primKey, role, ownerId);
174                            }
175                    }
176                    finally {
177                            DataAccess.cleanUp(con, ps, rs);
178                    }
179            }
180    
181            private static final String[][] _MODELS = new String[][] {
182                    new String[] {
183                            AnnouncementsEntry.class.getName(), "AnnouncementsEntry", "entryId"
184                    },
185                    new String[] {
186                            AssetCategory.class.getName(), "AssetCategory", "categoryId"
187                    },
188                    new String[] {
189                            AssetTag.class.getName(), "AssetTag", "tagId"
190                    },
191                    new String[] {
192                            AssetVocabulary.class.getName(), "AssetVocabulary", "vocabularyId"
193                    },
194                    new String[] {
195                            BlogsEntry.class.getName(), "BlogsEntry", "entryId"
196                    },
197                    new String[] {
198                            BookmarksEntry.class.getName(), "BookmarksEntry", "entryId"
199                    },
200                    new String[] {
201                            BookmarksFolder.class.getName(), "BookmarksFolder", "folderId"
202                    },
203                    new String[] {
204                            CalEvent.class.getName(), "CalEvent", "eventId"
205                    },
206                    new String[] {
207                            DDMStructure.class.getName(), "DDMStructure", "structureId"
208                    },
209                    new String[] {
210                            DDMTemplate.class.getName(), "DDMTemplate", "templateId"
211                    },
212                    new String[] {
213                            DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId"
214                    },
215                    new String[] {
216                            DLFileShortcut.class.getName(), "DLFileShortcut", "fileShortcutId"
217                    },
218                    new String[] {
219                            DLFolder.class.getName(), "DLFolder", "folderId"
220                    },
221                    new String[] {
222                            JournalArticle.class.getName(), "JournalArticle", "resourcePrimKey"
223                    },
224                    new String[] {
225                            JournalFeed.class.getName(), "JournalFeed", "id_"
226                    },
227                    new String[] {
228                            JournalStructure.class.getName(), "JournalStructure", "id_"
229                    },
230                    new String[] {
231                            JournalTemplate.class.getName(), "JournalTemplate", "id_"
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    }