NotificationHelper.java
8.18 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* 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.Credentials
* javax.jcr.PathNotFoundException
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.SimpleCredentials
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.Group
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.workflow.impl.email;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.Credentials;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NotificationHelper {
private static final Logger LOGGER = LoggerFactory.getLogger(NotificationHelper.class);
private static final String WORKFLOW_SERVICE = "workflow";
private NotificationHelper() {
}
public static Authorizable getAuthorizable(UserManager manager, String userId) {
if (null != manager && null != userId) {
if ("system".equals(userId)) {
userId = "admin";
}
try {
return manager.getAuthorizable(userId);
}
catch (RepositoryException e) {
LOGGER.warn("User Manager was unable to find the User: {}.", (Object)userId, (Object)e);
}
} else {
LOGGER.warn("user manager or user id unavailable: {} - {}", (Object)manager, (Object)userId);
}
return null;
}
public static Set<Authorizable> getParticipants(String userGroupId, ResourceResolver resolver) {
UserManager um;
HashSet<Authorizable> auths = new HashSet<Authorizable>();
if (userGroupId != null && userGroupId.length() > 0 && (um = (UserManager)resolver.adaptTo(UserManager.class)) != null) {
Authorizable participant = NotificationHelper.getAuthorizable(um, userGroupId);
auths.addAll(NotificationHelper.getParticipants(participant, resolver));
}
return auths;
}
private static Set<Authorizable> getParticipants(Authorizable userGroup, ResourceResolver resolver) {
HashSet<Authorizable> participants;
block9 : {
participants = new HashSet<Authorizable>();
if (userGroup != null) {
if (!userGroup.isGroup()) {
try {
String userID = userGroup.getID();
String email = NotificationHelper.getEmailAddress(userGroup.getID(), resolver);
if (email != null) {
LOGGER.debug("extracted user {} with email {} from workflow step.", (Object)userID, (Object)email);
participants.add(userGroup);
break block9;
}
LOGGER.debug("extracted user {} which has no email, not sending notification to this user.", (Object)userID);
}
catch (RepositoryException e) {
LOGGER.error("Unable to look up user's ID.", (Throwable)e);
}
} else {
Group group = (Group)userGroup;
Iterator members = null;
try {
members = group.getMembers();
}
catch (RepositoryException e) {
LOGGER.error("Unable to query for members of group.", (Throwable)e);
}
while (members != null && members.hasNext()) {
Authorizable member = (Authorizable)members.next();
participants.addAll(NotificationHelper.getParticipants(member, resolver));
}
}
}
}
return participants;
}
public static String getEmailAddress(String userID, ResourceResolver resolver) throws RepositoryException {
UserPropertiesManager userPropertiesManager = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
UserProperties props = userPropertiesManager.getUserProperties(userID, "profile");
String mail = props.getProperty("email");
if (mail != null && mail.length() == 0) {
mail = null;
}
return mail;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static String getUserLanguage(SlingRepository repository, ResourceResolverFactory resolverFactory, String userId) {
block16 : {
if ("system".equals(userId)) {
return null;
}
Session userSession = null;
ResourceResolver resolver = null;
if (null != repository) {
SimpleCredentials credentials = new SimpleCredentials(userId, new char[0]);
try {
userSession = repository.impersonateFromService("workflow", (Credentials)credentials, null);
resolver = resolverFactory.getResourceResolver(Collections.singletonMap("user.jcr.session", userSession));
UserPropertiesManager userPropertiesManager = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
UserProperties userProperties = userPropertiesManager.getUserProperties(userId, "preferences");
try {
String language;
String string = language = null != userProperties ? userProperties.getProperty("language") : null;
return string;
}
catch (PathNotFoundException e) {
try {
LOGGER.error("Could not get resource bundle for {} path does not exist: ", (Object)userId, (Object)e);
}
catch (RepositoryException e) {
LOGGER.error("Could not get resource bundle for user {}, impersonation failed: ", (Object)userId, (Object)e);
}
catch (LoginException e) {
LOGGER.error("Could not get resource bundle for user {}, impersonation failed: ", (Object)userId, (Object)e);
}
catch (Throwable var10_13) {
throw var10_13;
}
if (userSession != null && userSession.isLive()) {
userSession.logout();
}
if (resolver != null && resolver.isLive()) {
resolver.close();
}
break block16;
}
}
finally {
if (userSession != null && userSession.isLive()) {
userSession.logout();
}
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
}
LOGGER.error("could not get resource bundle for {}, repository unavailable.", (Object)userId);
}
return null;
}
}