CloudSettingsUtil.java
6.66 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
/*
* 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;
}
}
}