CloudSettingsUtil.java 6.66 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserProperties
 *  com.adobe.granite.security.user.UserPropertiesManager
 *  javax.jcr.RepositoryException
 *  javax.mail.internet.AddressException
 *  javax.mail.internet.InternetAddress
 *  org.apache.commons.collections.Predicate
 *  org.apache.commons.collections.iterators.FilterIterator
 *  org.apache.commons.lang3.ArrayUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.SyntheticResource
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.granite.cloudsettings;

import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import java.util.Collections;
import java.util.Iterator;
import javax.jcr.RepositoryException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.SyntheticResource;
import org.apache.sling.api.resource.ValueMap;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class CloudSettingsUtil {
    public static final String MIXIN_CLOUDSETTINGS_CONFIG_TYPE = "granite:CloudsettingsConfigType";
    public static final String PROP_ALLOWED_CHILD_TYPES = "allowedChildTypes";
    public static final String PROP_ALLOWED_PARENT_TYPES = "allowedParentTypes";
    public static final String PROP_UNIQUE_NAME = "uniqueName";

    public static String getPersonDisplayName(ResourceResolver resolver, String userId) {
        String displayName = null;
        if (!StringUtils.isEmpty((CharSequence)userId)) {
            try {
                InternetAddress[] addresses = InternetAddress.parse((String)userId);
                if (addresses.length > 0) {
                    userId = addresses[0].getAddress();
                    displayName = addresses[0].getPersonal();
                }
            }
            catch (AddressException e) {
                // empty catch block
            }
            try {
                String profileDisplayName;
                UserPropertiesManager upm = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
                UserProperties profile = upm.getUserProperties(userId, "profile");
                if (profile != null && !StringUtils.isEmpty((CharSequence)(profileDisplayName = profile.getDisplayName()))) {
                    displayName = profileDisplayName;
                }
            }
            catch (RepositoryException e) {
                // empty catch block
            }
        }
        if (StringUtils.isEmpty((CharSequence)displayName)) {
            displayName = userId;
        }
        return displayName;
    }

    public static Iterator<Resource> getAllCloudSettingsConfigTypes(ResourceResolver resolver) {
        Iterator<Resource> searchIt = resolver.findResources("SELECT * FROM [granite:CloudsettingsConfigType]", "JCR-SQL2");
        return searchIt == null ? Collections.emptyList().iterator() : searchIt;
    }

    public static Iterator<Resource> getCloudSettingsConfigTypes(ResourceResolver resolver, Resource parentResource) {
        FilterIterator resultIt = null;
        CloudSettingsConfigTypeFilter predicate = new CloudSettingsConfigTypeFilter(parentResource);
        Iterator searchIt = resolver.findResources("SELECT * FROM [granite:CloudsettingsConfigType]", "JCR-SQL2");
        if (searchIt != null && searchIt.hasNext()) {
            resultIt = new FilterIterator(searchIt, (Predicate)predicate);
        }
        return resultIt == null ? Collections.emptyList().iterator() : resultIt;
    }

    private static class CloudSettingsConfigTypeFilter
    implements Predicate {
        private Resource parentResource;

        public CloudSettingsConfigTypeFilter(Resource parentResource) {
            this.parentResource = parentResource;
        }

        public boolean evaluate(Object object) {
            if (object instanceof Resource) {
                Object allowedParentType;
                Resource configType = (Resource)object;
                if (configType.getParent() == null) {
                    return false;
                }
                if (!configType.getPath().startsWith("/apps") && !configType.getPath().startsWith("/libs")) {
                    return false;
                }
                ValueMap configTypeVm = ResourceUtil.getValueMap((Resource)configType);
                String uniqueName = (String)configTypeVm.get("uniqueName", String.class);
                if (StringUtils.isNotEmpty((CharSequence)uniqueName) && this.parentResource.getChild(uniqueName) != null) {
                    return false;
                }
                ResourceResolver resourceResolver = this.parentResource.getResourceResolver();
                Object[] allowedParentTypes = (String[])configTypeVm.get("allowedParentTypes", (Object)new String[0]);
                boolean isParentAllowed = ArrayUtils.isEmpty((Object[])allowedParentTypes);
                Object[] arr$ = allowedParentTypes;
                int len$ = arr$.length;
                for (int i$ = 0; i$ < len$ && !(isParentAllowed = resourceResolver.isResourceType(this.parentResource, (String)(allowedParentType = arr$[i$]))); ++i$) {
                }
                String parentResType = this.parentResource.getResourceType();
                Resource parentResTypeResource = resourceResolver.getResource(parentResType);
                ValueMap parentTypeVm = ResourceUtil.getValueMap((Resource)parentResTypeResource);
                Object[] allowedChildTypes = (String[])parentTypeVm.get("allowedChildTypes", (Object)new String[0]);
                boolean isChildAllowed = ArrayUtils.isEmpty((Object[])allowedChildTypes);
                for (Object allowedChildType : allowedChildTypes) {
                    SyntheticResource configTypeRes = new SyntheticResource(resourceResolver, "testRes", configType.getPath());
                    isChildAllowed = resourceResolver.isResourceType((Resource)configTypeRes, (String)allowedChildType);
                    if (isChildAllowed) break;
                }
                return isParentAllowed && isChildAllowed;
            }
            return false;
        }
    }

}