public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final DBBroker broker = getContext().getBroker();
final Subject currentUser = broker.getSubject();
final SecurityManager securityManager = broker.getBrokerPool().getSecurityManager();
final Sequence result;
if(isCalledAs(qnListUsers.getLocalName())) {
result = new ValueSequence();
if(currentUser.getName().equals(SecurityManager.GUEST_USER)) {
result.add(new StringValue(SecurityManager.GUEST_USER));
} else {
addUserNamesToSequence(securityManager.findAllUserNames(), result);
}
} else {
if(currentUser.getName().equals(SecurityManager.GUEST_USER)) {
throw new XPathException("You must be an authenticated user");
}
if(isCalledAs(qnUserExists.getLocalName())) {
final String username = args[0].getStringValue();
result = BooleanValue.valueOf(securityManager.hasAccount(username));
} else {
result = new ValueSequence();
final String startsWith = args[0].getStringValue();
final List<String> usernames;
if(isCalledAs(qnFindUsersByUsername.getLocalName())) {
usernames = securityManager.findUsernamesWhereUsernameStarts(startsWith);
} else if(isCalledAs(qnFindUsersByName.getLocalName())) {
usernames = securityManager.findUsernamesWhereNameStarts(startsWith);
} else if(isCalledAs(qnFindUsersByNamePart.getLocalName())) {
usernames = securityManager.findUsernamesWhereNamePartStarts(startsWith);
} else {
throw new XPathException("Unknown function");
}
addUserNamesToSequence(usernames, result);