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.kernel.test.util;
016    
017    import com.liferay.portal.exception.NoSuchRoleException;
018    import com.liferay.portal.kernel.test.randomizerbumpers.NumericStringRandomizerBumper;
019    import com.liferay.portal.kernel.test.randomizerbumpers.UniqueStringRandomizerBumper;
020    import com.liferay.portal.model.Role;
021    import com.liferay.portal.model.RoleConstants;
022    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
023    import com.liferay.portal.service.RoleLocalServiceUtil;
024    
025    /**
026     * @author Roberto D??az
027     */
028    public class RoleTestUtil {
029    
030            public static long addGroupRole(long groupId) throws Exception {
031                    Role role = addRole(RoleConstants.TYPE_SITE);
032    
033                    RoleLocalServiceUtil.addGroupRole(groupId, role.getRoleId());
034    
035                    return role.getRoleId();
036            }
037    
038            public static long addOrganizationRole(long groupId) throws Exception {
039                    Role role = addRole(RoleConstants.TYPE_ORGANIZATION);
040    
041                    RoleLocalServiceUtil.addGroupRole(groupId, role.getRoleId());
042    
043                    return role.getRoleId();
044            }
045    
046            public static long addRegularRole(long groupId) throws Exception {
047                    Role role = addRole(RoleConstants.TYPE_REGULAR);
048    
049                    RoleLocalServiceUtil.addGroupRole(groupId, role.getRoleId());
050    
051                    return role.getRoleId();
052            }
053    
054            public static void addResourcePermission(
055                            Role role, String resourceName, int scope, String primKey,
056                            String actionId)
057                    throws Exception {
058    
059                    ResourcePermissionLocalServiceUtil.addResourcePermission(
060                            role.getCompanyId(), resourceName, scope, primKey, role.getRoleId(),
061                            actionId);
062            }
063    
064            public static void addResourcePermission(
065                            String roleName, String resourceName, int scope, String primKey,
066                            String actionId)
067                    throws Exception {
068    
069                    Role role = RoleLocalServiceUtil.getRole(
070                            TestPropsValues.getCompanyId(), roleName);
071    
072                    addResourcePermission(role, resourceName, scope, primKey, actionId);
073            }
074    
075            public static Role addRole(int roleType) throws Exception {
076                    return addRole(
077                            RandomTestUtil.randomString(
078                                    NumericStringRandomizerBumper.INSTANCE,
079                                    UniqueStringRandomizerBumper.INSTANCE),
080                            roleType);
081            }
082    
083            public static Role addRole(String roleName, int roleType) throws Exception {
084                    Role role = null;
085    
086                    try {
087                            role = RoleLocalServiceUtil.getRole(
088                                    TestPropsValues.getCompanyId(), roleName);
089                    }
090                    catch (NoSuchRoleException nsre) {
091                            role = RoleLocalServiceUtil.addRole(
092                                    TestPropsValues.getUserId(), null, 0, roleName, null, null,
093                                    roleType, null, null);
094                    }
095    
096                    return role;
097            }
098    
099            public static Role addRole(
100                            String roleName, int roleType, String resourceName, int scope,
101                            String primKey, String actionId)
102                    throws Exception {
103    
104                    Role role = addRole(roleName, roleType);
105    
106                    addResourcePermission(role, resourceName, scope, primKey, actionId);
107    
108                    return role;
109            }
110    
111            public static void removeResourcePermission(
112                            String roleName, String resourceName, int scope, String primKey,
113                            String actionId)
114                    throws Exception {
115    
116                    Role role = RoleLocalServiceUtil.getRole(
117                            TestPropsValues.getCompanyId(), roleName);
118    
119                    ResourcePermissionLocalServiceUtil.removeResourcePermission(
120                            role.getCompanyId(), resourceName, scope, primKey, role.getRoleId(),
121                            actionId);
122            }
123    
124    }