001    /**
002     * Copyright (c) 2000-present 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.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.model.Contact;
023    import com.liferay.portal.model.Layout;
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.User;
029    import com.liferay.portal.service.ContactLocalServiceUtil;
030    import com.liferay.portal.service.LayoutLocalServiceUtil;
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.verify.model.VerifiableResourcedModel;
037    
038    import java.sql.Connection;
039    import java.sql.PreparedStatement;
040    import java.sql.ResultSet;
041    
042    import java.util.Collection;
043    import java.util.List;
044    import java.util.Map;
045    
046    /**
047     * @author Raymond Aug??
048     * @author James Lefeu
049     */
050    public class VerifyResourcePermissions extends VerifyProcess {
051    
052            public void verify(VerifiableResourcedModel ... verifiableResourcedModels)
053                    throws Exception {
054    
055                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
056    
057                    for (long companyId : companyIds) {
058                            Role role = RoleLocalServiceUtil.getRole(
059                                    companyId, RoleConstants.OWNER);
060    
061                            for (VerifiableResourcedModel verifiableResourcedModel :
062                                            verifiableResourcedModels) {
063    
064                                    verifyResourcedModel(
065                                            role, verifiableResourcedModel.getModelName(),
066                                            verifiableResourcedModel.getTableName(),
067                                            verifiableResourcedModel.getPrimaryKeyColumnName());
068                            }
069    
070                            verifyLayout(role);
071                    }
072            }
073    
074            @Override
075            protected void doVerify() throws Exception {
076                    Map<String, VerifiableResourcedModel> verifiableResourcedModelsMap =
077                            PortalBeanLocatorUtil.locate(VerifiableResourcedModel.class);
078    
079                    Collection<VerifiableResourcedModel> verifiableResourcedModels =
080                            verifiableResourcedModelsMap.values();
081    
082                    verify(
083                            verifiableResourcedModels.toArray(
084                                    new VerifiableResourcedModel[
085                                            verifiableResourcedModels.size()]));
086            }
087    
088            protected void verifyLayout(Role role) throws Exception {
089                    List<Layout> layouts = LayoutLocalServiceUtil.getNoPermissionLayouts(
090                            role.getRoleId());
091    
092                    int total = layouts.size();
093    
094                    for (int i = 0; i < total; i++) {
095                            Layout layout = layouts.get(i);
096    
097                            verifyResourcedModel(
098                                    role.getCompanyId(), Layout.class.getName(), layout.getPlid(),
099                                    role, 0, i, total);
100                    }
101            }
102    
103            protected void verifyResourcedModel(
104                            long companyId, String modelName, long primKey, Role role,
105                            long ownerId, int cur, int total)
106                    throws Exception {
107    
108                    if (_log.isInfoEnabled() && ((cur % 100) == 0)) {
109                            _log.info(
110                                    "Processed " + cur + " of " + total + " resource permissions " +
111                                            "for company = " + companyId + " and model " + modelName);
112                    }
113    
114                    ResourcePermission resourcePermission = null;
115    
116                    try {
117                            resourcePermission =
118                                    ResourcePermissionLocalServiceUtil.getResourcePermission(
119                                            companyId, modelName, ResourceConstants.SCOPE_INDIVIDUAL,
120                                            String.valueOf(primKey), role.getRoleId());
121                    }
122                    catch (NoSuchResourcePermissionException nsrpe) {
123                            if (_log.isDebugEnabled()) {
124                                    _log.debug(
125                                            "No resource found for {" + companyId + ", " + modelName +
126                                                    ", " + ResourceConstants.SCOPE_INDIVIDUAL + ", " +
127                                                            primKey + ", " + role.getRoleId() + "}");
128                            }
129    
130                            ResourceLocalServiceUtil.addResources(
131                                    companyId, 0, ownerId, modelName, String.valueOf(primKey),
132                                    false, false, false);
133                    }
134    
135                    if (resourcePermission == null) {
136                            try {
137                                    resourcePermission =
138                                            ResourcePermissionLocalServiceUtil.getResourcePermission(
139                                                    companyId, modelName,
140                                                    ResourceConstants.SCOPE_INDIVIDUAL,
141                                                    String.valueOf(primKey), role.getRoleId());
142                            }
143                            catch (NoSuchResourcePermissionException nsrpe) {
144                                    return;
145                            }
146                    }
147    
148                    if (modelName.equals(User.class.getName())) {
149                            User user = UserLocalServiceUtil.fetchUserById(ownerId);
150    
151                            if (user != null) {
152                                    Contact contact = ContactLocalServiceUtil.fetchContact(
153                                            user.getContactId());
154    
155                                    if (contact != null) {
156                                            ownerId = contact.getUserId();
157                                    }
158                            }
159                    }
160    
161                    if (ownerId != resourcePermission.getOwnerId()) {
162                            resourcePermission.setOwnerId(ownerId);
163    
164                            ResourcePermissionLocalServiceUtil.updateResourcePermission(
165                                    resourcePermission);
166                    }
167            }
168    
169            protected void verifyResourcedModel(
170                            Role role, String modelName, String tableName,
171                            String primaryKeyColumnName)
172                    throws Exception {
173    
174                    Connection con = null;
175                    PreparedStatement ps = null;
176                    ResultSet rs = null;
177    
178                    int total = 0;
179    
180                    try {
181                            con = DataAccess.getUpgradeOptimizedConnection();
182    
183                            ps = con.prepareStatement(
184                                    "select count(*) from " + tableName + " where companyId = " +
185                                            role.getCompanyId());
186    
187                            rs = ps.executeQuery();
188    
189                            if (rs.next()) {
190                                    total = rs.getInt(1);
191                            }
192                    }
193                    finally {
194                            DataAccess.cleanUp(con, ps, rs);
195                    }
196    
197                    try {
198                            con = DataAccess.getUpgradeOptimizedConnection();
199    
200                            ps = con.prepareStatement(
201                                    "select " + primaryKeyColumnName + ", userId from " +
202                                            tableName + " where companyId = " + role.getCompanyId());
203    
204                            rs = ps.executeQuery();
205    
206                            for (int i = 0; rs.next(); i++) {
207                                    long primKey = rs.getLong(primaryKeyColumnName);
208                                    long userId = rs.getLong("userId");
209    
210                                    verifyResourcedModel(
211                                            role.getCompanyId(), modelName, primKey, role, userId, i,
212                                            total);
213                            }
214                    }
215                    finally {
216                            DataAccess.cleanUp(con, ps, rs);
217                    }
218            }
219    
220            private static Log _log = LogFactoryUtil.getLog(
221                    VerifyResourcePermissions.class);
222    
223    }