use of cz.metacentrum.perun.core.api.Candidate in project perun by CESNET.
the class UsersManagerEntryIntegrationTest method convertAttributesToJSON.
@Test
public void convertAttributesToJSON() {
System.out.println(CLASS_NAME + "convertAttributesToJSON");
Candidate candidate = new Candidate(user, userExtSource);
candidate.setAttributes(Collections.singletonMap(perun.getAttributesManager().NS_USER_ATTR + ":attribute", "value"));
JSONObject jsonObject = candidate.convertAttributesToJSON();
assertEquals(8, jsonObject.length());
assertEquals("value", jsonObject.getJSONArray(perun.getAttributesManager().NS_USER_ATTR + ":attribute").getString(0));
assertEquals(userFirstName, jsonObject.getJSONArray(perun.getAttributesManager().NS_USER_ATTR_CORE + ":firstName").getString(0));
}
use of cz.metacentrum.perun.core.api.Candidate in project perun by CESNET.
the class UsersManagerEntryIntegrationTest method convertAttributesWithNullToJSON.
@Test
public void convertAttributesWithNullToJSON() {
System.out.println(CLASS_NAME + "convertAttributesWithNullToJSON");
Candidate candidate = new Candidate(user, userExtSource);
candidate.setAttributes(Collections.singletonMap(perun.getAttributesManager().NS_USER_ATTR + ":attribute", null));
JSONObject jsonObject = candidate.convertAttributesToJSON();
assertEquals(8, jsonObject.length());
assertTrue(jsonObject.getJSONArray(perun.getAttributesManager().NS_USER_ATTR + ":attribute").isNull(0));
}
use of cz.metacentrum.perun.core.api.Candidate in project perun by CESNET.
the class ExpirationNotifSchedulerTest method setUpMember.
private Member setUpMember() throws Exception {
Candidate candidate = new Candidate();
candidate.setFirstName(Long.toHexString(Double.doubleToLongBits(Math.random())));
candidate.setId(0);
candidate.setMiddleName("");
candidate.setLastName(Long.toHexString(Double.doubleToLongBits(Math.random())));
candidate.setTitleBefore("");
candidate.setTitleAfter("");
final UserExtSource userExtSource = new UserExtSource(extSource, Long.toHexString(Double.doubleToLongBits(Math.random())));
candidate.setUserExtSource(userExtSource);
candidate.setAttributes(new HashMap<>());
return perun.getMembersManagerBl().createMemberSync(session, vo, candidate);
}
use of cz.metacentrum.perun.core.api.Candidate 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());
}
use of cz.metacentrum.perun.core.api.Candidate in project perun by CESNET.
the class GroupsManagerEntryIntegrationTest method setUpMemberWithDifferentParam.
private Member setUpMemberWithDifferentParam(Vo vo, int i) throws Exception {
// List<Candidate> candidates = perun.getVosManager().findCandidates(sess, vo, extLogin);
// find candidates from ext source based on extLogin
// assertTrue(candidates.size() > 0);
Candidate candidate = setUpCandidate(i);
Member member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
// set first candidate as member of test VO
assertNotNull("No member created", member);
usersForDeletion.add(perun.getUsersManager().getUserByMember(sess, member));
// save user for deletion after test
return member;
}
Aggregations