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.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.model.GroupConstants;
018    import com.liferay.portal.kernel.model.ResourcePermission;
019    import com.liferay.portal.kernel.security.permission.ActionKeys;
020    import com.liferay.portal.kernel.util.LoggingTimer;
021    
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Juan Fern??ndez
027     * @author Sergio Gonz??lez
028     */
029    public abstract class BaseUpgradeAdminPortlets extends UpgradeProcess {
030    
031            protected void addResourcePermission(
032                            long resourcePermissionId, long companyId, String name, int scope,
033                            String primKey, long roleId, long actionIds)
034                    throws Exception {
035    
036                    try (PreparedStatement ps = connection.prepareStatement(
037                                    "insert into ResourcePermission (resourcePermissionId, " +
038                                            "companyId, name, scope, primKey, roleId, actionIds) " +
039                                                    "values (?, ?, ?, ?, ?, ?, ?)")) {
040    
041                            ps.setLong(1, resourcePermissionId);
042                            ps.setLong(2, companyId);
043                            ps.setString(3, name);
044                            ps.setInt(4, scope);
045                            ps.setString(5, primKey);
046                            ps.setLong(6, roleId);
047                            ps.setLong(7, actionIds);
048    
049                            ps.executeUpdate();
050                    }
051            }
052    
053            protected long getBitwiseValue(String name, String actionId)
054                    throws Exception {
055    
056                    try (PreparedStatement ps = connection.prepareStatement(
057                                    "select bitwiseValue from ResourceAction where name = ? and " +
058                                            "actionId = ?")) {
059    
060                            ps.setString(1, name);
061                            ps.setString(2, actionId);
062    
063                            try (ResultSet rs = ps.executeQuery()) {
064                                    if (rs.next()) {
065                                            return rs.getLong("bitwiseValue");
066                                    }
067    
068                                    return 0;
069                            }
070                    }
071            }
072    
073            protected long getControlPanelGroupId() throws Exception {
074                    try (PreparedStatement ps = connection.prepareStatement(
075                                    "select groupId from Group_ where name = '" +
076                                            GroupConstants.CONTROL_PANEL + "'");
077                            ResultSet rs = ps.executeQuery()) {
078    
079                            if (rs.next()) {
080                                    return rs.getLong("groupId");
081                            }
082    
083                            return 0;
084                    }
085            }
086    
087            protected void updateAccessInControlPanelPermission(
088                            String portletFrom, String portletTo)
089                    throws Exception {
090    
091                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
092                            long bitwiseValue = getBitwiseValue(
093                                    portletFrom, ActionKeys.ACCESS_IN_CONTROL_PANEL);
094    
095                            try (PreparedStatement ps = connection.prepareStatement(
096                                            "select * from ResourcePermission where name = ?")) {
097    
098                                    ps.setString(1, portletFrom);
099    
100                                    try (ResultSet rs = ps.executeQuery()) {
101                                            while (rs.next()) {
102                                                    long resourcePermissionId = rs.getLong(
103                                                            "resourcePermissionId");
104                                                    long actionIds = rs.getLong("actionIds");
105    
106                                                    if ((actionIds & bitwiseValue) != 0) {
107                                                            actionIds = actionIds & (~bitwiseValue);
108    
109                                                            runSQL(
110                                                                    "update ResourcePermission set actionIds = " +
111                                                                            actionIds + " where resourcePermissionId " +
112                                                                                    "= " + resourcePermissionId);
113    
114                                                            resourcePermissionId = increment(
115                                                                    ResourcePermission.class.getName());
116    
117                                                            long companyId = rs.getLong("companyId");
118                                                            int scope = rs.getInt("scope");
119                                                            String primKey = rs.getString("primKey");
120                                                            long roleId = rs.getLong("roleId");
121    
122                                                            actionIds = rs.getLong("actionIds");
123    
124                                                            actionIds |= bitwiseValue;
125    
126                                                            addResourcePermission(
127                                                                    resourcePermissionId, companyId, portletTo,
128                                                                    scope, primKey, roleId, actionIds);
129                                                    }
130                                            }
131                                    }
132                            }
133                    }
134            }
135    
136    }