FormsConditionRestrictionProvider.java 5.29 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.oak.api.PropertyState
 *  org.apache.jackrabbit.oak.api.Tree
 *  org.apache.jackrabbit.oak.api.Type
 *  org.apache.jackrabbit.oak.spi.security.authorization.restriction.AbstractRestrictionProvider
 *  org.apache.jackrabbit.oak.spi.security.authorization.restriction.Restriction
 *  org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition
 *  org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinitionImpl
 *  org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionPattern
 *  org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionProvider
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.aemfd.formsfoundation.oak.restrictions.impl;

import com.adobe.aemfd.formsfoundation.oak.restrictions.impl.FormsConditionRestrictionPattern;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.AbstractRestrictionProvider;
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.Restriction;
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition;
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinitionImpl;
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionPattern;
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionProvider;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;

@Component
@Service(value={RestrictionProvider.class})
public class FormsConditionRestrictionProvider
extends AbstractRestrictionProvider {
    private static final String RESTRICTION_NAME = "fd:condition";
    private static final String[] DEFAULT_ROOTS = new String[]{"/content/dam/formsanddocuments"};
    @Property(value={"/content/dam/formsanddocuments"}, label="Valid Root Paths", description="Paths for which restriction is valid", cardinality=Integer.MAX_VALUE)
    private static final String PROPERTY_ROOTS = "form.condition.restriction.roots";
    private static String[] rootPaths = DEFAULT_ROOTS;
    private static final Map<String, RestrictionDefinition> supportedRestrictions = FormsConditionRestrictionProvider.supportedRestrictions();

    public FormsConditionRestrictionProvider() {
        super(supportedRestrictions);
    }

    @Activate
    public void activate(ComponentContext ctx) {
        Dictionary props = ctx.getProperties();
        rootPaths = PropertiesUtil.toStringArray(props.get("form.condition.restriction.roots"), (String[])DEFAULT_ROOTS);
    }

    private boolean isSupported(String oakPath) {
        if (oakPath == null) {
            return false;
        }
        for (String rootPath : rootPaths) {
            if (rootPath == null || rootPath.trim().length() <= 0 || !oakPath.startsWith(rootPath + "/") && !oakPath.equals(rootPath)) continue;
            return true;
        }
        return false;
    }

    public boolean isUnsupportedPath(String oakPath) {
        return !this.isSupported(oakPath);
    }

    private static Map<String, RestrictionDefinition> supportedRestrictions() {
        RestrictionDefinitionImpl restrictionDefinition = new RestrictionDefinitionImpl("fd:condition", Type.STRINGS, false);
        HashMap<String, RestrictionDefinitionImpl> supportedRestriction = new HashMap<String, RestrictionDefinitionImpl>();
        supportedRestriction.put(restrictionDefinition.getName(), restrictionDefinition);
        return Collections.unmodifiableMap(supportedRestriction);
    }

    public RestrictionPattern getPattern(String oakPath, Tree tree) {
        PropertyState values;
        if (oakPath != null && (values = tree.getProperty("fd:condition")) != null) {
            return new FormsConditionRestrictionPattern((Iterable)values.getValue(Type.STRINGS), oakPath);
        }
        return RestrictionPattern.EMPTY;
    }

    public RestrictionPattern getPattern(String oakPath, Set<Restriction> restrictions) {
        if (oakPath != null && restrictions != null && !restrictions.isEmpty()) {
            for (Restriction restriction : restrictions) {
                Iterable values;
                String name = restriction.getDefinition().getName();
                if (!"fd:condition".equals(name) || (values = (Iterable)restriction.getProperty().getValue(Type.STRINGS)) == null) continue;
                return new FormsConditionRestrictionPattern((Iterable)restriction.getProperty().getValue(Type.STRINGS), oakPath);
            }
        }
        return RestrictionPattern.EMPTY;
    }
}