/// <summary> /// Queue the sending of an audit /// </summary> /// <param name="state"></param> private void SendAuditAsync(object state) { try { var ad = state as MARC.HI.EHRS.SVC.Auditing.Data.AuditData; var configuration = ApplicationContext.Current.Configuration; // Create the audit basic AuditMessage am = new AuditMessage( ad.Timestamp, (ActionType)Enum.Parse(typeof(ActionType), ad.ActionCode.ToString()), (OutcomeIndicator)Enum.Parse(typeof(OutcomeIndicator), ad.Outcome.ToString()), (EventIdentifierType)Enum.Parse(typeof(EventIdentifierType), ad.EventIdentifier.ToString()), null ); if (ad.EventTypeCode != null) { am.EventIdentification.EventType.Add(new CodeValue <String>(ad.EventTypeCode.Code, ad.EventTypeCode.CodeSystem) { DisplayName = ad.EventTypeCode.DisplayName }); } am.SourceIdentification.Add(new AuditSourceIdentificationType() { AuditEnterpriseSiteID = ad.Metadata.FirstOrDefault(o => o.Key == Data.AuditMetadataKey.EnterpriseSiteID)?.Value ?? String.Format("{1}^^^&{0}&ISO", configuration.DeviceIdentifier, configuration.DeviceName), AuditSourceID = ad.Metadata.FirstOrDefault(o => o.Key == Data.AuditMetadataKey.AuditSourceID)?.Value ?? Dns.GetHostName(), AuditSourceTypeCode = new List <CodeValue <AuditSourceType> >() { new CodeValue <AuditSourceType>( (AuditSourceType)Enum.Parse(typeof(AuditSourceType), ad.Metadata.FirstOrDefault(o => o.Key == Data.AuditMetadataKey.AuditSourceType)?.Value ?? "ApplicationServerProcess")) } }); // Add additional data like the participant bool thisFound = false; string dnsName = Dns.GetHostName(); foreach (var adActor in ad.Actors) { thisFound |= (adActor.NetworkAccessPointId == Environment.MachineName || adActor.NetworkAccessPointId == dnsName) && adActor.NetworkAccessPointType == MARC.HI.EHRS.SVC.Auditing.Data.NetworkAccessPointType.MachineName; var act = new AuditActorData() { NetworkAccessPointId = adActor.NetworkAccessPointId, NetworkAccessPointType = (NetworkAccessPointType)Enum.Parse(typeof(NetworkAccessPointType), adActor.NetworkAccessPointType.ToString()), NetworkAccessPointTypeSpecified = adActor.NetworkAccessPointType != 0, UserIdentifier = adActor.UserIdentifier, UserIsRequestor = adActor.UserIsRequestor, UserName = adActor.UserName, AlternativeUserId = adActor.AlternativeUserId }; foreach (var rol in adActor.ActorRoleCode) { act.ActorRoleCode.Add(new CodeValue <string>(rol.Code, rol.CodeSystem) { DisplayName = rol.DisplayName }); } am.Actors.Add(act); } foreach (var aoPtctpt in ad.AuditableObjects) { var atnaAo = new AuditableObject() { IDTypeCode = aoPtctpt.IDTypeCode.HasValue ? aoPtctpt.IDTypeCode.Value != Auditing.Data.AuditableObjectIdType.Custom ? new CodeValue <AuditableObjectIdType>((AuditableObjectIdType)Enum.Parse(typeof(AuditableObjectIdType), aoPtctpt?.IDTypeCode?.ToString())) : (aoPtctpt.CustomIdTypeCode != null ? new CodeValue <AuditableObjectIdType>() { Code = aoPtctpt.CustomIdTypeCode?.Code, CodeSystem = aoPtctpt.CustomIdTypeCode?.CodeSystem, DisplayName = aoPtctpt.CustomIdTypeCode?.DisplayName } : null) : null, LifecycleType = aoPtctpt.LifecycleType.HasValue ? (AuditableObjectLifecycle)Enum.Parse(typeof(AuditableObjectLifecycle), aoPtctpt.LifecycleType.ToString()) : 0, LifecycleTypeSpecified = aoPtctpt.LifecycleType.HasValue, ObjectId = aoPtctpt.ObjectId, Role = aoPtctpt.Role.HasValue ? (AuditableObjectRole)Enum.Parse(typeof(AuditableObjectRole), aoPtctpt.Role.ToString()) : 0, RoleSpecified = aoPtctpt.Role != 0, Type = (AuditableObjectType)Enum.Parse(typeof(AuditableObjectType), aoPtctpt.Type.ToString()), TypeSpecified = aoPtctpt.Type != 0, ObjectSpec = aoPtctpt.QueryData ?? aoPtctpt.NameData, ObjectSpecChoice = aoPtctpt.QueryData == null ? ObjectDataChoiceType.ParticipantObjectName : ObjectDataChoiceType.ParticipantObjectQuery }; // TODO: Object Data foreach (var kv in aoPtctpt.ObjectData) { if (!String.IsNullOrEmpty(kv.Key)) { atnaAo.ObjectDetail.Add(new ObjectDetailType() { Type = kv.Key, Value = kv.Value }); } } am.AuditableObjects.Add(atnaAo); } // Was a record of this service found? if (!thisFound) { am.Actors.Add(new AuditActorData() { NetworkAccessPointId = Environment.MachineName, NetworkAccessPointType = NetworkAccessPointType.MachineName, UserIdentifier = String.Format("{1}^^^&{0}&ISO", configuration.DeviceIdentifier, configuration.DeviceName) }); } // Send the message this.m_configuration.MessagePublisher.SendMessage(am); } catch (Exception e) { Trace.TraceError(e.ToString()); } }
/// <summary> /// Append user information /// </summary> public static AuditData WithUser(this AuditData me, IPrincipal principal = null) { // Use all remote endpoint providers to find the current request principal = principal ?? AuthenticationContext.Current.Principal; if (principal is IClaimsPrincipal cp) { foreach (var identity in cp.Identities) { if (identity is IDeviceIdentity && identity is IClaimsIdentity did) { me.Actors.Add(new AuditActorData() { NetworkAccessPointId = RemoteEndpointUtil.Current.GetRemoteClient()?.RemoteAddress, NetworkAccessPointType = NetworkAccessPointType.IPAddress, UserName = did.Name, ActorRoleCode = new List <AuditCode>() { new AuditCode("110153", "DCM") { DisplayName = "Source" } }, AlternativeUserId = did.FindFirst(SanteDBClaimTypes.Sid)?.Value }); } else if (identity is IApplicationIdentity && identity is IClaimsIdentity aid) { me.Actors.Add(new AuditActorData() { NetworkAccessPointId = RemoteEndpointUtil.Current.GetRemoteClient()?.RemoteAddress, NetworkAccessPointType = NetworkAccessPointType.IPAddress, UserName = aid.Name, ActorRoleCode = new List <AuditCode>() { new AuditCode("110150", "DCM") { DisplayName = "Application" } }, AlternativeUserId = aid.FindFirst(SanteDBClaimTypes.Sid)?.Value }); } else if (identity is IClaimsIdentity uid) { me.Actors.Add(new AuditActorData() { UserName = uid.Name, UserIsRequestor = true, ActorRoleCode = new List <AuditCode>() { new AuditCode("humanuser", "http://terminology.hl7.org/CodeSystem/extra-security-role-type") { DisplayName = "Human User" } }, AlternativeUserId = uid.FindFirst(SanteDBClaimTypes.Sid)?.Value }); } } } else { var actor = new AuditActorData() { NetworkAccessPointId = RemoteEndpointUtil.Current.GetRemoteClient()?.RemoteAddress, NetworkAccessPointType = NetworkAccessPointType.IPAddress, UserName = principal.Identity.Name }; if (principal.Identity is IApplicationIdentity || principal.Identity is IDeviceIdentity) { actor.ActorRoleCode.Add(new AuditCode("110153", "DCM") { DisplayName = "Source" }); } else { actor.UserIsRequestor = true; actor.ActorRoleCode.Add(new AuditCode("humanuser", "http://terminology.hl7.org/CodeSystem/extra-security-role-type")); } me.Actors.Add(actor); } return(me); }