Exemplo n.º 1
0
        /// <summary>
        /// Save <see cref="Audit"/>
        /// </summary>
        /// <param name="event"><see cref="PreUpdateEvent"/></param>
        internal static void Save(PreUpdateEvent @event)
        {
            var differences = AuditDifference.Retrieve(@event);

            if (!differences.Any())
            {
                return;
            }

            var session    = @event.Session.GetSession(EntityMode.Poco);
            var auditClass = AuditClass.Find(session, @event.Entity.GetType().FullName);
            var auditable  = (IAuditable)@event.Entity;

            if (auditClass == null)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(auditable.AuditDescription))
            {
                auditable.AuditDescription = "{0} updated".With(auditClass.Description);
            }

            var audit = Save(session,
                             @event.Id.ToString(),
                             auditable.AuditGroupReference,
                             auditClass,
                             AuditType.Update(),
                             auditable.AuditDescription,
                             auditable.AuditUser);

            foreach (var difference in differences)
            {
                AuditClassPropertyChange.Save(session,
                                              audit,
                                              AuditClassProperty.Find(session, auditClass, difference.Property),
                                              difference.OldValue,
                                              difference.NewValue);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Save <see cref="Audit"/>
        /// </summary>
        /// <param name="session"><see cref="ISession"/></param>
        /// <param name="reference">Reference</param>
        /// <param name="groupReference">Group Reference</param>
        /// <param name="auditClass"><see cref="AuditClass"/></param>
        /// <param name="auditType"><see cref="AuditType"/></param>
        /// <param name="description">Description</param>
        /// <param name="user">User</param>
        /// <returns><see cref="Audit"/></returns>
        internal static Audit Save(ISession session, string reference, string groupReference, AuditClass auditClass, AuditType auditType,
                                   string description, string user)
        {
            if (string.IsNullOrEmpty(user))
            {
                user = Thread.CurrentPrincipal.Identity.Name;
            }

            if (string.IsNullOrWhiteSpace(user) && HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                user = HttpContext.Current.Session[ConfigurationManager.AppSettings["Thunder.Audit.UserKeyInSession"]] as string;
            }

            if (string.IsNullOrWhiteSpace(description))
            {
                if (auditType.IsUpdate)
                {
                    description = "{0} updated".With(auditClass.Description);
                }

                if (auditType.IsInsert)
                {
                    description = "{0} inserted".With(auditClass.Description);
                }
            }

            var audit = new Audit
            {
                Class          = auditClass,
                Type           = auditType,
                Reference      = reference,
                GroupReference = string.IsNullOrEmpty(groupReference) ? reference : groupReference,
                Created        = DateTime.Now,
                Description    = description ?? string.Empty,
                User           = string.IsNullOrEmpty(user) ? "Unknown" : user
            };

            session.Save(audit);

            return(audit);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Save <see cref="Audit"/>
        /// </summary>
        /// <param name="event"><see cref="PreDeleteEvent"/></param>
        internal static void Save(PreDeleteEvent @event)
        {
            if ([email protected]().GetInterfaces().Contains(typeof(IAuditable)))
            {
                return;
            }

            var session    = @event.Session.GetSession(EntityMode.Poco);
            var auditClass = AuditClass.Find(session, @event.Entity.GetType().FullName);

            if (auditClass == null)
            {
                return;
            }

            var auditable = (IAuditable)@event.Entity;

            if (string.IsNullOrWhiteSpace(auditable.AuditDescription))
            {
                auditable.AuditDescription = "{0} deleted".With(auditClass.Description);
            }

            Save(session, @event.Id.ToString(), auditable.AuditGroupReference, auditClass, AuditType.Delete(),
                 auditable.AuditDescription, auditable.AuditUser);
        }