AuthorizableUtil.java
5.72 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Value
* org.apache.commons.lang3.StringUtils
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.security.user.util;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AuthorizableUtil {
private static Logger log = LoggerFactory.getLogger(AuthorizableUtil.class);
static final String FAMILY_NAME = "profile/familyName";
static final String GIVEN_NAME = "profile/givenName";
@Deprecated
private static final String PROPERTY_NAME = "rep:fullname";
@Deprecated
private static final String PROPERTY_DESCRIPTION = "rep:description";
@Deprecated
private static final String PROPERTY_FIRST_NAME = "cq:first-name";
@Deprecated
private static final String PROPERTY_LAST_NAME = "cq:last-name";
public static String getName(Authorizable authorizable) throws RepositoryException {
return AuthorizableUtil.getName(authorizable, null);
}
public static String getFormattedName(ResourceResolver resolver, String userId) {
return AuthorizableUtil.getFormattedName(resolver, null, userId, null);
}
public static String getFormattedName(ResourceResolver resolver, Authorizable authorizable) {
return AuthorizableUtil.getFormattedName(resolver, authorizable, null, null);
}
public static String getFormattedName(ResourceResolver resolver, String userId, String nameDisplayOrder) {
return AuthorizableUtil.getFormattedName(resolver, null, userId, nameDisplayOrder);
}
public static String getFormattedName(ResourceResolver resolver, Authorizable authorizable, String nameDisplayOrder) {
return AuthorizableUtil.getFormattedName(resolver, authorizable, null, nameDisplayOrder);
}
private static String getFormattedName(ResourceResolver resolver, Authorizable authorizable, String userId, String nameDisplayOrder) {
if (authorizable != null || !StringUtils.isEmpty((CharSequence)userId)) {
try {
UserPropertiesManager upm = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
if (upm != null) {
String displayName;
UserProperties props;
UserProperties userProperties = props = authorizable != null ? upm.getUserProperties(authorizable, "profile") : upm.getUserProperties(userId, "profile");
if (props != null && StringUtils.isNotEmpty((CharSequence)(displayName = props.getDisplayName(nameDisplayOrder)))) {
return displayName;
}
}
}
catch (RepositoryException e) {
log.warn("Unable to get display name for " + userId, (Throwable)e);
}
}
try {
return authorizable != null ? authorizable.getID() : userId;
}
catch (RepositoryException e) {
log.error("Unable to get authorizable id", (Throwable)e);
return null;
}
}
private static String getName(Authorizable authorizable, UserProperties profile) throws RepositoryException {
String name;
String string = name = profile != null ? profile.getDisplayName() : null;
if (name != null && name.length() > 0) {
return name;
}
name = AuthorizableUtil.getProperty(authorizable, "profile/givenName", "cq:first-name");
if (authorizable.isGroup()) {
if (name != null && name.length() > 0) {
return name;
}
name = AuthorizableUtil.getProperty(authorizable, "rep:fullname");
if (name != null && name.length() > 0) {
return name;
}
name = AuthorizableUtil.getProperty(authorizable, "rep:description");
if (name != null && name.length() > 0) {
return name;
}
} else {
String familyName;
StringBuilder buf = new StringBuilder();
if (name != null) {
buf.append(name);
}
if ((familyName = AuthorizableUtil.getProperty(authorizable, "profile/familyName", "cq:last-name")) != null && familyName.length() > 0) {
buf.append(" ").append(familyName);
}
if (buf.length() > 0) {
return buf.toString();
}
}
return authorizable.getID();
}
private static String getProperty(Authorizable authorizable, String propertyName) {
return AuthorizableUtil.getProperty(authorizable, propertyName, null);
}
private static String getProperty(Authorizable authorizable, String propertyName, String fallbackName) {
try {
Value[] vs;
if (authorizable.hasProperty(propertyName) && (vs = authorizable.getProperty(propertyName)) != null && vs.length > 0) {
return vs[0].getString();
}
if (fallbackName != null && authorizable.hasProperty(fallbackName) && (vs = authorizable.getProperty(fallbackName)) != null && vs.length > 0) {
return vs[0].getString();
}
}
catch (RepositoryException vs) {
// empty catch block
}
return null;
}
}