Search in sources :

Example 1 with Persisted

use of org.candlepin.model.Persisted in project candlepin by candlepin.

the class VerifyAuthorizationFilter method getAccessedEntities.

@SuppressWarnings("unchecked")
protected List<Persisted> getAccessedEntities(Verify verify, Object requestValue) {
    // Nothing to access!
    if (verify.nullable() && null == requestValue) {
        return Collections.emptyList();
    }
    List<Persisted> entities = new ArrayList<>();
    Class<? extends Persisted> verifyType = verify.value();
    if (requestValue instanceof String) {
        String verifyParam = (String) requestValue;
        Persisted entity = null;
        entity = storeFactory.getFor(verifyType).lookup(verifyParam);
        // 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 = i18nProvider.get().tr("Organization");
            }
            String msg = i18nProvider.get().tr("{0} with id {1} could not be found.", typeName, verifyParam);
            log.info("No such entity: {}, id: {}", typeName, verifyParam);
            throw new NotFoundException(msg);
        }
        entities.add(entity);
    } else if (requestValue instanceof Collection) {
        Collection<String> verifyParams = (Collection<String>) requestValue;
        // up to the requester to determine if something is missing or not.
        if (verifyParams != null && !verifyParams.isEmpty()) {
            entities.addAll(storeFactory.getFor(verifyType).lookup(verifyParams));
        }
    }
    return entities;
}
Also used : ArrayList(java.util.ArrayList) Persisted(org.candlepin.model.Persisted) NotFoundException(org.candlepin.common.exceptions.NotFoundException) Collection(java.util.Collection)

Example 2 with Persisted

use of org.candlepin.model.Persisted in project candlepin by candlepin.

the class VerifyAuthorizationFilter method hasAccess.

protected boolean hasAccess(Map<Verify, Object> argMap, Principal principal, Access defaultAccess) {
    boolean hasAccess = false;
    Owner owner = null;
    for (Map.Entry<Verify, Object> entry : argMap.entrySet()) {
        List<Persisted> accessedObjects = new ArrayList<>();
        Object obj = entry.getValue();
        Verify verify = entry.getKey();
        Class<? extends Persisted> verifyType = verify.value();
        accessedObjects.addAll(getAccessedEntities(verify, obj));
        Access requiredAccess = defaultAccess;
        if (verify.require() != Access.NONE) {
            requiredAccess = verify.require();
        }
        log.debug("Verifying {} access to {}: {}", requiredAccess, verifyType, obj);
        SubResource subResource = verify.subResource();
        for (Persisted entity : accessedObjects) {
            if (!principal.canAccess(entity, subResource, requiredAccess)) {
                hasAccess = false;
                break;
            }
            hasAccess = true;
            Owner entityOwner = ((EntityStore) storeFactory.getFor(verifyType)).getOwner(entity);
            if (entityOwner != null) {
                if (owner != null && !owner.equals(entityOwner)) {
                    log.error("Found entities from multiple orgs in a single request");
                    throw new IseException("Found entities from multiple orgs in a single request");
                }
                owner = entityOwner;
            }
        }
        // Stop all further checking with any authorization failure
        if (!hasAccess) {
            break;
        }
    }
    if (hasAccess && owner != null) {
        MDC.put("org", owner.getKey());
        if (owner.getLogLevel() != null) {
            MDC.put("orgLogLevel", owner.getLogLevel());
        }
    }
    return hasAccess;
}
Also used : SubResource(org.candlepin.auth.SubResource) Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) Access(org.candlepin.auth.Access) Persisted(org.candlepin.model.Persisted) IseException(org.candlepin.common.exceptions.IseException) Verify(org.candlepin.auth.Verify) LinkedHashMap(java.util.LinkedHashMap) ResourceLocatorMap(org.candlepin.resteasy.ResourceLocatorMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)2 Persisted (org.candlepin.model.Persisted)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Access (org.candlepin.auth.Access)1 SubResource (org.candlepin.auth.SubResource)1 Verify (org.candlepin.auth.Verify)1 IseException (org.candlepin.common.exceptions.IseException)1 NotFoundException (org.candlepin.common.exceptions.NotFoundException)1 Owner (org.candlepin.model.Owner)1 ResourceLocatorMap (org.candlepin.resteasy.ResourceLocatorMap)1