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.upgrade.v6_1_0;
016    
017    import com.liferay.portal.kernel.model.GroupConstants;
018    import com.liferay.portal.kernel.model.ResourceConstants;
019    import com.liferay.portal.kernel.model.RoleConstants;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.util.LoggingTimer;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    
025    /**
026     * @author Hugo Huijser
027     */
028    public class UpgradeResourcePermission extends UpgradeProcess {
029    
030            @Override
031            protected void doUpgrade() throws Exception {
032                    updateResourcePermissions();
033            }
034    
035            protected void updateResourcePermissions() throws Exception {
036                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
037                            StringBundler sb = new StringBundler(11);
038    
039                            sb.append("update ResourcePermission set scope = ");
040                            sb.append(ResourceConstants.SCOPE_GROUP_TEMPLATE);
041                            sb.append(", primKey = '");
042                            sb.append(GroupConstants.DEFAULT_PARENT_GROUP_ID);
043                            sb.append("' where scope = ");
044                            sb.append(ResourceConstants.SCOPE_COMPANY);
045                            sb.append(" and primKey = CAST_TEXT(companyId) and exists ");
046                            sb.append("(select roleId from Role_ where Role_.roleId = ");
047                            sb.append("ResourcePermission.roleId and Role_.type_ = ");
048                            sb.append(RoleConstants.TYPE_PROVIDER);
049                            sb.append(StringPool.CLOSE_PARENTHESIS);
050    
051                            runSQL(sb.toString());
052                    }
053            }
054    
055    }