Search

Thursday, June 17, 2010

Community Roles for User Groups in Liferay

I have done massive investigation on User Group Roles and Permission. I have using same community roles for User Group to make working like community.
Resource resource = ResourceLocalServiceUtil.getResource(group.getCompanyId(), Group.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, Long.toString(group.getGroupId()));
System.out.println("resourceId : "+resource.getResourceId());
/*List actions =
ResourceActionsUtil.getModelResourceCommunityDefaultActions(
Group.class.getName());
String[] actionIds = actions.toArray(new String[actions.size()]);*/

String[] actionIds = new String[7];
actionIds[0] = "ASSIGN_MEMBERS";
actionIds[1] = "DELETE";
actionIds[2] = "MANAGE_ANNOUNCEMENTS";
actionIds[3] = "PERMISSIONS";
actionIds[4] = "MANAGE_LAYOUTS";
actionIds[5] = "UPDATE";
actionIds[6] = "VIEW";

PermissionLocalServiceUtil.setUserPermissions(group.getCreatorUserId(), actionIds, resource.getResourceId());

//PermissionLocalServiceUtil.addPermissions(group.getCompanyId(), actions, resource.getResourceId());

Role roleAdmin = RoleLocalServiceUtil.getRole(group.getCompanyId(), RoleConstants.COMMUNITY_ADMINISTRATOR);

PermissionLocalServiceUtil.setRolePermissions(roleAdmin.getRoleId(), actionIds, resource.getResourceId());

Role roleOwner = RoleLocalServiceUtil.getRole(group.getCompanyId(), RoleConstants.COMMUNITY_OWNER);

PermissionLocalServiceUtil.setRolePermissions(roleOwner.getRoleId(), actionIds, resource.getResourceId());

Added this code in EditUserGroupAction.java at the time of creating User Group.

With this User Group is start behaving like Community.

3 comments:

  1. I want to know what version of Liferay are you using.

    I'm using Liferay 6, and I'm always getting resource.getResourceId() = 0. May it's a LR6 bug.

    Thanks! Nice Blog!

    ReplyDelete
  2. In LR6 I solved it using

    ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, name,scope, primKey, roleId, actionIds);

    istead of

    PermissionLocalServiceUtil.setRolePermissions

    ReplyDelete
  3. Thank you very much for comments.

    The above solution is for LR5.2.3.

    Thanks,
    S Mohammed Zaheer

    ReplyDelete