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