-
-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathLoggerEngineDataSelector.cls
More file actions
253 lines (223 loc) · 9.46 KB
/
LoggerEngineDataSelector.cls
File metadata and controls
253 lines (223 loc) · 9.46 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//------------------------------------------------------------------------------------------------//
// This file is part of the Nebula Logger project, released under the MIT License. //
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. //
//------------------------------------------------------------------------------------------------//
/**
* @group Logger Engine
* @description Selector class used for all queries that are specific to the logger engine layer
*/
@SuppressWarnings('PMD.ApexCRUDViolation, PMD.ExcessivePublicCount')
public without sharing virtual class LoggerEngineDataSelector {
@TestVisible
private static final Boolean IS_EXPERIENCE_CLOUD_ENABLED = System.Type.forName('Schema.Network') != null;
@TestVisible
private static List<LoggerSObjectProxy.AuthSession> mockAuthSessionProxies;
@TestVisible
private static List<LoggerSObjectProxy.Network> mockNetworkProxies;
private static LoggerEngineDataSelector instance = new LoggerEngineDataSelector();
@SuppressWarnings('PMD.EmptyStatementBlock')
@TestVisible
private LoggerEngineDataSelector() {
}
/**
* @description The instance `LoggerEngineDataSelector` used for any querying specific to the logger engine layer
* @return The singleton instance of `LoggerEngineDataSelector`
*/
public static LoggerEngineDataSelector getInstance() {
return instance;
}
/**
* @description Returns a `Map<Id, LoggerSObjectProxy.AuthSession>` for the specified user IDs & their matching active sessions,
* or `null` if there is not a current session
* @param userIds userIds description
* @return The instance of `Map<Id, LoggerSObjectProxy.AuthSession>` containing any matching `Schema.AuthSession` records
*/
public virtual Map<Id, LoggerSObjectProxy.AuthSession> getAuthSessionProxies(List<Id> userIds) {
Map<Id, LoggerSObjectProxy.AuthSession> userIdToAuthSessionProxy = new Map<Id, LoggerSObjectProxy.AuthSession>();
if (LoggerParameter.QUERY_AUTH_SESSION_DATA == false) {
return userIdToAuthSessionProxy;
}
List<SObject> authSessionRecords = [
SELECT
Id,
IsAssociatedWithJwtAccessToken,
LoginType,
LoginHistoryId,
LoginHistory.Application,
LoginHistory.Browser,
LoginHistory.Platform,
LoginHistory.UserId,
LogoutUrl,
ParentId,
SessionSecurityLevel,
SessionType,
SourceIp,
UsersId
FROM AuthSession
WHERE UsersId IN :userIds
AND (
IsCurrent = TRUE
OR IsAssociatedWithJwtAccessToken = TRUE
)
ORDER BY ParentId NULLS LAST, IsCurrent DESC
];
List<LoggerSObjectProxy.AuthSession> authSessionProxies = (List<LoggerSObjectProxy.AuthSession>) System.JSON.deserialize(
System.JSON.serialize(authSessionRecords),
List<LoggerSObjectProxy.AuthSession>.class
);
if (mockAuthSessionProxies != null) {
authSessionProxies = mockAuthSessionProxies;
}
for (LoggerSObjectProxy.AuthSession authSessionProxy : authSessionProxies) {
if (!userIdToAuthSessionProxy.containsKey(authSessionProxy.UsersId)) {
userIdToAuthSessionProxy.put(authSessionProxy.UsersId, authSessionProxy);
}
}
return userIdToAuthSessionProxy;
}
/**
* @description Returns a cached copy of `LoggerSObjectProxy.AuthSession` for the current user's current session,
* or `null` if there is not a current session
* @return The cached `LoggerSObjectProxy.AuthSession` record
*/
public virtual LoggerSObjectProxy.AuthSession getCachedAuthSessionProxy() {
Id userId = System.UserInfo.getUserId();
String cacheKey = 'AuthSession' + userId;
if (LoggerCache.getSessionCache().contains(cacheKey)) {
return (LoggerSObjectProxy.AuthSession) LoggerCache.getSessionCache().get(cacheKey);
}
LoggerSObjectProxy.AuthSession authSessionProxy = getAuthSessionProxies(new List<Id>{ userId }).get(userId);
LoggerCache.getSessionCache().put(cacheKey, authSessionProxy);
return authSessionProxy;
}
/**
* @description Returns a cached copy of the current user's `Schema.Network` site, or `null` if the current user is not associated
* with a `Schema.Network` site
* @param networkId The record ID of the `Schema.Network` to query
* @return The cached `Schema.Network` record
*/
public virtual LoggerSObjectProxy.Network getCachedNetworkProxy(Id networkId) {
if (networkId == null) {
return null;
}
String cacheKey = 'Network' + networkId;
if (LoggerCache.getOrganizationCache().contains(cacheKey)) {
return (LoggerSObjectProxy.Network) LoggerCache.getOrganizationCache().get(cacheKey);
}
LoggerSObjectProxy.Network networkProxy = getNetworkProxies(new List<Id>{ networkId }).get(networkId);
LoggerCache.getOrganizationCache().put(cacheKey, networkProxy);
return networkProxy;
}
/**
* @description Returns a cached copy of the `Schema.Organization` record in the org, including some fields that cannot be accessed via `UserInfo`
* @return The cached `Schema.Organization` record
*/
public virtual Schema.Organization getCachedOrganization() {
String cacheKey = 'Organization';
if (LoggerCache.getOrganizationCache().contains(cacheKey)) {
return (Schema.Organization) LoggerCache.getOrganizationCache().get(cacheKey);
}
Schema.Organization organization;
if (LoggerParameter.QUERY_ORGANIZATION_DATA) {
organization = [
SELECT
Id,
CreatedById,
CreatedBy.Name,
CreatedBy.Username,
CreatedDate,
InstanceName,
IsSandbox,
Name,
NamespacePrefix,
OrganizationType,
TrialExpirationDate
FROM Organization
LIMIT 1
];
LoggerCache.getOrganizationCache().put(cacheKey, organization);
}
return organization;
}
/**
* @description Returns a cached copy of the current user, including some profile fields that cannot be accessed via `UserInfo`
* @return The cached `Schema.User` record for the current user
*/
public virtual Schema.User getCachedUser() {
Id userId = System.UserInfo.getUserId();
String cacheKey = 'User' + userId;
if (LoggerCache.getSessionCache().contains(cacheKey)) {
return (Schema.User) LoggerCache.getSessionCache().get(cacheKey);
}
Schema.User user = getUsers(new List<Id>{ userId }).get(userId);
if (user != null) {
LoggerCache.getSessionCache().put(cacheKey, user);
}
return user;
}
/**
* @description Returns a list of matching `Schema.Network` records based on the provided list of network IDs
* @param networkIds The list of `Schema.Network` IDs to query
* @return The instance of `Map<Id, SObject>` containing any matching `Schema.Network` records
*/
public Map<Id, LoggerSObjectProxy.Network> getNetworkProxies(List<Id> networkIds) {
Map<Id, LoggerSObjectProxy.Network> networkIdToNetworkProxy = new Map<Id, LoggerSObjectProxy.Network>();
if (LoggerParameter.QUERY_NETWORK_DATA == false) {
return networkIdToNetworkProxy;
}
// Networks (aka experience sites aka community sites aka portal sites ò_ô)
// may not be enabled in the org (no Schema.Network object), so run everything dynamically
String query = 'SELECT Id, Name, UrlPathPrefix FROM Network WHERE Id IN :networkIds';
List<SObject> networkRecords = IS_EXPERIENCE_CLOUD_ENABLED ? System.Database.query(String.escapeSingleQuotes(query)) : new List<SObject>();
List<LoggerSObjectProxy.Network> networkProxies = (List<LoggerSObjectProxy.Network>) System.JSON.deserialize(
System.JSON.serialize(networkRecords),
List<LoggerSObjectProxy.Network>.class
);
if (mockNetworkProxies != null) {
networkProxies = mockNetworkProxies;
}
for (LoggerSObjectProxy.Network networkProxy : networkProxies) {
networkIdToNetworkProxy.put(networkProxy.Id, networkProxy);
}
return networkIdToNetworkProxy;
}
/**
* @description Returns a list of matching `Schema.User` records based on the provided list of user IDs
* @param userIds The list of `Schema.User` IDs to query
* @return The instance of `Map<Id, Schema.User>` containing any matching `Schema.User` records
*/
public Map<Id, Schema.User> getUsers(List<Id> userIds) {
if (LoggerParameter.QUERY_USER_DATA == false) {
return new Map<Id, Schema.User>();
}
return new Map<Id, Schema.User>(
[
SELECT
FederationIdentifier,
Id,
Profile.Name,
Profile.UserLicenseId,
Profile.UserLicense.LicenseDefinitionKey,
Profile.UserLicense.Name,
Username,
UserRole.Name
FROM User
WHERE Id IN :userIds
]
);
}
// DELETEME Deprecated method, remove in a future release
@TestVisible
private static void setMock(LoggerEngineDataSelector mockSelectorInstance) {
instance = mockSelectorInstance;
}
@TestVisible
private static void useMocks() {
// Avoid using the org's actual data records when running tests
// by initializing all of the mock list variables.
// And just in case there are multiple calls made to useMocks(),
// only set the mock variables if they're null.
mockAuthSessionProxies = mockAuthSessionProxies ?? new List<LoggerSObjectProxy.AuthSession>();
mockNetworkProxies = mockNetworkProxies ?? new List<LoggerSObjectProxy.Network>();
}
}