Skip to content

Instantly share code, notes, and snippets.

@nshaw
Created February 20, 2013 17:38
Show Gist options
  • Save nshaw/4997388 to your computer and use it in GitHub Desktop.
Save nshaw/4997388 to your computer and use it in GitHub Desktop.
Add MDR permissions to users with the role Content Editor.
import com.liferay.portal.service.RoleLocalServiceUtil
import com.liferay.portal.service.ResourcePermissionLocalServiceUtil
import com.liferay.portal.service.ResourceActionLocalServiceUtil
import com.liferay.portal.security.permission.ActionKeys
import com.liferay.portal.model.ResourceAction
import com.liferay.portal.util.PortletKeys
Map<String, String[]> portletSets = new TreeMap<String, String[]>();
// Mobile Device Rules
portletSets.put(PortletKeys.MOBILE_DEVICE_SITE_ADMIN,
[ActionKeys.ACCESS_IN_CONTROL_PANEL, ActionKeys.ADD_TO_PAGE,
ActionKeys.CONFIGURATION, ActionKeys.PERMISSIONS,
ActionKeys.VIEW].toArray(new String[0]));
portletSets.put("com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup",
[ActionKeys.DELETE, ActionKeys.PERMISSIONS, ActionKeys.UPDATE,
ActionKeys.VIEW].toArray(new String[0]));
portletSets.put("com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance",
[ActionKeys.DELETE, ActionKeys.PERMISSIONS, ActionKeys.UPDATE,
ActionKeys.VIEW].toArray(new String[0]));
portletSets.put("com.liferay.portlet.mobiledevicerules",
[ActionKeys.ADD_RULE_GROUP, ActionKeys.ADD_RULE_GROUP_INSTANCE,
ActionKeys.CONFIGURATION, ActionKeys.PERMISSIONS,
ActionKeys.VIEW].toArray(new String[0]));
println "--------"
println " EDITOR PERMISSIONS"
println "----"
updateRolePermissions("Content Editor", portletSets)
// This only adds permissions to the role, it does not remove any existing permissions
def updateRolePermissions(roleName, Map<String, String[]> portletSets) {
long companyId = 10095
int scope = 3
String primKey = 0
role = RoleLocalServiceUtil.getRole(companyId, roleName)
long roleId = role.roleId
println "--------"
println "Found role: " + role.name + ", roleId: " + role.roleId
println "---"
for (portletSet in portletSets) {
String name = portletSet.key
String[] actionIds = portletSet.value
println "### " + name;
try {
perm = ResourcePermissionLocalServiceUtil.getResourcePermission(companyId, name,scope, primKey, roleId)
long ids = perm.getActionIds();
for (action in actionIds) {
ResourceAction resourceAction =
ResourceActionLocalServiceUtil.fetchResourceAction(
name, action);
ids = ids | resourceAction.bitwiseValue;
}
perm.setActionIds(ids)
println("Updating role permissions: " + actionIds + ", ids:" + ids)
ResourcePermissionLocalServiceUtil.updateResourcePermission(perm);
}
catch (Exception e){
println (e.message)
println("Setting new role permissions: " + actionIds)
ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name, scope, primKey, roleId, actionIds)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment