use of cz.metacentrum.perun.core.api.RichUserExtSource in project perun by CESNET.
the class UtilsIntegrationTest method extractAdditionalUserExtSourcesWithAttributeTest.
@Test
public void extractAdditionalUserExtSourcesWithAttributeTest() throws Exception {
System.out.println("Utils.extractAdditionalUserExtSourcesWithAttributeTest");
Map<String, String> map = new HashMap<>();
map.put("additionalues_b", extSourceName2 + "|cz.metacentrum.perun.core.impl.ExtSourceInternal|" + extLogin2 + ";urn:perun:ues:attribute-def:def:eppn=" + extLogin2 + "|2");
AttributeDefinition attributeDefinition = new AttributeDefinition();
attributeDefinition.setNamespace("urn:perun:ues:attribute-def:def");
attributeDefinition.setFriendlyName("eppn");
attributeDefinition.setDescription("login value");
attributeDefinition.setType(String.class.getName());
sess.getPerun().getAttributesManager().createAttribute(sess, attributeDefinition);
List<RichUserExtSource> list = Utils.extractAdditionalUserExtSources(sess, map);
assertEquals(list.size(), 1);
assertTrue(list.contains(new RichUserExtSource(userExtSource2, Arrays.asList(new Attribute(attributeDefinition, extLogin2)))));
}
use of cz.metacentrum.perun.core.api.RichUserExtSource in project perun by CESNET.
the class GroupsManagerBlImpl method categorizeMembersForLightweightSynchronization.
/**
* For lightweight synchronization prepare candidate to add and members to remove.
*
* Get all subjects from loginSource and try to find users in Perun by their login and this ExtSource.
* If found, look if this user is already in synchronized Group. If yes skip him, if not add him to candidateToAdd
* If not found in vo of the group, skip him.
*
* Rest of former members need to be add to membersToRemove to remove them from group.
*
* This method fill 2 member structures which get as parameters:
* 1. candidateToAdd - New members of the group
* 2. membersToRemove - Former members who are not in synchronized ExtSource now
*
* @param sess
* @param group
* @param loginSource
* @param memberSource
* @param groupMembers
* @param candidatesToAdd
* @param membersToRemove
* @param skippedMembers
*/
private void categorizeMembersForLightweightSynchronization(PerunSession sess, Group group, ExtSource loginSource, ExtSource memberSource, List<RichMember> groupMembers, List<Candidate> candidatesToAdd, List<RichMember> membersToRemove, List<String> skippedMembers) {
// Get subjects from loginSource
List<Map<String, String>> subjects = getSubjectsFromExtSource(sess, loginSource, group);
// Prepare structure of userIds with richMembers to better work with actual members
Map<Integer, RichMember> idsOfUsersInGroup = new HashMap<>();
for (RichMember richMember : groupMembers) {
idsOfUsersInGroup.put(richMember.getUserId(), richMember);
}
// try to find users by login and loginSource
for (Map<String, String> subjectFromLoginSource : subjects) {
if (subjectFromLoginSource == null) {
log.error("Null value in the subjects list. Skipping.");
continue;
}
String login = subjectFromLoginSource.get("login");
// Skip subjects, which doesn't have login
if (login == null || login.isEmpty()) {
log.debug("Subject {} doesn't contain attribute login, skipping.", subjectFromLoginSource);
skippedMembers.add("MemberEntry:[" + subjectFromLoginSource + "] was skipped because login is missing");
continue;
}
// try to find user from perun by login and member extSource (need to use memberSource because loginSource is not saved by synchronization)
User user = null;
List<UserExtSource> userExtSources = new ArrayList<>();
try {
UserExtSource userExtSource = getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, memberSource, login);
userExtSources.add(userExtSource);
} catch (UserExtSourceNotExistsException e) {
// skipping, this extSource does not exist and thus won't be in the list
}
Vo groupVo = getVo(sess, group);
List<UserExtSource> additionalUserExtSources = Utils.extractAdditionalUserExtSources(sess, subjectFromLoginSource).stream().map(RichUserExtSource::asUserExtSource).collect(toList());
userExtSources.addAll(additionalUserExtSources);
for (UserExtSource source : userExtSources) {
try {
user = getPerunBl().getUsersManagerBl().getUserByUserExtSource(sess, source);
// check if user is already member of group's vo
if (getPerunBl().getUsersManagerBl().getVosWhereUserIsMember(sess, user).contains(groupVo)) {
if (idsOfUsersInGroup.containsKey(user.getId())) {
// we can skip this one, because he is already in group, and remove him from the map
// but first we need to also validate him if he was disabled before (invalidate and then validate)
RichMember richMember = idsOfUsersInGroup.get(user.getId());
if (richMember != null && Status.DISABLED.equals(richMember.getStatus())) {
getPerunBl().getMembersManagerBl().invalidateMember(sess, richMember);
try {
getPerunBl().getMembersManagerBl().validateMember(sess, richMember);
} catch (WrongAttributeValueException | WrongReferenceAttributeValueException e) {
log.info("Switching member id {} into INVALID state from DISABLED, because there was problem with attributes {}.", richMember.getId(), e);
}
}
idsOfUsersInGroup.remove(user.getId());
} else {
// he is not yet in group, so we need to create a candidate
Candidate candidate = new Candidate(user, source);
// for lightweight synchronization we want to skip all update of attributes
candidate.setAttributes(new HashMap<>());
candidatesToAdd.add(candidate);
}
break;
}
} catch (UserNotExistsException e) {
// skip because the user from this ExtSource does not exist so we can continue
}
}
// If user not found in group's vo, skip him and log it
if (user == null) {
log.debug("Subject {} with login {} was skipped during lightweight synchronization of group {} because he is not in vo of the group yet.", subjectFromLoginSource, login, group);
}
}
// Rest of them need to be removed
membersToRemove.addAll(idsOfUsersInGroup.values());
}
Aggregations