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.service.permission.test;
016    
017    import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
018    import com.liferay.portal.kernel.test.util.GroupTestUtil;
019    import com.liferay.portal.kernel.test.util.RoleTestUtil;
020    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
021    import com.liferay.portal.kernel.test.util.TestPropsValues;
022    import com.liferay.portal.kernel.test.util.UserTestUtil;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.ResourceConstants;
025    import com.liferay.portal.model.Role;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.security.permission.PermissionThreadLocal;
031    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
032    import com.liferay.portal.service.RoleLocalServiceUtil;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.test.ServiceTestUtil;
035    
036    import org.junit.After;
037    import org.junit.Before;
038    
039    /**
040     * @author Shinn Lok
041     */
042    public abstract class BasePermissionTestCase {
043    
044            @Before
045            public void setUp() throws Exception {
046                    group = GroupTestUtil.addGroup();
047                    user = UserTestUtil.addUser();
048    
049                    serviceContext = ServiceContextTestUtil.getServiceContext(
050                            group.getGroupId());
051    
052                    doSetUp();
053    
054                    ServiceTestUtil.setUser(user);
055    
056                    permissionChecker = PermissionThreadLocal.getPermissionChecker();
057    
058                    addPortletModelViewPermission();
059            }
060    
061            @After
062            public void tearDown() throws Exception {
063                    ServiceTestUtil.setUser(TestPropsValues.getUser());
064    
065                    removePortletModelViewPermission();
066            }
067    
068            protected void addPortletModelViewPermission() throws Exception {
069                    RoleTestUtil.addResourcePermission(
070                            getRoleName(), getResourceName(), ResourceConstants.SCOPE_GROUP,
071                            getPrimKey(), ActionKeys.VIEW);
072    
073                    Role role = RoleLocalServiceUtil.getRole(
074                            TestPropsValues.getCompanyId(), getRoleName());
075    
076                    ResourcePermissionLocalServiceUtil.setResourcePermissions(
077                            group.getCompanyId(), getResourceName(),
078                            ResourceConstants.SCOPE_INDIVIDUAL, getPrimKey(), role.getRoleId(),
079                            new String[] {ActionKeys.VIEW});
080            }
081    
082            protected abstract void doSetUp() throws Exception;
083    
084            protected String getPrimKey() {
085                    return String.valueOf(group.getGroupId());
086            }
087    
088            protected abstract String getResourceName();
089    
090            protected String getRoleName() {
091                    return RoleConstants.GUEST;
092            }
093    
094            protected void removePortletModelViewPermission() throws Exception {
095                    RoleTestUtil.removeResourcePermission(
096                            getRoleName(), getResourceName(), ResourceConstants.SCOPE_GROUP,
097                            getPrimKey(), ActionKeys.VIEW);
098    
099                    RoleTestUtil.removeResourcePermission(
100                            getRoleName(), getResourceName(),
101                            ResourceConstants.SCOPE_INDIVIDUAL, getPrimKey(), ActionKeys.VIEW);
102            }
103    
104            @DeleteAfterTestRun
105            protected Group group;
106    
107            protected PermissionChecker permissionChecker;
108            protected ServiceContext serviceContext;
109    
110            @DeleteAfterTestRun
111            protected User user;
112    
113    }