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