Search in sources :

Example 6 with ExtSourceNotExistsException

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);
    }
}
Also used : UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)

Example 7 with ExtSourceNotExistsException

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);
}
Also used : ParserException(cz.metacentrum.perun.core.api.exceptions.ParserException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)

Example 8 with ExtSourceNotExistsException

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);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) DataAccessException(org.springframework.dao.DataAccessException) ExtSourceNotAssignedException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotAssignedException) ExtSourceAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.ExtSourceAlreadyAssignedException) SQLException(java.sql.SQLException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) FileNotFoundException(java.io.FileNotFoundException) ExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException) ExtSourceAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.ExtSourceAlreadyRemovedException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ExtSource(cz.metacentrum.perun.core.api.ExtSource) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)

Example 9 with ExtSourceNotExistsException

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;
}
Also used : InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Arrays(java.util.Arrays) Validation(cz.metacentrum.perun.core.api.Validation) Candidate(cz.metacentrum.perun.core.api.Candidate) AuthzResolver(cz.metacentrum.perun.core.api.AuthzResolver) Vo(cz.metacentrum.perun.core.api.Vo) Date(java.util.Date) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) LoggerFactory(org.slf4j.LoggerFactory) SpecificUserType(cz.metacentrum.perun.core.api.SpecificUserType) MembersManager(cz.metacentrum.perun.core.api.MembersManager) ExtSource(cz.metacentrum.perun.core.api.ExtSource) MembersManagerBl(cz.metacentrum.perun.core.bl.MembersManagerBl) AlreadySponsoredMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException) LoginNotExistsException(cz.metacentrum.perun.core.api.exceptions.LoginNotExistsException) NamespaceRulesNotExistsException(cz.metacentrum.perun.core.api.exceptions.NamespaceRulesNotExistsException) Map(java.util.Map) Role(cz.metacentrum.perun.core.api.Role) MemberNotValidYetException(cz.metacentrum.perun.core.api.exceptions.MemberNotValidYetException) Attribute(cz.metacentrum.perun.core.api.Attribute) MembersPageQuery(cz.metacentrum.perun.core.api.MembersPageQuery) MemberAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.MemberAlreadyRemovedException) MemberWithSponsors(cz.metacentrum.perun.core.api.MemberWithSponsors) PasswordCreationFailedException(cz.metacentrum.perun.core.api.exceptions.PasswordCreationFailedException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Collectors(java.util.stream.Collectors) User(cz.metacentrum.perun.core.api.User) Sponsor(cz.metacentrum.perun.core.api.Sponsor) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) List(java.util.List) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) LocalDate(java.time.LocalDate) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) RichMember(cz.metacentrum.perun.core.api.RichMember) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) PasswordStrengthException(cz.metacentrum.perun.core.api.exceptions.PasswordStrengthException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) SponsoredUserData(cz.metacentrum.perun.core.api.SponsoredUserData) Resource(cz.metacentrum.perun.core.api.Resource) PerunSession(cz.metacentrum.perun.core.api.PerunSession) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) NamespaceRules(cz.metacentrum.perun.core.api.NamespaceRules) Paginated(cz.metacentrum.perun.core.api.Paginated) Group(cz.metacentrum.perun.core.api.Group) ArrayList(java.util.ArrayList) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) MemberNotSuspendedException(cz.metacentrum.perun.core.api.exceptions.MemberNotSuspendedException) SponsorshipDoesNotExistException(cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException) PasswordResetMailNotExistsException(cz.metacentrum.perun.core.api.exceptions.PasswordResetMailNotExistsException) Member(cz.metacentrum.perun.core.api.Member) Logger(org.slf4j.Logger) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) Iterator(java.util.Iterator) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) ResourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceNotExistsException) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) Utils(cz.metacentrum.perun.core.impl.Utils) InvalidLoginException(cz.metacentrum.perun.core.api.exceptions.InvalidLoginException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) InvalidSponsoredUserDataException(cz.metacentrum.perun.core.api.exceptions.InvalidSponsoredUserDataException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) Collections(java.util.Collections) Status(cz.metacentrum.perun.core.api.Status) Group(cz.metacentrum.perun.core.api.Group) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member)

Example 10 with ExtSourceNotExistsException

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;
}
Also used : PerunSession(cz.metacentrum.perun.core.api.PerunSession) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)

Aggregations

ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)30 ExtSource (cz.metacentrum.perun.core.api.ExtSource)27 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)24 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)23 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)21 ArrayList (java.util.ArrayList)12 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)11 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)10 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)10 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)10 Attribute (cz.metacentrum.perun.core.api.Attribute)9 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)9 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)9 UserExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)8 Candidate (cz.metacentrum.perun.core.api.Candidate)5 CandidateNotExistsException (cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)5 ExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException)5 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)5 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)5 RichUserExtSource (cz.metacentrum.perun.core.api.RichUserExtSource)4