Search in sources :

Example 86 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class CallFunction method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
     */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    final Sequence arg0 = getArgument(0).eval(contextSequence, contextItem);
    if (arg0.getCardinality() != Cardinality.EXACTLY_ONE) {
        throw new XPathException(this, "Expected exactly one item for first argument");
    }
    final Item item0 = arg0.itemAt(0);
    if (item0.getType() != Type.FUNCTION_REFERENCE) {
        throw new XPathException(this, "Type error: expected function, got " + Type.getTypeName(item0.getType()));
    }
    try (final FunctionReference ref = (FunctionReference) item0) {
        // pass the remaining parameters to the function call
        final List<Expression> params = new ArrayList<>(getArgumentCount() - 1);
        for (int i = 1; i < getArgumentCount(); i++) {
            params.add(getArgument(i));
        }
        ref.setArguments(params);
        ref.analyze(new AnalyzeContextInfo(this, 0));
        // Evaluate the function
        return ref.eval(contextSequence);
    }
}
Also used : Item(org.exist.xquery.value.Item) XPathException(org.exist.xquery.XPathException) Expression(org.exist.xquery.Expression) ArrayList(java.util.ArrayList) FunctionReference(org.exist.xquery.value.FunctionReference) Sequence(org.exist.xquery.value.Sequence) AnalyzeContextInfo(org.exist.xquery.AnalyzeContextInfo)

Example 87 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class PermissionsFunction method functionModeToOctal.

private Sequence functionModeToOctal(final String modeStr) throws XPathException {
    try {
        final int mode = AbstractUnixStylePermission.simpleSymbolicModeToInt(modeStr);
        final String octal = mode == 0 ? "0" : "0" + Integer.toOctalString(mode);
        return new StringValue(octal);
    } catch (final SyntaxException se) {
        throw new XPathException(se.getMessage(), se);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) SyntaxException(org.exist.util.SyntaxException) StringValue(org.exist.xquery.value.StringValue)

Example 88 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class FindGroupFunction method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final DBBroker broker = getContext().getBroker();
    final Subject currentUser = broker.getCurrentSubject();
    if (!isCalledAs(qnGetUserGroups.getLocalPart()) && currentUser.getName().equals(SecurityManager.GUEST_USER)) {
        throw new XPathException(this, "You must be an authenticated user");
    }
    final SecurityManager securityManager = broker.getBrokerPool().getSecurityManager();
    final Sequence result;
    if (isCalledAs(qnGetUserPrimaryGroup.getLocalPart())) {
        final String username = args[0].getStringValue();
        result = new StringValue(securityManager.getAccount(username).getPrimaryGroup());
    } else if (isCalledAs(qnGroupExists.getLocalPart())) {
        final String groupName = args[0].getStringValue();
        result = BooleanValue.valueOf(securityManager.hasGroup(groupName));
    } else {
        final List<String> groupNames;
        if (isCalledAs(qnListGroups.getLocalPart())) {
            groupNames = securityManager.findAllGroupNames();
        } else if (isCalledAs(qnFindGroupsByGroupname.getLocalPart())) {
            final String startsWith = args[0].getStringValue();
            groupNames = securityManager.findGroupnamesWhereGroupnameStarts(startsWith);
        } else if (isCalledAs(qnFindGroupsWhereGroupnameContains.getLocalPart())) {
            final String fragment = args[0].getStringValue();
            groupNames = securityManager.findGroupnamesWhereGroupnameContains(fragment);
        } else if (isCalledAs(qnGetUserGroups.getLocalPart())) {
            final String username = args[0].getStringValue();
            if (!currentUser.hasDbaRole() && !currentUser.getName().equals(username)) {
                throw new XPathException(this, "You must be a DBA or enquiring about your own user account!");
            }
            final Account user = securityManager.getAccount(username);
            groupNames = Arrays.asList(user.getGroups());
        } else {
            throw new XPathException(this, "Unknown function");
        }
        // order a-z
        Collections.sort(groupNames);
        result = new ValueSequence();
        for (final String groupName : groupNames) {
            result.add(new StringValue(groupName));
        }
    }
    return result;
}
Also used : Account(org.exist.security.Account) DBBroker(org.exist.storage.DBBroker) SecurityManager(org.exist.security.SecurityManager) XPathException(org.exist.xquery.XPathException) List(java.util.List) Subject(org.exist.security.Subject)

