use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class VosManagerBlImpl method findCandidates.
public List<Candidate> findCandidates(PerunSession sess, Group group, String searchString) throws InternalErrorException {
List<Candidate> candidates = new ArrayList<>();
try {
// Iterate through all registered extSources in the group
for (ExtSource source : getPerunBl().getExtSourcesManagerBl().getGroupExtSources(sess, group)) {
// Info if this is only simple ext source, change behavior if not
boolean simpleExtSource = true;
// Get potential subjects from the extSource
List<Map<String, String>> subjects;
try {
if (source instanceof ExtSourceApi) {
// find subjects with all their properties
subjects = ((ExtSourceApi) source).findSubjects(searchString);
simpleExtSource = false;
} else {
// find subjects only with logins - they then must be retrieved by login
subjects = ((ExtSourceSimpleApi) source).findSubjectsLogins(searchString);
}
} catch (ExtSourceUnsupportedOperationException e1) {
log.warn("ExtSource {} doesn't support findSubjects", source.getName());
continue;
} catch (InternalErrorException e) {
log.error("Error occurred on ExtSource {}, Exception {}.", source.getName(), e);
continue;
} finally {
try {
((ExtSourceSimpleApi) source).close();
} catch (ExtSourceUnsupportedOperationException e) {
// ExtSource doesn't support that functionality, so silently skip it.
} catch (InternalErrorException e) {
log.error("Can't close extSource connection. Cause: {}", e);
}
}
Set<String> uniqueLogins = new HashSet<>();
for (Map<String, String> s : subjects) {
// Check if the user has unique identifier within extSource
if ((s.get("login") == null) || (s.get("login") != null && ((String) s.get("login")).isEmpty())) {
log.error("User '{}' cannot be added, because he/she doesn't have a unique identifier (login)", s);
// Skip to another user
continue;
}
String extLogin = (String) s.get("login");
// check uniqueness of every login in extSource
if (uniqueLogins.contains(extLogin)) {
throw new InternalErrorException("There are more than 1 login '" + extLogin + "' getting from extSource '" + source + "'");
} else {
uniqueLogins.add(extLogin);
}
// Get Candidate
Candidate candidate;
try {
if (simpleExtSource) {
// retrieve data about subjects from ext source based on ext. login
candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, source, extLogin);
} else {
// retrieve data about subjects from subjects we already have locally
candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, s, source, extLogin);
}
} catch (ExtSourceNotExistsException e) {
throw new ConsistencyErrorException("Getting candidate from non-existing extSource " + source, e);
} catch (CandidateNotExistsException e) {
throw new ConsistencyErrorException("findSubjects returned that candidate, but getCandidate cannot find him using login " + extLogin, e);
} catch (ExtSourceUnsupportedOperationException e) {
throw new InternalErrorException("extSource supports findSubjects but not getCandidate???", e);
}
try {
Vo vo = getPerunBl().getVosManagerBl().getVoById(sess, group.getVoId());
getPerunBl().getMembersManagerBl().getMemberByUserExtSources(sess, vo, candidate.getUserExtSources());
// Candidate is already a member of the VO, so do not add him to the list of candidates
continue;
} catch (VoNotExistsException e) {
throw new InternalErrorException(e);
} catch (MemberNotExistsException e) {
// This is OK
}
// Add candidate to the list of candidates
log.debug("findCandidates: returning candidate: {}", candidate);
candidates.add(candidate);
}
}
log.debug("Returning {} potential members for group {}", candidates.size(), group);
return candidates;
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class urn_perun_group_attribute_def_def_groupExtSource method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl sess, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
//prepare groupName value variable
String extSourceName = null;
if (attribute.getValue() != null)
extSourceName = (String) attribute.getValue();
if (extSourceName == null) {
//attribute can be removed
return;
} else {
try {
Vo groupVo = sess.getPerunBl().getVosManagerBl().getVoById(sess, group.getVoId());
List<ExtSource> allowedExtSources = sess.getPerunBl().getExtSourcesManagerBl().getVoExtSources(sess, groupVo);
for (ExtSource es : allowedExtSources) {
if (extSourceName.equals(es.getName()))
return;
}
throw new WrongAttributeValueException(attribute, group, "ExtSourceName " + extSourceName + " is not valid, because VO " + groupVo + " of this group has no such extSource assigned.");
} catch (VoNotExistsException ex) {
throw new ConsistencyErrorException("Vo of this group " + group + " not exists!");
}
}
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class VosManagerEntryIntegrationTest method getAdmins.
@Test
public void getAdmins() throws Exception {
System.out.println(CLASS_NAME + "getAdmins");
final Vo createdVo = vosManagerEntry.createVo(sess, myVo);
// set up first user
final Member member = createMemberFromExtSource(createdVo);
User user = perun.getUsersManagerBl().getUserByMember(sess, member);
vosManagerEntry.addAdmin(sess, createdVo, user);
// set up authorized group
Group authorizedGroup = new Group("authorizedGroup", "testovaciGroup");
Group returnedGroup = perun.getGroupsManager().createGroup(sess, createdVo, authorizedGroup);
vosManagerEntry.addAdmin(sess, createdVo, returnedGroup);
// set up second user
//Mockito.mock(Candidate.class);
Candidate candidate = new Candidate();
candidate.setFirstName("Josef");
candidate.setId(4);
candidate.setMiddleName("");
candidate.setLastName("Novak");
candidate.setTitleBefore("");
candidate.setTitleAfter("");
UserExtSource userExtSource = new UserExtSource(new ExtSource(0, "testExtSource", "cz.metacentrum.perun.core.impl.ExtSourceInternal"), Long.toHexString(Double.doubleToLongBits(Math.random())));
candidate.setUserExtSource(userExtSource);
candidate.setAttributes(new HashMap<String, String>());
Member member2 = perun.getMembersManagerBl().createMemberSync(sess, createdVo, candidate);
User user2 = perun.getUsersManagerBl().getUserByMember(sess, member2);
perun.getGroupsManager().addMember(sess, returnedGroup, member2);
// test
List<User> admins = vosManagerEntry.getAdmins(sess, createdVo);
assertTrue("should have 2 admins", admins.size() == 2);
assertTrue("our member as direct user should be admin", admins.contains(user));
assertTrue("our member as member of admin group should be admin", admins.contains(user2));
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class UsersManagerEntryIntegrationTest method addIDPExtSourcesWithSameLogin.
@Test(expected = InternalErrorException.class)
public void addIDPExtSourcesWithSameLogin() throws Exception {
System.out.println(CLASS_NAME + "addIDPExtSourcesWithSameLogin");
ExtSource ext1 = new ExtSource("test1", ExtSourcesManagerEntry.EXTSOURCE_IDP);
ExtSource ext2 = new ExtSource("test2", ExtSourcesManagerEntry.EXTSOURCE_IDP);
ext1 = perun.getExtSourcesManagerBl().createExtSource(sess, ext1, null);
ext2 = perun.getExtSourcesManagerBl().createExtSource(sess, ext2, null);
UserExtSource ues1 = new UserExtSource(ext1, 1, "testExtLogin@test");
UserExtSource ues2 = new UserExtSource(ext2, 1, "testExtLogin@test");
ues1 = usersManager.addUserExtSource(sess, user, ues1);
ues2 = usersManager.addUserExtSource(sess, user, ues2);
}
use of cz.metacentrum.perun.core.api.ExtSource in project perun by CESNET.
the class UsersManagerEntryIntegrationTest method removeUserExtSourcePersistent.
@Test(expected = InternalErrorException.class)
public void removeUserExtSourcePersistent() throws Exception {
System.out.println(CLASS_NAME + "removeUserExtSourcePersistent");
// Assuming ExtSource PERUN is persistent (set as property)
ExtSource extSource = perun.getExtSourcesManagerBl().getExtSourceByName(sess, "PERUN");
List<UserExtSource> userExtSources = usersManager.getUserExtSources(sess, user);
for (UserExtSource ues : userExtSources) {
if (ues.getExtSource().equals(extSource)) {
usersManager.removeUserExtSource(sess, user, ues);
break;
}
}
}
Aggregations