|
21 | 21 | */
|
22 | 22 | package org.exist.xquery.functions.system;
|
23 | 23 |
|
| 24 | +import com.evolvedbinary.j8fu.function.SupplierE; |
24 | 25 | import org.apache.logging.log4j.LogManager;
|
25 | 26 | import org.apache.logging.log4j.Logger;
|
26 | 27 | import org.exist.security.AuthenticationException;
|
27 | 28 | import org.exist.security.SecurityManager;
|
28 | 29 | import org.exist.security.Subject;
|
29 | 30 | import org.exist.storage.DBBroker;
|
30 | 31 | import org.exist.xquery.*;
|
| 32 | +import org.exist.xquery.value.FunctionReference; |
31 | 33 | import org.exist.xquery.value.Item;
|
32 | 34 | import org.exist.xquery.value.Sequence;
|
33 | 35 | import org.exist.xquery.value.Type;
|
@@ -56,6 +58,21 @@ public class AsUser extends Function {
|
56 | 58 | optManyParam("code-block", Type.ITEM, "The code block to run as the identified user")
|
57 | 59 | );
|
58 | 60 |
|
| 61 | + private static String FS_FUNCTION_AS_USER_NAME = "function-as-user"; |
| 62 | + public final static FunctionSignature FS_FUNCTION_AS_USER = functionSignature( |
| 63 | + FS_FUNCTION_AS_USER_NAME, |
| 64 | + "A pseudo-function to execute a function as a different " + |
| 65 | + "user. The first argument is the name of the user, the second is the " + |
| 66 | + "password. If the user can be authenticated, the function will execute the " + |
| 67 | + "function given in the third argument with the permissions of that user and" + |
| 68 | + "returns the result of the execution. Before the function completes, it switches " + |
| 69 | + "the current user back to the old user.", |
| 70 | + returnsOptMany(Type.ITEM, "the results of the code block executed"), |
| 71 | + param("username", Type.STRING, "The username of the user to run the code against"), |
| 72 | + optParam("password", Type.STRING, "The password of the user to run the code against"), |
| 73 | + param("function", Type.FUNCTION_REFERENCE, "The zero arity function to run as the identified user") |
| 74 | + ); |
| 75 | + |
59 | 76 | public AsUser(final XQueryContext context, final FunctionSignature signature) {
|
60 | 77 | super(context, signature);
|
61 | 78 | }
|
@@ -86,12 +103,27 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem) thr
|
86 | 103 | throw exception;
|
87 | 104 | }
|
88 | 105 |
|
| 106 | + final SupplierE<Sequence, XPathException> function; |
| 107 | + if (isCalledAs(FS_AS_USER_NAME)) { |
| 108 | + final Expression codeBlock = getArgument(2); |
| 109 | + function = () -> codeBlock.eval(contextSequence, contextItem); |
| 110 | + } else if (isCalledAs(FS_FUNCTION_AS_USER_NAME)) { |
| 111 | + final FunctionReference functionArg = (FunctionReference) getArgument(2).eval(contextSequence, contextItem).itemAt(0); |
| 112 | + final int functionArgArity = functionArg.getSignature().getArgumentCount(); |
| 113 | + if (functionArgArity != 0) { |
| 114 | + throw new XPathException(this, "$function argument must be a zero arity function, but found a function with arity: " + functionArgArity); |
| 115 | + } |
| 116 | + function = () -> functionArg.evalFunction(null, null, null); |
| 117 | + } else { |
| 118 | + throw new XPathException(this, "Unknown function: " + getSignature().getName()); |
| 119 | + } |
| 120 | + |
89 | 121 | if (logger.isTraceEnabled()) {
|
90 | 122 | logger.trace("Setting the effective user to: [{}]", username);
|
91 | 123 | }
|
92 | 124 | try {
|
93 | 125 | broker.pushSubject(user);
|
94 |
| - return getArgument(2).eval(contextSequence, contextItem); |
| 126 | + return function.get(); |
95 | 127 | } finally {
|
96 | 128 | broker.popSubject();
|
97 | 129 | if (logger.isTraceEnabled()) {
|
|
0 commit comments