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);
}
}
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);
}
}
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;
}
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;
}
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");
}
}
Aggregations