Access defaultAccess = getAssumedAccessType(method);
// Need to check after examining all parameters to see if we found any:
boolean foundVerifiedParameters = false;
Owner owner = null;
Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < annotations.length; i++) {
for (Annotation a : annotations[i]) {
if (a instanceof Verify) {
foundVerifiedParameters = true;
Access requiredAccess = defaultAccess;
@SuppressWarnings("rawtypes")
Class verifyType = ((Verify) a).value();
if (((Verify) a).require() != Access.NONE) {
requiredAccess = ((Verify) a).require();
}
SubResource subResource = ((Verify) a).subResource();
// Use the correct curator (in storeMap) to look up the actual
// entity with the annotated argument
if (!storeMap.containsKey(verifyType)) {
log.error("No store configured to verify: " + verifyType);
throw new IseException(i18n.tr("Unable to verify request."));
}
List entities = new ArrayList();
Object argument = parameters[i];
// if the argument is null, we don't have to check anything
if (argument == null && ((Verify) a).nullable()) {
continue;
}
else if (argument == null) {
log.info("null argument is not allowed");
throw new NotFoundException(i18n.tr(
"{0} with id {1} could not be found.",
Util.getClassName(verifyType), null));
}
if (argument instanceof String) {
String verifyParam = (String) argument;
log.debug("Verifying " + requiredAccess +
" access to " + verifyType + ": " + verifyParam);
Object entity = storeMap.get(verifyType).lookup(verifyParam);
// If the request is just for a single item, throw an exception
// if it is not found.
if (entity == null) {
// This is bad, we're verifying a parameter with an ID which
// doesn't seem to exist in the DB. Error will be thrown in
// invoke though.
String typeName = Util.getClassName(verifyType);
if (typeName.equals("Owner")) {
typeName = i18n.tr("Organization");
}
log.info("No such entity: " + typeName + " id: " +
verifyParam);
throw new NotFoundException(i18n.tr(
"{0} with id {1} could not be found.",
typeName, verifyParam));
}
entities.add(entity);
}
else {
Collection<String> verifyParams = (Collection<String>) argument;
log.debug("Verifying " + requiredAccess +
" access to collection of {}: {}", verifyType, verifyParams);
// If the request is for a list of items, we'll leave it
// up to the requester to determine if something is missing or not.
if (verifyParams != null && !verifyParams.isEmpty()) {
entities = storeMap.get(verifyType).lookup(verifyParams);
}
}
for (Object entity : entities) {
if (!principal.canAccess(entity, subResource, requiredAccess)) {
denyAccess(principal, method);
}
else {
// Access granted, grab the org key for logging purposes:
Owner o = storeMap.get(verifyType).getOwner((Persisted) entity);
if (o != null) {
if (owner != null && !o.equals(owner)) {
log.warn("Found entities from multiple orgs in " +
"one request.");
}
owner = o;
}