001    /**
002     * Copyright (c) 2000-2012 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()
039                    // requires an updated Company table
040    
041                    runSQL("alter table Company add active_ BOOLEAN");
042                    runSQL("update Company set active_ = TRUE");
043    
044                    // LPS-14202 and LPS-17841
045    
046                    RoleLocalServiceUtil.checkSystemRoles();
047    
048                    updatePermissions("com.liferay.portlet.bookmarks", true, true);
049                    updatePermissions("com.liferay.portlet.documentlibrary", false, true);
050                    updatePermissions("com.liferay.portlet.imagegallery", true, true);
051                    updatePermissions("com.liferay.portlet.messageboards", true, true);
052                    updatePermissions("com.liferay.portlet.shopping", true, true);
053            }
054    
055            protected void updatePermissions(
056                            String name, boolean community, boolean guest)
057                    throws Exception {
058    
059                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
060                            updatePermissions_6(name, community, guest);
061                    }
062                    else {
063                            updatePermissions_1to5(name, community, guest);
064                    }
065            }
066    
067            protected void updatePermissions_1to5(
068                            String name, boolean community, boolean guest)
069                    throws Exception {
070    
071                    if (community) {
072                            PermissionLocalServiceUtil.setContainerResourcePermissions(
073                                    name, _COMMUNITY_MEMBER, ActionKeys.VIEW);
074                            PermissionLocalServiceUtil.setContainerResourcePermissions(
075                                    name, _ORGANIZATION_MEMBER, ActionKeys.VIEW);
076                    }
077    
078                    if (guest) {
079                            PermissionLocalServiceUtil.setContainerResourcePermissions(
080                                    name, RoleConstants.GUEST, ActionKeys.VIEW);
081                    }
082    
083                    PermissionLocalServiceUtil.setContainerResourcePermissions(
084                            name, RoleConstants.OWNER, ActionKeys.VIEW);
085            }
086    
087            protected void updatePermissions_6(
088                            String name, boolean community, boolean guest)
089                    throws Exception {
090    
091                    List<String> modelActions =
092                            ResourceActionsUtil.getModelResourceActions(name);
093    
094                    ResourceActionLocalServiceUtil.checkResourceActions(name, modelActions);
095    
096                    int scope = ResourceConstants.SCOPE_INDIVIDUAL;
097                    long actionIdsLong = 1;
098    
099                    if (community) {
100                            ResourcePermissionLocalServiceUtil.addResourcePermissions(
101                                    name, _COMMUNITY_MEMBER, scope, actionIdsLong);
102                            ResourcePermissionLocalServiceUtil.addResourcePermissions(
103                                    name, _ORGANIZATION_MEMBER, scope, actionIdsLong);
104                    }
105    
106                    if (guest) {
107                            ResourcePermissionLocalServiceUtil.addResourcePermissions(
108                                    name, RoleConstants.GUEST, scope, actionIdsLong);
109                    }
110    
111                    ResourcePermissionLocalServiceUtil.addResourcePermissions(
112                            name, RoleConstants.OWNER, scope, actionIdsLong);
113            }
114    
115            private static final String _COMMUNITY_MEMBER = "Community Member";
116    
117            private static final String _ORGANIZATION_MEMBER = "Organization Member";
118    
119    }