Search in sources :

Example 1 with StartElement

use of org.exist.util.sax.event.contenthandler.StartElement in project exist by eXist-db.

the class ConfigurationDocumentTrigger method setAccountPrimaryGroupToNoGroup.

private void setAccountPrimaryGroupToNoGroup() throws SAXException {
    final SAXEvent firstEvent = deferred.peek();
    if (!(firstEvent instanceof StartElement)) {
        throw new SAXException("Unbalanced SAX Events");
    }
    StartElement start = ((StartElement) firstEvent);
    if (start.namespaceURI == null || !start.namespaceURI.equals(Configuration.NS) || !start.localName.equals(PrincipalType.ACCOUNT.getElementName())) {
        throw new SAXException("First element does not match ending '" + PrincipalType.ACCOUNT.getElementName() + "' element");
    }
    start = (StartElement) deferred.pop();
    final AttributesImpl attrs = new AttributesImpl();
    attrs.addAttribute("", "name", "name", "CDATA", SecurityManager.UNKNOWN_GROUP);
    final StartElement startPrimaryGroup = new StartElement(start.namespaceURI, PrincipalType.GROUP.getElementName(), PrincipalType.GROUP.getElementName(), attrs);
    final EndElement endPrimaryGroup = new EndElement(startPrimaryGroup.namespaceURI, startPrimaryGroup.localName, startPrimaryGroup.qname);
    deferred.push(endPrimaryGroup);
    deferred.push(startPrimaryGroup);
    deferred.push(start);
}
Also used : StartElement(org.exist.util.sax.event.contenthandler.StartElement) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SAXEvent(org.exist.util.sax.event.SAXEvent) EndElement(org.exist.util.sax.event.contenthandler.EndElement) SAXException(org.xml.sax.SAXException)

Example 2 with StartElement

use of org.exist.util.sax.event.contenthandler.StartElement in project exist by eXist-db.

the class ConfigurationDocumentTrigger method processPrincipal.

/**
 * When configuring a Principal (Account or Group) we need to
 * make sure of two things:
 *
 * 1) If the principal uses an old style id, i.e. before ACL Permissions
 * were introduced then we have to modernise this id
 *
 * 2) If the principal uses a name or id which already exists in
 * the database then we must avoid conflicts
 */
private void processPrincipal(final PrincipalType principalType) throws SAXException {
    final SAXEvent firstEvent = deferred.peek();
    if (!(firstEvent instanceof StartElement)) {
        throw new SAXException("Unbalanced SAX Events");
    }
    final StartElement start = ((StartElement) firstEvent);
    if (start.namespaceURI == null || !start.namespaceURI.equals(Configuration.NS) || !start.localName.equals(principalType.getElementName())) {
        throw new SAXException("First element does not match ending '" + principalType.getElementName() + "' element");
    }
    final SecurityManager sm = broker.getBrokerPool().getSecurityManager();
    // if needed, update old style id to new style id
    final AttributesImpl attrs = new AttributesImpl(migrateIdAttribute(sm, start.attributes, principalType));
    // check if there is a name collision, i.e. another principal with the same name
    final String principalName = findName();
    // first check if the account or group exists before trying to retrieve it
    // otherwise the LDAP realm will create a new user, leading to an endless loop
    final boolean principalExists = principalName != null && principalType.hasPrincipal(sm, principalName);
    Principal existingPrincipleByName = null;
    if (principalExists) {
        existingPrincipleByName = principalType.getPrincipal(sm, principalName);
    }
    final int newId;
    if (existingPrincipleByName != null) {
        // use id of existing principal which has the same name
        newId = existingPrincipleByName.getId();
    } else {
        // check if there is an id collision, i.e. another principal with the same id
        final Integer id = Integer.valueOf(attrs.getValue(ID_ATTR));
        final boolean principalIdExists = principalType.hasPrincipal(sm, id);
        Principal existingPrincipalById = null;
        if (principalIdExists) {
            existingPrincipalById = principalType.getPrincipal(sm, id);
        }
        if (existingPrincipalById != null) {
            // pre-allocate a new id, so as not to collide with the existing principal
            if (isValidating()) {
                try {
                    principalType.preAllocateId(sm, preAllocatedId);
                } catch (final PermissionDeniedException | EXistException e) {
                    throw new SAXException("Unable to pre-allocate principle id for " + principalType.getElementName() + ": " + principalName, e);
                }
            }
            newId = preAllocatedId.getId();
            if (!isValidating()) {
                preAllocatedId.clear();
            }
        } else {
            // use the provided id as it is currently unallocated
            newId = id;
        }
    }
    // update attributes of the principal in deferred
    attrs.setValue(attrs.getIndex(ID_ATTR), String.valueOf(newId));
    final StartElement prevPrincipalStart = (StartElement) deferred.poll();
    deferred.addFirst(new StartElement(prevPrincipalStart.namespaceURI, prevPrincipalStart.localName, prevPrincipalStart.qname, attrs));
}
Also used : SecurityManager(org.exist.security.SecurityManager) EXistException(org.exist.EXistException) SAXException(org.xml.sax.SAXException) StartElement(org.exist.util.sax.event.contenthandler.StartElement) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SAXEvent(org.exist.util.sax.event.SAXEvent)

Aggregations

SAXEvent (org.exist.util.sax.event.SAXEvent)2 StartElement (org.exist.util.sax.event.contenthandler.StartElement)2 SAXException (org.xml.sax.SAXException)2 AttributesImpl (org.xml.sax.helpers.AttributesImpl)2 EXistException (org.exist.EXistException)1 SecurityManager (org.exist.security.SecurityManager)1 EndElement (org.exist.util.sax.event.contenthandler.EndElement)1