MacSyncHelperImpl.java 7.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.mac.sync.api.SyncAgent
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.security.AccessControlManager
 *  javax.jcr.security.Privilege
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.mac.sync.helper.impl;

import com.adobe.cq.dam.mac.sync.api.SyncAgent;
import com.adobe.cq.dam.mac.sync.helper.MacSyncHelper;
import com.adobe.cq.dam.mac.sync.helper.impl.MACSyncClient;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfiguration;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfigurationResolver;
import java.util.Collections;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.Privilege;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
public class MacSyncHelperImpl
implements MacSyncHelper {
    private static final Logger log = LoggerFactory.getLogger(MacSyncHelperImpl.class);
    private static final Map<String, Object> REPLICATION_SERVICE_AUTH_INFO = Collections.singletonMap("sling.service.subservice", "dam-replication-helper");
    @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
    private volatile MACSyncClient syncClient = null;
    @Reference
    private ResourceResolverFactory rrf = null;
    @Reference
    private MACTenantConfigurationResolver configResolver = null;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public Map<String, String> getCCUsers(Resource currentResource) {
        ResourceResolver serviceResolver = null;
        ResourceResolver userResolver = currentResource.getResourceResolver();
        try {
            serviceResolver = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
            if (this.canSync(userResolver, currentResource.getPath())) {
                MACTenantConfiguration mtc = this.configResolver.getDefaultConfig(serviceResolver);
                Map<String, String> map = this.syncClient.getCCUsers(mtc);
                return map;
            }
        }
        catch (LoginException e) {
            log.error("Unable to process event: failed to obtain a resource resolver.", (Throwable)e);
        }
        finally {
            if (serviceResolver != null) {
                serviceResolver.close();
            }
        }
        return Collections.EMPTY_MAP;
    }

    @Override
    public boolean canSync(ResourceResolver resourceResolver, String path) {
        try {
            if (this.syncClient == null || path == null) {
                return false;
            }
            Session session = (Session)resourceResolver.adaptTo(Session.class);
            AccessControlManager accessControlManager = session.getAccessControlManager();
            Privilege[] privs = "/content/dam".equals(path) ? new Privilege[]{accessControlManager.privilegeFromName("{http://www.jcp.org/jcr/1.0}addChildNodes"), accessControlManager.privilegeFromName("{http://www.jcp.org/jcr/1.0}modifyProperties")} : new Privilege[]{accessControlManager.privilegeFromName("crx:replicate"), accessControlManager.privilegeFromName("{http://www.jcp.org/jcr/1.0}read"), accessControlManager.privilegeFromName("{http://www.jcp.org/jcr/1.0}write")};
            return accessControlManager.hasPrivileges(path, privs);
        }
        catch (RepositoryException e) {
            log.error("unable to determine if user can apply team.", (Throwable)e);
            new RuntimeException((Throwable)e);
            return false;
        }
    }

    @Override
    public void updateReplicationAgentProviderPid(SyncAgent macSyncAgent, ResourceResolver rr, MACTenantConfiguration macConfiguration, String pid) {
        Resource authorAgentsRoot = rr.getResource("/etc/replication/agents.author");
        String agentResourceName = macSyncAgent.getAgentNameForTenant(macConfiguration.getTenant());
        Resource agentResource = rr.getResource(authorAgentsRoot, agentResourceName);
        Resource agentJcrContentResource = null;
        try {
            Map agentProperties = null;
            if (agentResource != null) {
                agentJcrContentResource = rr.getResource(agentResource, "jcr:content");
                agentProperties = (Map)agentJcrContentResource.adaptTo(ModifiableValueMap.class);
                agentProperties.put("accessTokenProviderPid", pid);
                rr.commit();
                log.info("Updating access token provider pid for {} replication agent." + macSyncAgent.getAgentNameForTenant(macConfiguration.getTenant()));
            }
        }
        catch (PersistenceException e) {
            log.error("Error while updating access token provider pid for {} replication agent." + macSyncAgent.getAgentNameForTenant(macConfiguration.getTenant()), (Throwable)e);
        }
    }

    protected void bindSyncClient(MACSyncClient mACSyncClient) {
        this.syncClient = mACSyncClient;
    }

    protected void unbindSyncClient(MACSyncClient mACSyncClient) {
        if (this.syncClient == mACSyncClient) {
            this.syncClient = null;
        }
    }

    protected void bindRrf(ResourceResolverFactory resourceResolverFactory) {
        this.rrf = resourceResolverFactory;
    }

    protected void unbindRrf(ResourceResolverFactory resourceResolverFactory) {
        if (this.rrf == resourceResolverFactory) {
            this.rrf = null;
        }
    }

    protected void bindConfigResolver(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
        this.configResolver = mACTenantConfigurationResolver;
    }

    protected void unbindConfigResolver(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
        if (this.configResolver == mACTenantConfigurationResolver) {
            this.configResolver = null;
        }
    }
}