|
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 |
| -import org.exist.dom.QName; |
27 | 27 | import org.exist.security.AuthenticationException;
|
28 | 28 | import org.exist.security.SecurityManager;
|
29 | 29 | import org.exist.security.Subject;
|
30 | 30 | import org.exist.storage.DBBroker;
|
31 | 31 | import org.exist.xquery.*;
|
32 |
| -import org.exist.xquery.value.FunctionParameterSequenceType; |
| 32 | +import org.exist.xquery.value.FunctionReference; |
33 | 33 | import org.exist.xquery.value.Item;
|
34 | 34 | import org.exist.xquery.value.Sequence;
|
35 |
| -import org.exist.xquery.value.SequenceType; |
36 | 35 | import org.exist.xquery.value.Type;
|
37 | 36 |
|
| 37 | +import static org.exist.xquery.FunctionDSL.*; |
| 38 | +import static org.exist.xquery.functions.system.SystemModule.functionSignature; |
| 39 | + |
38 | 40 | /**
|
39 | 41 | */
|
40 | 42 | public class AsUser extends Function {
|
41 | 43 |
|
42 | 44 | private final static Logger logger = LogManager.getLogger(AsUser.class);
|
43 | 45 |
|
44 |
| - public final static FunctionSignature signature = new FunctionSignature( |
45 |
| - new QName("as-user", SystemModule.NAMESPACE_URI, SystemModule.PREFIX), |
46 |
| - "A pseudo-function to execute a limited block of code as a different " + |
47 |
| - "user. The first argument is the name of the user, the second is the " + |
48 |
| - "password. If the user can be authenticated, the function will execute the " + |
49 |
| - "code block given in the third argument with the permissions of that user and" + |
50 |
| - "returns the result of the execution. Before the function completes, it switches " + |
51 |
| - "the current user back to the old user.", |
52 |
| - new SequenceType[] { |
53 |
| - new FunctionParameterSequenceType("username", Type.STRING, Cardinality.EXACTLY_ONE, "The username of the user to run the code against"), |
54 |
| - new FunctionParameterSequenceType("password", Type.STRING, Cardinality.ZERO_OR_ONE, "The password of the user to run the code against"), |
55 |
| - new FunctionParameterSequenceType("code-block", Type.ITEM, Cardinality.ZERO_OR_MORE, "The code block to run as the identified user") |
56 |
| - }, |
57 |
| - new FunctionParameterSequenceType("result", Type.ITEM, Cardinality.ZERO_OR_MORE, "the results of the code block executed") |
| 46 | + private static String FS_AS_USER_NAME = "as-user"; |
| 47 | + public final static FunctionSignature FS_AS_USER = functionSignature( |
| 48 | + FS_AS_USER_NAME, |
| 49 | + "A pseudo-function to execute a limited block of code as a different " + |
| 50 | + "user. The first argument is the name of the user, the second is the " + |
| 51 | + "password. If the user can be authenticated, the function will execute the " + |
| 52 | + "code block given in the third argument with the permissions of that user and" + |
| 53 | + "returns the result of the execution. Before the function completes, it switches " + |
| 54 | + "the current user back to the old user.", |
| 55 | + returnsOptMany(Type.ITEM, "the results of the code block executed"), |
| 56 | + param("username", Type.STRING, "The username of the user to run the code against"), |
| 57 | + optParam("password", Type.STRING, "The password of the user to run the code against"), |
| 58 | + optManyParam("code-block", Type.ITEM, "The code block to run as the identified user") |
| 59 | + ); |
| 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") |
58 | 74 | );
|
59 | 75 |
|
60 |
| - public AsUser(final XQueryContext context) { |
| 76 | + public AsUser(final XQueryContext context, final FunctionSignature signature) { |
61 | 77 | super(context, signature);
|
62 | 78 | }
|
63 | 79 |
|
@@ -87,27 +103,40 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem) thr
|
87 | 103 | throw exception;
|
88 | 104 | }
|
89 | 105 |
|
90 |
| - logger.info("Setting the effective user to: [{}]", username); |
| 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 | + |
| 121 | + if (logger.isTraceEnabled()) { |
| 122 | + logger.trace("Setting the effective user to: [{}]", username); |
| 123 | + } |
91 | 124 | try {
|
92 | 125 | broker.pushSubject(user);
|
93 |
| - return getArgument(2).eval(contextSequence, contextItem); |
| 126 | + return function.get(); |
94 | 127 | } finally {
|
95 | 128 | broker.popSubject();
|
96 |
| - logger.info("Returned the effective user to: [{}]", broker.getCurrentSubject()); |
| 129 | + if (logger.isTraceEnabled()) { |
| 130 | + logger.trace("Returned the effective user to: [{}]", broker.getCurrentSubject()); |
| 131 | + } |
97 | 132 | }
|
98 | 133 | }
|
99 | 134 |
|
100 |
| - /* (non-Javadoc) |
101 |
| - * @see org.exist.xquery.AbstractExpression#getDependencies() |
102 |
| - */ |
103 | 135 | @Override
|
104 | 136 | public int getDependencies() {
|
105 | 137 | return getArgument(2).getDependencies();
|
106 | 138 | }
|
107 | 139 |
|
108 |
| - /* (non-Javadoc) |
109 |
| - * @see org.exist.xquery.PathExpr#returnsType() |
110 |
| - */ |
111 | 140 | @Override
|
112 | 141 | public int returnsType() {
|
113 | 142 | return getArgument(2).returnsType();
|
|
0 commit comments