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_0_12;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.model.ResourceConstants;
019    import com.liferay.portal.model.Role;
020    import com.liferay.portal.model.RoleConstants;
021    import com.liferay.portal.security.permission.ResourceActionsUtil;
022    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
023    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
024    import com.liferay.portal.service.RoleLocalServiceUtil;
025    
026    import java.util.List;
027    
028    /**
029     * @author Alexander Chow
030     */
031    public class UpgradePermission extends UpgradeProcess {
032    
033            @Override
034            protected void doUpgrade() throws Exception {
035    
036                    // PermissionLocalServiceUtil.setContainerResourcePermissions() requires
037                    // an updated Company and Role_ table
038    
039                    runSQL("alter table Company add active_ BOOLEAN");
040                    runSQL("update Company set active_ = TRUE");
041    
042                    runSQL(
043                            "update Role_ set name = 'Site Administrator' where name = " +
044                                    "'Community Administrator'");
045                    runSQL(
046                            "update Role_ set name = 'Site Member' where name = 'Community " +
047                                    "Member'");
048                    runSQL(
049                            "update Role_ set name = 'Site Owner' where name = 'Community " +
050                                    "Owner'");
051                    runSQL(
052                            "update Role_ set name = 'Organization User' where name = " +
053                                    "'Organization Member'");
054    
055                    // LPS-54467
056    
057                    List<String> modelActions = ResourceActionsUtil.getModelResourceActions(
058                            Role.class.getName());
059    
060                    ResourceActionLocalServiceUtil.checkResourceActions(
061                            Role.class.getName(), modelActions);
062    
063                    // LPS-14202 and LPS-17841
064    
065                    RoleLocalServiceUtil.checkSystemRoles();
066    
067                    updatePermissions("com.liferay.portlet.bookmarks", true, true);
068                    updatePermissions("com.liferay.portlet.documentlibrary", false, true);
069                    updatePermissions("com.liferay.portlet.imagegallery", true, true);
070                    updatePermissions("com.liferay.portlet.messageboards", true, true);
071                    updatePermissions("com.liferay.portlet.shopping", true, true);
072            }
073    
074            protected void updatePermissions(
075                            String name, boolean community, boolean guest)
076                    throws Exception {
077    
078                    List<String> modelActions = ResourceActionsUtil.getModelResourceActions(
079                            name);
080    
081                    ResourceActionLocalServiceUtil.checkResourceActions(name, modelActions);
082    
083                    int scope = ResourceConstants.SCOPE_INDIVIDUAL;
084                    long actionIdsLong = 1;
085    
086                    if (community) {
087                            ResourcePermissionLocalServiceUtil.addResourcePermissions(
088                                    name, RoleConstants.SITE_MEMBER, scope, actionIdsLong);
089                            ResourcePermissionLocalServiceUtil.addResourcePermissions(
090                                    name, RoleConstants.ORGANIZATION_USER, scope, actionIdsLong);
091                    }
092    
093                    if (guest) {
094                            ResourcePermissionLocalServiceUtil.addResourcePermissions(
095                                    name, RoleConstants.GUEST, scope, actionIdsLong);
096                    }
097    
098                    ResourcePermissionLocalServiceUtil.addResourcePermissions(
099                            name, RoleConstants.OWNER, scope, actionIdsLong);
100            }
101    
102    }