This may be of value to the community, I found this useful today for completing approximately 3000 member and role assignment transactions. In summary, We had some 300 users that needed to be assigned to approximately 6 organisations each e.g. students assigned as members to a class. Once they were members we needed to assign them a organisation role.
Obviously there are far better ways to do this level of integration e..g IDM, LDAP etc but in our current situation this is a work in progress. We created a simple beanshell script that took a CSV list of User and Organisation and completed the task for us rather than the manual assignment. The beanshell script was executed via the normal method in Control Panel, and I guess could be extended if required for additional role assignments.
1
2import com.liferay.portal.service.UserLocalServiceUtil;
3import com.liferay.portal.service.OrganizationLocalServiceUtil;
4import com.liferay.portal.service.RoleLocalServiceUtil;
5import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
6
7companyId = COMPANYID;
8studentOrgRoleName = "ROLENAME";
9
10userToGrps = new String[] { "ORGNAME,SCREENNAME","ORGNAME,SCREENNAME",};
11studentOrgRole = RoleLocalServiceUtil.getRole(companyId, studentOrgRoleName);
12
13for(int i=0; i<userToGrps.length; i++) {
14userToGrp = userToGrps[i];
15userGrpDetail = userToGrp.split(",");
16
17out.println("user: " + userGrpDetail[0] + ", grp: " + userGrpDetail[1]);
18
19try {
20 user = UserLocalServiceUtil.getUserByScreenName(companyId, userGrpDetail[1]);
21 org = OrganizationLocalServiceUtil.getOrganization(companyId, userGrpDetail[0]);
22
23 //add user as member to org
24
25 if(!UserLocalServiceUtil.hasOrganizationUser(org.getOrganizationId(), user.getUserId())) {
26 UserLocalServiceUtil.addOrganizationUsers(org.getOrganizationId(), new long[] {user.getUserId()});
27 out.println("adding user: " + userGrpDetail[0] + " to grp: " + userGrpDetail[1]);
28}
29
30//add org role to user
31
32if(!UserGroupRoleLocalServiceUtil.hasUserGroupRole(user.getUserId(), org.getGroup().getGroupId(), studentOrgRole.getRoleId())) {
33 UserGroupRoleLocalServiceUtil.addUserGroupRoles(user.getUserId(), org.getGroup().getGroupId(), new long[] {studentOrgRole.getRoleId(),});
34
35 out.println("adding org role: " +studentOrgRoleName+ " to user: " + userGrpDetail[1]);
36 }
37}
38 catch(Exception e) { out.println(e.getMessage()); }
39}