MacSyncHelperImpl.java
7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* 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;
}
}
}