Example 89 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class GetPrincipalMetadataFunction method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    Sequence result = Sequence.EMPTY_SEQUENCE;
    final DBBroker broker = getContext().getBroker();
    final Subject currentUser = broker.getCurrentSubject();
    if (args.length == 0) {
        if (isCalledAs(qnGetAccountMetadataKeys.getLocalPart())) {
            result = getAllAccountMetadataKeys();
        } else if (isCalledAs(qnGetGroupMetadataKeys.getLocalPart())) {
            result = getAllGroupMetadataKeys();
        } else {
            throw new XPathException("Unknown function");
        }
    } else {
        final SecurityManager securityManager = broker.getBrokerPool().getSecurityManager();
        final String strPrincipal = args[0].getStringValue();
        final Principal principal;
        if (isCalledAs(qnGetAccountMetadataKeys.getLocalPart()) || isCalledAs(qnGetAccountMetadata.getLocalPart())) {
            if (!currentUser.hasDbaRole() && !currentUser.getUsername().equals(strPrincipal)) {
                throw new XPathException("You must be a DBA to retrieve metadata about other users, otherwise you may only retrieve metadata about yourself.");
            }
            principal = securityManager.getAccount(strPrincipal);
        } else if (isCalledAs(qnGetGroupMetadataKeys.getLocalPart()) || isCalledAs(qnGetGroupMetadata.getLocalPart())) {
            if (!currentUser.hasDbaRole() && !currentUser.hasGroup(strPrincipal)) {
                throw new XPathException("You must be a DBA to retrieve metadata about other groups, otherwise you may only retrieve metadata about groups you are a member of.");
            }
            principal = securityManager.getGroup(strPrincipal);
        } else {
            throw new XPathException("Unknown function");
        }
        if (principal == null) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            if (isCalledAs(qnGetAccountMetadataKeys.getLocalPart()) || isCalledAs(qnGetGroupMetadataKeys.getLocalPart())) {
                result = getPrincipalMetadataKeys(principal);
            } else if (isCalledAs(qnGetAccountMetadata.getLocalPart()) || isCalledAs(qnGetGroupMetadata.getLocalPart())) {
                final String metadataAttributeNamespace = args[1].getStringValue();
                result = getPrincipalMetadata(principal, metadataAttributeNamespace);
            } else {
                throw new XPathException("Unknown function");
            }
        }
    }
    return result;
}
Also used : DBBroker(org.exist.storage.DBBroker) SecurityManager(org.exist.security.SecurityManager) XPathException(org.exist.xquery.XPathException) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) Subject(org.exist.security.Subject) Principal(org.exist.security.Principal)

Example 90 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class UMaskFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final DBBroker broker = getContext().getBroker();
    final Subject currentUser = broker.getCurrentSubject();
    if (currentUser.getName().equals(SecurityManager.GUEST_USER)) {
        throw new XPathException("You must be an authenticated user");
    }
    final String username = args[0].getStringValue();
    if (isCalledAs(qnGetUMask.getLocalPart())) {
        return getUMask(broker, username);
    } else if (isCalledAs(qnSetUMask.getLocalPart())) {
        final int umask = ((IntegerValue) args[1].itemAt(0)).getInt();
        setUMask(broker, currentUser, username, umask);
        return Sequence.EMPTY_SEQUENCE;
    } else {
        throw new XPathException("Unknown function");
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) Subject(org.exist.security.Subject)

Aggregations

XPathException (org.exist.xquery.XPathException)306 Sequence (org.exist.xquery.value.Sequence)86 IOException (java.io.IOException)61 SAXException (org.xml.sax.SAXException)43 StringValue (org.exist.xquery.value.StringValue)40 PermissionDeniedException (org.exist.security.PermissionDeniedException)34 NodeValue (org.exist.xquery.value.NodeValue)34 DBBroker (org.exist.storage.DBBroker)32 IntegerValue (org.exist.xquery.value.IntegerValue)32 ValueSequence (org.exist.xquery.value.ValueSequence)27 Item (org.exist.xquery.value.Item)26 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)24 EXistException (org.exist.EXistException)23 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)22 BrokerPool (org.exist.storage.BrokerPool)21 Txn (org.exist.storage.txn.Txn)21 XQueryContext (org.exist.xquery.XQueryContext)21 Element (org.w3c.dom.Element)21 XQuery (org.exist.xquery.XQuery)20