001package ca.cdr.api.model.json;
002
003/*
004 * #%L
005 * Smile CDR - CDR
006 * %%
007 * Copyright (C) 2016 - 2025 Smile CDR, Inc.
008 * %%
009 * All rights reserved.
010 * #L%
011 */
012
013import ca.cdr.api.model.enm.AuthenticatedUserTypeEnum;
014import com.fasterxml.jackson.annotation.JsonProperty;
015import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
016import com.fasterxml.jackson.databind.annotation.JsonSerialize;
017import io.swagger.v3.oas.annotations.Parameter;
018import io.swagger.v3.oas.annotations.media.Schema;
019import jakarta.validation.Valid;
020import jakarta.validation.constraints.NotBlank;
021import jakarta.validation.constraints.NotNull;
022
023import java.time.Instant;
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.Date;
027import java.util.List;
028
029import static org.apache.commons.lang3.StringUtils.isNotBlank;
030
031/**
032 * This class represents a single Audit Event, with nested objects for all related audit data.
033 *
034 * Also see {@link StringifiedCollectionMixin} to see how we flatten high cardinality elements during CSV export.
035 */
036public class AuditEventJson implements IModelJson {
037        @JsonProperty("id")
038        private Long myId;
039
040        @JsonProperty("transactionGuid")
041        private String myTransactionGuid;
042
043        @JsonProperty("requestId")
044        private String myRequestId;
045
046        @NotBlank
047        @JsonProperty("endpointModuleId")
048        private String myEndpointModuleId;
049
050        @NotBlank
051        @JsonProperty("endpointNodeId")
052        private String myEndpointNodeId;
053        // Nullable
054        @JsonProperty("remoteAddress")
055        private String myRemoteAddress;
056
057        @Valid
058        @JsonProperty("targetModules")
059        private Collection<AuditEventTargetModuleJson> myTargetModules;
060
061        @Valid
062        @JsonProperty("targetResources")
063        private List<AuditEventTargetResourceJson> myTargetResources;
064
065        @Valid
066        @JsonProperty("targetUsers")
067        private Collection<AuditEventTargetUserJson> myTargetUsers;
068
069        @JsonProperty("targetRequestId")
070        @Parameter(
071                        description =
072                                        "If the target of this audit event is a transaction log item (i.e. we are auditing the viewing of the transaction log) this is the Request ID for the transaction log entry in question")
073        private String myTargetRequestId;
074
075        @NotNull
076        @JsonProperty("timestamp")
077        @JsonSerialize(using = JsonDateSerializer.class)
078        @JsonDeserialize(using = JsonDateDeserializer.class)
079        private Date myTimestamp;
080
081        @NotBlank
082        @JsonProperty("typeCode")
083        private String myTypeCode;
084
085        @NotBlank
086        @JsonProperty("typeDisplay")
087        private String myTypeDisplay;
088
089        @NotBlank
090        @JsonProperty("typeSystem")
091        private String myTypeSystem;
092
093        @JsonProperty("familyName")
094        private String myUserFamilyName;
095
096        @JsonProperty("givenName")
097        private String myUserGivenName;
098
099        @JsonProperty("userId")
100        private Long myUserId;
101
102        @JsonProperty("username")
103        private String myUsername;
104
105        @JsonProperty("userModuleId")
106        private String myUserModuleId;
107
108        @JsonProperty("userNodeId")
109        private String myUserNodeId;
110
111        @JsonProperty("authenticatedUserType")
112        @NotNull
113        @Parameter(
114                        description =
115                                        "Was the authentication performed by a user (generally a person) or a client (generally a system)")
116        private AuthenticatedUserTypeEnum myAuthenticatedUserType;
117
118        @JsonProperty("clientNodeId")
119        @Parameter(
120                        description =
121                                        "If this event was received from an OAuth2 client, this is the node ID of the module that supplied the client definition")
122        private String myClientNodeId;
123
124        @JsonProperty("clientModuleId")
125        @Parameter(
126                        description =
127                                        "If this event was received from an OAuth2 client, this is the module ID of the module that supplied the client definition")
128        private String myClientModuleId;
129
130        @JsonProperty("clientId")
131        @Parameter(description = "If this event was received from an OAuth2 client, this is the client ID")
132        private String myClientId;
133
134        @JsonProperty("clientPid")
135        @Schema(
136                        description = "If this event was received from an OAuth2 client, this is the client PID",
137                        accessMode = Schema.AccessMode.READ_ONLY)
138        private Long myClientPid;
139
140        @JsonProperty("clientName")
141        @Parameter(description = "If this event was received from an OAuth2 client, this is the client name")
142        private String myClientName;
143
144        @JsonProperty("headers")
145        @Parameter(
146                        description =
147                                        "A list of headers to be stored in the audit event. This can be configured to change which headers are persisted.")
148        private Collection<AuditEventHeaderJson> myHeaders;
149
150        @JsonProperty("additionalJson")
151        @Parameter(description = "Additional desired JSON to be logged")
152        private String myAdditionalJson;
153        /**
154         * Constructor
155         */
156        public AuditEventJson() {
157                super();
158        }
159
160        public String getRequestId() {
161                return myRequestId;
162        }
163
164        public void setRequestId(String theRequestId) {
165                myRequestId = theRequestId;
166        }
167
168        public Long getClientPid() {
169                return myClientPid;
170        }
171
172        public void setClientPid(Long theClientPid) {
173                myClientPid = theClientPid;
174        }
175
176        public String getClientName() {
177                return myClientName;
178        }
179
180        public void setClientName(String theClientName) {
181                myClientName = theClientName;
182        }
183
184        public String getClientNodeId() {
185                return myClientNodeId;
186        }
187
188        public void setClientNodeId(String theClientNodeId) {
189                myClientNodeId = theClientNodeId;
190        }
191
192        public String getClientModuleId() {
193                return myClientModuleId;
194        }
195
196        public void setClientModuleId(String theClientModuleId) {
197                myClientModuleId = theClientModuleId;
198        }
199
200        public String getClientId() {
201                return myClientId;
202        }
203
204        public void setClientId(String theClientId) {
205                myClientId = theClientId;
206        }
207
208        public String getEndpointModuleId() {
209                return myEndpointModuleId;
210        }
211
212        public void setEndpointModuleId(String theModuleId) {
213                myEndpointModuleId = theModuleId;
214        }
215
216        public String getEndpointNodeId() {
217                return myEndpointNodeId;
218        }
219
220        public void setEndpointNodeId(String theNodeId) {
221                myEndpointNodeId = theNodeId;
222        }
223
224        public Long getId() {
225                return myId;
226        }
227
228        public void setId(Long theId) {
229                myId = theId;
230        }
231
232        public String getUserModuleId() {
233                return myUserModuleId;
234        }
235
236        public void setUserModuleId(String theModuleId) {
237                myUserModuleId = theModuleId;
238        }
239
240        public String getUserNodeId() {
241                return myUserNodeId;
242        }
243
244        public void setUserNodeId(String theNodeId) {
245                myUserNodeId = theNodeId;
246        }
247
248        public String getRemoteAddress() {
249                return myRemoteAddress;
250        }
251
252        public void setRemoteAddress(String theRemoteAddress) {
253                myRemoteAddress = theRemoteAddress;
254        }
255
256        public Collection<AuditEventTargetModuleJson> getTargetModules() {
257                if (myTargetModules == null) {
258                        myTargetModules = new ArrayList<>();
259                }
260                return myTargetModules;
261        }
262
263        public List<AuditEventTargetResourceJson> getTargetResources() {
264                if (myTargetResources == null) {
265                        myTargetResources = new ArrayList<>();
266                }
267                return myTargetResources;
268        }
269
270        public Collection<AuditEventTargetUserJson> getTargetUsers() {
271                if (myTargetUsers == null) {
272                        myTargetUsers = new ArrayList<>();
273                }
274                return myTargetUsers;
275        }
276
277        public Collection<AuditEventHeaderJson> getHeaders() {
278                if (myHeaders == null) {
279                        myHeaders = new ArrayList<>();
280                }
281                return myHeaders;
282        }
283
284        public String getAdditionalJson() {
285                return myAdditionalJson;
286        }
287
288        public void setAdditionalJson(String theAdditionalJson) {
289                myAdditionalJson = theAdditionalJson;
290        }
291
292        public Date getTimestamp() {
293                return myTimestamp;
294        }
295
296        public void setTimestamp(Instant theTimestamp) {
297                myTimestamp = new Date(theTimestamp.toEpochMilli());
298        }
299
300        public void setTimestamp(Date theTimestamp) {
301                myTimestamp = theTimestamp;
302        }
303
304        public String getTypeCode() {
305                return myTypeCode;
306        }
307
308        public void setTypeCode(String theCode) {
309                myTypeCode = theCode;
310        }
311
312        public String getTypeDisplay() {
313                return myTypeDisplay;
314        }
315
316        public void setTypeDisplay(String theTypeDisplay) {
317                myTypeDisplay = theTypeDisplay;
318        }
319
320        public String getTypeSystem() {
321                return myTypeSystem;
322        }
323
324        public void setTypeSystem(String theSystem) {
325                myTypeSystem = theSystem;
326        }
327
328        public String getUserFamilyName() {
329                return myUserFamilyName;
330        }
331
332        public void setUserFamilyName(String theUserFamilyName) {
333                myUserFamilyName = theUserFamilyName;
334        }
335
336        public String getUserGivenName() {
337                return myUserGivenName;
338        }
339
340        public void setUserGivenName(String theUserGivenName) {
341                myUserGivenName = theUserGivenName;
342        }
343
344        public Long getUserId() {
345                return myUserId;
346        }
347
348        public void setUserId(Long theUserId) {
349                myUserId = theUserId;
350        }
351
352        public String getUsername() {
353                return myUsername;
354        }
355
356        public void setUsername(String theUsername) {
357                myUsername = theUsername;
358        }
359
360        @SuppressWarnings("unused") // Called by Web Admin Console
361        public boolean hasTargets() {
362                return getTargetModules().isEmpty() == false
363                                || getTargetResources().isEmpty() == false
364                                || getTargetUsers().isEmpty() == false
365                                || isNotBlank(myTargetRequestId);
366        }
367
368        public AuthenticatedUserTypeEnum getAuthenticatedUserType() {
369                return myAuthenticatedUserType;
370        }
371
372        public void setAuthenticatedUserType(AuthenticatedUserTypeEnum theAuthenticatedUserType) {
373                myAuthenticatedUserType = theAuthenticatedUserType;
374        }
375
376        /**
377         * If the target of this audit event is a transaction log item (i.e. we are auditing the viewing of the transaction log) this is the Request ID for the transaction log entry in question
378         */
379        public String getTargetRequestId() {
380                return myTargetRequestId;
381        }
382
383        /**
384         * If the target of this audit event is a transaction log item (i.e. we are auditing the viewing of the transaction log) this is the Request ID for the transaction log entry in question
385         */
386        public void setTargetRequestId(String theTargetRequestId) {
387                myTargetRequestId = theTargetRequestId;
388        }
389
390        public String getTransactionGuid() {
391                return myTransactionGuid;
392        }
393
394        public void setTransactionGuid(String theTransactionGuid) {
395                myTransactionGuid = theTransactionGuid;
396        }
397}