PUMAAuthServiceImpl extends AbstractSSOAuthServiceImpl { protected void syncGroupsMembershipFromPUMA(XWikiDocument userProfile, User user, PumaLocator pl, PumaProfile pumaProfile, XWikiContext context) throws XWikiException { Map<String, Collection<String>> groupMappings = getConfig().getGroupMapping(context); if (groupMappings != null && groupMappings.size() > 0) { try { Collection<String> xwikiGroupsIn = new ArrayList<String>(); Collection<String> xwikiGroupsOut = new ArrayList<String>(); Map<String, Collection<String>> groupsToRemove = new HashMap<String, Collection<String>>(groupMappings); List<Group> pumaUserGroups = pl.findGroupsByPrincipal(user, true); LOG.debug("The user belongs to following PUMA groups: "); for (Group group : pumaUserGroups) { String groupUid = pumaProfile.getIdentifier(group); LOG.debug(" - " + groupUid); Collection<String> xwikiGroups = groupsToRemove.get(groupUid); if (xwikiGroups != null) { xwikiGroupsIn.addAll(xwikiGroups); groupsToRemove.remove(groupUid); } } for (Collection<String> xwikiGroups : groupsToRemove.values()) { xwikiGroupsOut.addAll(xwikiGroups); } syncGroupsMembership(userProfile, xwikiGroupsIn, xwikiGroupsOut, context); } catch (Exception e) { LOG.error("Failed to synchronize groups for user [" + userProfile + "]", e); } } }    }
@Test public void testSyncGroupsMembershipFromPUMAWhenAlreadySynced() throws Exception { final Group pumaGroup1 = this.mockery.mock(Group.class, "group1"); final Group pumaGroup2 = this.mockery.mock(Group.class, "group2"); this.mockery.checking(new Expectations() {{ allowing(xwikiMock).getXWikiPreference(with(any(String.class)), with(any(XWikiContext.class))); will(returnValue(group.getFullName() + "=cn=group,ou=groups,o=xwiki,c=org" + "|XWiki.group2=cn=group2,ou=groups,o=xwiki,c=org")); allowing(pumaLocatorMock).findGroupsByPrincipal(pumaUserMock, true); will(returnValue(Arrays.asList(pumaGroup1, pumaGroup2))); allowing(pumaProfileMock).getIdentifier(pumaGroup1); will(returnValue("cn=group,ou=groups,o=xwiki,c=org")); allowing(pumaProfileMock).getIdentifier(pumaGroup2); will(returnValue("cn=group3,ou=groups,o=xwiki,c=org")); allowing(xwikiMock).getGroupService(getContext()); will(returnValue(xwikiGroupServiceMock)); allowing(xwikiMock).getXClass(groupClass.getDocumentReference(), getContext()); will(returnValue(groupClass)); allowing(xwikiGroupServiceMock).getAllGroupsNamesForMember(user.getFullName(), 0, 0, getContext()); will(returnValue(Arrays.<String> asList(group.getFullName()))); }}); this.groupObject.setStringValue("member", this.user.getFullName()); this.authService.syncGroupsMembershipFromPUMA(this.user, this.pumaUserMock, this.pumaLocatorMock, this.pumaProfileMock, getContext()); }
@Test public void testSyncGroupsMembershipFromPUMA() throws Exception { final Group pumaGroup1 = this.mockery.mock(Group.class, "group1"); final Group pumaGroup2 = this.mockery.mock(Group.class, "group2"); this.mockery.checking(new Expectations() {{ allowing(xwikiMock).getXWikiPreference(with(any(String.class)), with(any(XWikiContext.class))); will(returnValue(group.getFullName() + "=cn=group,ou=groups,o=xwiki,c=org" + "|XWiki.group2=cn=group2,ou=groups,o=xwiki,c=org")); allowing(pumaLocatorMock).findGroupsByPrincipal(pumaUserMock, true); will(returnValue(Arrays.asList(pumaGroup1, pumaGroup2))); allowing(pumaProfileMock).getIdentifier(pumaGroup1); will(returnValue("cn=group,ou=groups,o=xwiki,c=org")); allowing(pumaProfileMock).getIdentifier(pumaGroup2); will(returnValue("cn=group3,ou=groups,o=xwiki,c=org")); allowing(xwikiMock).getGroupService(getContext()); will(returnValue(xwikiGroupServiceMock)); allowing(xwikiMock).getXClass(groupClass.getDocumentReference(), getContext()); will(returnValue(groupClass)); allowing(xwikiGroupServiceMock).getAllGroupsNamesForMember(user.getFullName(), 0, 0, getContext()); will(returnValue(Arrays.<String> asList())); oneOf(xwikiMock).getDocument(group.getFullName(), getContext()); will(returnValue(group)); oneOf(xwikiMock).getGroupClass(getContext()); will(returnValue(groupClass)); oneOf(xwikiMock).saveDocument(group, getContext()); }}); this.authService.syncGroupsMembershipFromPUMA(this.user, this.pumaUserMock, this.pumaLocatorMock, this.pumaProfileMock, getContext()); }