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.upgrade.v6_1_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.model.GroupConstants;
020    import com.liferay.portal.model.ResourcePermission;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.util.PortletKeys;
023    
024    import java.sql.Connection;
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    
028    /**
029     * @author Juan Fern??ndez
030     */
031    public class UpgradeAdminPortlets extends UpgradeProcess {
032    
033            protected void addResourcePermission(
034                            long resourcePermissionId, long companyId, String name, int scope,
035                            String primKey, long roleId, long actionIds)
036                    throws Exception {
037    
038                    Connection con = null;
039                    PreparedStatement ps = null;
040    
041                    try {
042                            con = DataAccess.getUpgradeOptimizedConnection();
043    
044                            ps = con.prepareStatement(
045                                    "insert into ResourcePermission (resourcePermissionId, " +
046                                            "companyId, name, scope, primKey, roleId, actionIds) " +
047                                                    "values (?, ?, ?, ?, ?, ?, ?)");
048    
049                            ps.setLong(1, resourcePermissionId);
050                            ps.setLong(2, companyId);
051                            ps.setString(3, name);
052                            ps.setInt(4, scope);
053                            ps.setString(5, primKey);
054                            ps.setLong(6, roleId);
055                            ps.setLong(7, actionIds);
056    
057                            ps.executeUpdate();
058                    }
059                    finally {
060                            DataAccess.cleanUp(con, ps);
061                    }
062            }
063    
064            @Override
065            protected void doUpgrade() throws Exception {
066                    updateAccessInControlPanelPermission(
067                            PortletKeys.BLOGS, PortletKeys.BLOGS_ADMIN);
068    
069                    updateAccessInControlPanelPermission(
070                            PortletKeys.MESSAGE_BOARDS, PortletKeys.MESSAGE_BOARDS_ADMIN);
071            }
072    
073            protected long getBitwiseValue(String name, String actionId)
074                    throws Exception {
075    
076                    Connection con = null;
077                    PreparedStatement ps = null;
078                    ResultSet rs = null;
079    
080                    try {
081                            con = DataAccess.getUpgradeOptimizedConnection();
082    
083                            ps = con.prepareStatement(
084                                    "select bitwiseValue from ResourceAction where name = ? and " +
085                                            "actionId = ?");
086    
087                            ps.setString(1, name);
088                            ps.setString(2, actionId);
089    
090                            rs = ps.executeQuery();
091    
092                            if (rs.next()) {
093                                    return rs.getLong("bitwiseValue");
094                            }
095    
096                            return 0;
097                    }
098                    finally {
099                            DataAccess.cleanUp(con, ps, rs);
100                    }
101            }
102    
103            protected long getControlPanelGroupId() throws Exception {
104                    Connection con = null;
105                    PreparedStatement ps = null;
106                    ResultSet rs = null;
107    
108                    try {
109                            con = DataAccess.getUpgradeOptimizedConnection();
110    
111                            ps = con.prepareStatement(
112                                    "select groupId from Group_ where name = '" +
113                                            GroupConstants.CONTROL_PANEL + "'");
114    
115                            rs = ps.executeQuery();
116    
117                            if (rs.next()) {
118                                    return rs.getLong("groupId");
119                            }
120    
121                            return 0;
122                    }
123                    finally {
124                            DataAccess.cleanUp(con, ps, rs);
125                    }
126            }
127    
128            protected void updateAccessInControlPanelPermission(
129                            String portletFrom, String portletTo)
130                    throws Exception {
131    
132                    long bitwiseValue = getBitwiseValue(
133                            portletFrom, ActionKeys.ACCESS_IN_CONTROL_PANEL);
134    
135                    Connection con = null;
136                    PreparedStatement ps = null;
137                    ResultSet rs = null;
138    
139                    try {
140                            con = DataAccess.getUpgradeOptimizedConnection();
141    
142                            ps = con.prepareStatement(
143                                    "select * from ResourcePermission where name = ?");
144    
145                            ps.setString(1, portletFrom);
146    
147                            rs = ps.executeQuery();
148    
149                            while (rs.next()) {
150                                    long resourcePermissionId = rs.getLong("resourcePermissionId");
151                                    long actionIds = rs.getLong("actionIds");
152    
153                                    if ((actionIds & bitwiseValue) != 0) {
154                                            actionIds = actionIds & (~bitwiseValue);
155    
156                                            runSQL(
157                                                    "update ResourcePermission set actionIds = " +
158                                                            actionIds + " where resourcePermissionId = " +
159                                                                    resourcePermissionId);
160    
161                                            resourcePermissionId = increment(
162                                                    ResourcePermission.class.getName());
163    
164                                            long companyId = rs.getLong("companyId");
165                                            int scope = rs.getInt("scope");
166                                            String primKey = rs.getString("primKey");
167                                            long roleId = rs.getLong("roleId");
168    
169                                            actionIds = rs.getLong("actionIds");
170    
171                                            actionIds |= bitwiseValue;
172    
173                                            addResourcePermission(
174                                                    resourcePermissionId, companyId, portletTo, scope,
175                                                    primKey, roleId, actionIds);
176                                    }
177                            }
178                    }
179                    finally {
180                            DataAccess.cleanUp(con, ps, rs);
181                    }
182            }
183    
184    }