use of cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_login_namespace_researcher_access_persistent_shadow method changedAttributeHook.
/**
* ChangedAttributeHook() sets UserExtSource with following properties:
* - extSourceType is IdP
* - extSourceName is {getExtSourceName()}
* - user's extSource login is the same as his persistent attribute
*/
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) {
try {
String userNamespace = attribute.getFriendlyNameParameter();
if (userNamespace.equals(FRIENDLY_NAME_PARAMETER) && attribute.getValue() != null && !attribute.valueAsString().isEmpty()) {
ExtSource extSource = session.getPerunBl().getExtSourcesManagerBl().getExtSourceByName(session, getExtSourceName());
UserExtSource userExtSource = new UserExtSource(extSource, 0, attribute.getValue().toString());
session.getPerunBl().getUsersManagerBl().addUserExtSource(session, user, userExtSource);
}
} catch (UserExtSourceExistsException ex) {
log.warn("Attribute: {}, External source already exists for the user.", FRIENDLY_NAME_PARAMETER, ex);
} catch (ExtSourceNotExistsException ex) {
throw new InternalErrorException("Attribute: " + FRIENDLY_NAME_PARAMETER + ", IdP external source doesn't exist.", ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException 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.ExtSourceNotExistsException in project perun by CESNET.
the class ExtSourcesManagerImpl method loadExtSourcesDefinitions.
/**
* Loads the extSources definitions from the XML configuration file.
* All data from the extSouces XML file are synchronized with the DB.
*/
@Override
public void loadExtSourcesDefinitions(PerunSession sess) {
try {
// Load the XML file
BufferedInputStream is = new BufferedInputStream(new FileInputStream(ExtSourcesManager.CONFIGURATIONFILE));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
// Check if the root element is "extSources"
if (!doc.getDocumentElement().getNodeName().equals("extSources")) {
throw new InternalErrorException("perun-extSources.xml doesn't contain extSources as root element");
}
// Get all defined extSources
NodeList extSourcesNodes = doc.getElementsByTagName("extSource");
for (int extSourceSeq = 0; extSourceSeq < extSourcesNodes.getLength(); extSourceSeq++) {
// Get each extSource
Node extSourceNode = extSourcesNodes.item(extSourceSeq);
if (extSourceNode.getNodeType() == Node.ELEMENT_NODE) {
Element extSourceElement = (Element) extSourceNode;
// Get extSource name
String extSourceName = extSourceElement.getElementsByTagName("name").item(0).getChildNodes().item(0).getNodeValue();
if (extSourceName == null) {
throw new InternalErrorException("extSource doesn't have defined name");
}
// Get extSource type
String extSourceType = extSourceElement.getElementsByTagName("type").item(0).getChildNodes().item(0).getNodeValue();
if (extSourceType == null) {
throw new InternalErrorException("extSource " + extSourceName + " doesn't have defined type");
}
// Get all extSource attributes
NodeList attributeNodes = extSourceElement.getElementsByTagName("attribute");
Map<String, String> attributes = new HashMap<>();
for (int attributeSeq = 0; attributeSeq < attributeNodes.getLength(); attributeSeq++) {
Element elem = (Element) attributeNodes.item(attributeSeq);
if (elem.getNodeType() == Node.ELEMENT_NODE) {
String attrName = elem.getAttribute("name");
String attrValue = null;
if (elem.getChildNodes() != null && elem.getChildNodes().item(0) != null) {
attrValue = elem.getChildNodes().item(0).getNodeValue();
}
attributes.put(attrName, attrValue);
}
}
// Check if the extSource
try {
ExtSource extSource;
try {
extSource = this.getExtSourceByName(sess, extSourceName);
extSource.setName(extSourceName);
extSource.setType(extSourceType);
// ExtSource exists, so check values and potentionally update it
self.updateExtSource(sess, extSource, attributes);
} catch (ExtSourceNotExistsException e) {
// ExtSource doesn't exist, so create it
extSource = new ExtSource();
extSource.setName(extSourceName);
extSource.setType(extSourceType);
self.createExtSource(sess, extSource, attributes);
}
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
}
} catch (FileNotFoundException e) {
log.warn("No external source configuration file found.");
} catch (Exception e) {
log.error("Cannot initialize ExtSourceManager.");
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException in project perun by CESNET.
the class MembersManagerEntry method createMember.
@Override
public Member createMember(PerunSession sess, Vo vo, ExtSource extSource, String login, List<Group> groups) throws WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException, VoNotExistsException, ExtSourceNotExistsException, PrivilegeException, GroupNotExistsException {
Utils.checkPerunSession(sess);
getPerunBl().getVosManagerBl().checkVoExists(sess, vo);
getPerunBl().getExtSourcesManagerBl().checkExtSourceExists(sess, extSource);
// if any group is not from the vo, throw an exception
if (groups != null) {
for (Group group : groups) {
perunBl.getGroupsManagerBl().checkGroupExists(sess, group);
if (group.getVoId() != vo.getId())
throw new InternalErrorException("Group " + group + " is not from the vo " + vo + " where user with login " + login + " from ExtSource " + extSource + " should be added.");
}
}
// Authorization
if (!AuthzResolver.authorizedInternal(sess, "createMember_Vo_ExtSource_String_List<Group>_policy", Arrays.asList(vo, extSource))) {
// also group admin of all affected groups is ok
if (groups != null && !groups.isEmpty()) {
for (Group group : groups) {
if (!AuthzResolver.authorizedInternal(sess, "createMember_Vo_ExtSource_String_List<Group>_policy", group)) {
throw new PrivilegeException(sess, "createMember - from login and extSource");
}
}
// ExtSource has to be assigned to at least one of the groups
boolean groupContainsExtSource = groups.stream().map(group -> getPerunBl().getExtSourcesManagerBl().getGroupExtSources(sess, group)).anyMatch(extSources -> extSources.contains(extSource));
if (!groupContainsExtSource) {
throw new PrivilegeException(sess, "createMember - from login and extSource");
}
} else {
throw new PrivilegeException(sess, "createMember - from login and extSource");
}
}
// we run async validation
Member member = getMembersManagerBl().createMember(sess, vo, extSource, login, groups);
getMembersManagerBl().validateMemberAsync(sess, member);
return member;
}
use of cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException in project perun by CESNET.
the class ExtSourceREMS method existsSubjectWithUes.
/**
* Finds out for given ues and login exits user in Perun.
* Format of ues is {extSourceName}|{extSourceClass}|{eppn}|0.
* The eppn is used as a 'login'.
*
* @param ues ues with user login: {extSourceName}|{extSourceClass}|{eppn}|0
* @return true if is found existing ues with given login, false otherwise
* @throws InternalErrorException internalError
*/
private boolean existsSubjectWithUes(String ues) {
String[] extSourceSplit = ues.split("\\|", 4);
if (extSourceSplit.length != 4) {
log.error("Ivalid format of additionalues_1. It should be '{extSourceName}|{extSourceClass}|{eppn}|0'. Actual: {}", ues);
return false;
}
PerunSession sess = getSession();
String extSourceName = extSourceSplit[0];
String eppn = extSourceSplit[2];
try {
// try to find user by additionalues
perunBl.getUsersManagerBl().getUserByExtSourceNameAndExtLogin(sess, extSourceName, eppn);
return true;
} catch (ExtSourceNotExistsException | UserExtSourceNotExistsException e) {
log.error("Failed to get extSource with name '{}'", extSourceName);
} catch (UserNotExistsException e) {
return false;
}
return false;
}
Aggregations