use of cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException in project perun by CESNET.
the class Utils method parseAdditionalUserExtSource.
/**
* Returns additional user ext source either found in Perun or creates new. Parameter userExtSourceRaw is array of
* Strings containing name, type and extLogin. If any of the required parts is empty, ParserException is thrown.
* Used in extractAdditionalUserExtSources to get ues.
*
* @param sess perun session
* @param userExtSourceRaw array of strings containing all parts of ues
* @return UserExtSource additional ues
*/
private static UserExtSource parseAdditionalUserExtSource(PerunSession sess, String[] userExtSourceRaw) {
// Get extLogin from 3rd part of userExtSourceRaw as well as ues attributes, so it needs to be parsed from it
String extLogin = userExtSourceRaw[2].split(";")[0];
// Check whether any of the required parts of ues are not empty
if (userExtSourceRaw[0].isEmpty() || userExtSourceRaw[1].isEmpty() || extLogin.isEmpty()) {
throw new ParserException("Some of the required parts of userExtSource are empty.");
}
ExtSource additionalExtSource;
try {
// Try to get extSource, with full extSource object (containg ID)
additionalExtSource = ((PerunBl) sess.getPerun()).getExtSourcesManagerBl().getExtSourceByName(sess, userExtSourceRaw[0]);
} catch (ExtSourceNotExistsException e) {
try {
// Create new one if not exists
additionalExtSource = new ExtSource(userExtSourceRaw[0], userExtSourceRaw[1]);
additionalExtSource = ((PerunBl) sess.getPerun()).getExtSourcesManagerBl().createExtSource(sess, additionalExtSource, null);
} catch (ExtSourceExistsException e1) {
throw new ConsistencyErrorException("Creating existing extSource: " + userExtSourceRaw[0]);
}
}
// Get optional LoA (0 if not stated)
int loa = parseAdditionalUESLoa(userExtSourceRaw);
return new UserExtSource(additionalExtSource, loa, extLogin);
}
use of cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException in project perun by CESNET.
the class DummyPasswordManagerModule method validatePassword.
@Override
public void validatePassword(PerunSession sess, String userLogin, User user) throws InvalidLoginException {
log.debug("validatePassword(userLogin={})", userLogin);
if (user == null) {
user = ((PerunBl) sess.getPerun()).getModulesUtilsBl().getUserByLoginInNamespace(sess, userLogin, "dummy");
}
if (user == null) {
log.warn("No user was found by login '{}' in {} namespace.", userLogin, "dummy");
} else {
// set extSources and extSource related attributes
ExtSource extSource;
try {
extSource = ((PerunBl) sess.getPerun()).getExtSourcesManagerBl().getExtSourceByName(sess, "https://dummy");
} catch (ExtSourceNotExistsException e) {
extSource = new ExtSource("https://dummy", ExtSourcesManager.EXTSOURCE_IDP);
try {
extSource = ((PerunBl) sess.getPerun()).getExtSourcesManagerBl().createExtSource(sess, extSource, null);
} catch (ExtSourceExistsException e1) {
log.warn("impossible or race condition", e1);
}
}
UserExtSource ues = new UserExtSource(extSource, userLogin + "@dummy");
ues.setLoa(2);
try {
((PerunBl) sess.getPerun()).getUsersManagerBl().addUserExtSource(sess, user, ues);
} catch (UserExtSourceExistsException ex) {
// this is OK
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException in project perun by CESNET.
the class ExtSourcesManagerBlImpl method getCandidate.
@Override
public Candidate getCandidate(PerunSession sess, ExtSource source, String login) throws InternalErrorException, ExtSourceNotExistsException, CandidateNotExistsException, ExtSourceUnsupportedOperationException {
// New Canddate
Candidate candidate = new Candidate();
// Prepare userExtSource object
UserExtSource userExtSource = new UserExtSource();
userExtSource.setExtSource(source);
userExtSource.setLogin(login);
// Set the userExtSource
candidate.setUserExtSource(userExtSource);
// Get the subject from the extSource
Map<String, String> subject = null;
try {
subject = ((ExtSourceSimpleApi) source).getSubjectByLogin(login);
} catch (SubjectNotExistsException e) {
throw new CandidateNotExistsException(login);
}
if (subject == null) {
throw new CandidateNotExistsException("Candidate with login [" + login + "] not exists");
}
//If first name of candidate is not in format of name, set null instead
candidate.setFirstName(subject.get("firstName"));
if (candidate.getFirstName() != null) {
Matcher name = namePattern.matcher(candidate.getFirstName());
if (!name.matches())
candidate.setFirstName(null);
}
//If last name of candidate is not in format of name, set null instead
candidate.setLastName(subject.get("lastName"));
if (candidate.getLastName() != null) {
Matcher name = namePattern.matcher(candidate.getLastName());
if (!name.matches())
candidate.setLastName(null);
}
candidate.setMiddleName(subject.get("middleName"));
candidate.setTitleAfter(subject.get("titleAfter"));
candidate.setTitleBefore(subject.get("titleBefore"));
//Set service user
if (subject.get("isServiceUser") == null) {
candidate.setServiceUser(false);
} else {
String isServiceUser = subject.get("isServiceUser");
if (isServiceUser.equals("true")) {
candidate.setServiceUser(true);
} else {
candidate.setServiceUser(false);
}
}
//Set sponsored user
if (subject.get("isSponsoredUser") == null) {
candidate.setSponsoredUser(false);
} else {
String isSponsoredUser = subject.get("isSponsoredUser");
if (isSponsoredUser.equals("true")) {
candidate.setSponsoredUser(true);
} else {
candidate.setSponsoredUser(false);
}
}
// Additional userExtSources
List<UserExtSource> additionalUserExtSources = new ArrayList<UserExtSource>();
// Filter attributes
Map<String, String> attributes = new HashMap<String, String>();
for (String attrName : subject.keySet()) {
// FIXME volat metody z attributesManagera nez kontrolovat na zacatek jmena
if (attrName.startsWith(AttributesManager.NS_MEMBER_ATTR) || attrName.startsWith(AttributesManager.NS_USER_ATTR)) {
attributes.put(attrName, subject.get(attrName));
} else if (attrName.startsWith(ExtSourcesManagerImpl.USEREXTSOURCEMAPPING)) {
//skip null additional ext sources
if (subject.get(attrName) == null)
continue;
// Add additionalUserExtSources
// Entry contains extSourceName|extSourceType|extLogin[|LoA]
String[] userExtSourceRaw = subject.get(attrName).split("\\|");
log.debug("Processing additionalUserExtSource {}", subject.get(attrName));
//Check if the array has at least 3 parts, this is protection against outOfBoundException
if (userExtSourceRaw.length < 3) {
throw new InternalErrorException("There is missing some mandatory part of additional user extSource value when processing it - '" + attrName + "'");
}
String additionalExtSourceName = userExtSourceRaw[0];
String additionalExtSourceType = userExtSourceRaw[1];
String additionalExtLogin = userExtSourceRaw[2];
int additionalExtLoa = 0;
//Loa is not mandatory argument
if (userExtSourceRaw.length > 3 && userExtSourceRaw[3] != null) {
try {
additionalExtLoa = Integer.parseInt(userExtSourceRaw[3]);
} catch (NumberFormatException e) {
throw new ParserException("Candidate with login [" + login + "] has wrong LoA '" + userExtSourceRaw[3] + "'.", e, "LoA");
}
}
ExtSource additionalExtSource;
if (additionalExtSourceName == null || additionalExtSourceName.isEmpty() || additionalExtSourceType == null || additionalExtSourceType.isEmpty() || additionalExtLogin == null || additionalExtLogin.isEmpty()) {
log.error("User with login {} has invalid additional userExtSource defined {}.", login, userExtSourceRaw);
} else {
try {
// Try to get extSource, with full extSource object (containg ID)
additionalExtSource = getPerunBl().getExtSourcesManagerBl().getExtSourceByName(sess, additionalExtSourceName);
} catch (ExtSourceNotExistsException e) {
try {
// Create new one if not exists
additionalExtSource = new ExtSource(additionalExtSourceName, additionalExtSourceType);
additionalExtSource = getPerunBl().getExtSourcesManagerBl().createExtSource(sess, additionalExtSource, null);
} catch (ExtSourceExistsException e1) {
throw new ConsistencyErrorException("Creating existin extSource: " + additionalExtSourceName);
}
}
//add additional user extSource
additionalUserExtSources.add(new UserExtSource(additionalExtSource, additionalExtLoa, additionalExtLogin));
}
}
}
candidate.setAdditionalUserExtSources(additionalUserExtSources);
candidate.setAttributes(attributes);
return candidate;
}
use of cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException in project perun by CESNET.
the class ExtSourcesManagerImpl method createExtSource.
@Override
public ExtSource createExtSource(PerunSession sess, ExtSource extSource, Map<String, String> attributes) throws ExtSourceExistsException {
Utils.notNull(extSource.getName(), "extSource.getName()");
Utils.notNull(extSource.getType(), "extSource.getType()");
try {
// Check if the extSources already exists
if (0 < jdbc.queryForInt("select count(id) from ext_sources where name=? and type=?", extSource.getName(), extSource.getType())) {
throw new ExtSourceExistsException(extSource);
}
// Get a new Id
int newId = Utils.getNewId(jdbc, "ext_sources_id_seq");
jdbc.update("insert into ext_sources (id, name, type, created_by,created_at,modified_by,modified_at,created_by_uid,modified_by_uid) " + "values (?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", newId, extSource.getName(), extSource.getType(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
extSource.setId(newId);
ExtSource es;
// Get the instance by the type of the extSource
try {
Class<?> extSourceClass = Class.forName(extSource.getType());
es = (ExtSource) extSourceClass.newInstance();
} catch (ClassNotFoundException e) {
throw new InternalErrorException(e);
} catch (InstantiationException | IllegalAccessException e) {
throw new InternalErrorException(e);
}
// Set the properties
es.setId(extSource.getId());
es.setName(extSource.getName());
es.setType(extSource.getType());
// Now store the attributes
if (attributes != null) {
for (String attr_name : attributes.keySet()) {
jdbc.update("insert into ext_sources_attributes (attr_name, attr_value, ext_sources_id,created_by, created_at, modified_by, modified_at, created_by_uid, modified_by_uid) " + "values (?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", attr_name, attributes.get(attr_name), extSource.getId(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
}
}
// Assign newly created extSource
extSource = es;
return extSource;
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
Aggregations