Beispiel #1
0
        internal Auditoria AuditarCreacion(object entidad, string nombreEntidad, string claveEntidad)
        {
            var propiedades = entidad.GetType().GetProperties();
            var auditoria   = new Auditoria
            {
                IdOperacion        = IdOperacion,
                IdUsuario          = ((LoginData)App.Current.Resources["LoginData"]).IdUsuario,
                DetallesAuditorias = new List <DetalleAuditoria>()
            };

            foreach (var propiedad in propiedades)
            {
                if (propiedad.PropertyType.FullName != null && (propiedad.PropertyType.FullName.Contains("System")))
                {
                    var detalle = new DetalleAuditoria
                    {
                        Entidad      = nombreEntidad,
                        ClaveEntidad = claveEntidad,
                        Campo        = propiedad.Name,
                        ValorAntiguo = "",
                        ValorNuevo   =
                            propiedad.GetValue(entidad, null) == null
                                ? ""
                                : propiedad.GetValue(entidad, null).ToString()
                    };
                    auditoria.DetallesAuditorias.Add(detalle);
                }
            }
            return(auditoria);
        }
Beispiel #2
0
        internal Auditoria AuditarActualizacion(object entidad, object entidadVieja, string nombreEntidad, string claveEntidad)
        {
            var propiedades = entidad.GetType().GetProperties();
            var auditoria   = new Auditoria
            {
                IdOperacion        = IdOperacion,
                IdUsuario          = ((LoginData)App.Current.Resources["LoginData"]).IdUsuario,
                DetallesAuditorias = new List <DetalleAuditoria>()
            };

            foreach (var propiedad in propiedades)
            {
                if (propiedad.PropertyType.FullName != null && (propiedad.PropertyType.FullName.Contains("System") &&
                                                                CambioPropiedad(entidadVieja, propiedad, NuevoValor(entidad, propiedad))))
                {
                    var detalle = new DetalleAuditoria
                    {
                        Entidad      = nombreEntidad,
                        ClaveEntidad = claveEntidad,
                        Campo        = propiedad.Name,
                        ValorAntiguo = ObtenerValorViejo(entidadVieja, propiedad),
                        ValorNuevo   = NuevoValor(entidad, propiedad)
                    };
                    auditoria.DetallesAuditorias.Add(detalle);
                }
            }
            return(auditoria);
        }
Beispiel #3
0
        internal Auditoria AuditarAsignacion(IEnumerable <object> nuevosValores, IEnumerable <object> viejosValores, string nombreEntidad, string claveEntidad)
        {
            var auditoria = new Auditoria
            {
                IdOperacion        = IdOperacion,
                IdUsuario          = ((LoginData)App.Current.Resources["LoginData"]).IdUsuario,
                DetallesAuditorias = new List <DetalleAuditoria>()
            };

            foreach (var valor in viejosValores)
            {
                var detalle = new DetalleAuditoria
                {
                    Entidad      = nombreEntidad,
                    ClaveEntidad = claveEntidad,
                    Campo        = "",
                    ValorAntiguo = valor.ToString(),
                    ValorNuevo   = ""
                };
                auditoria.DetallesAuditorias.Add(detalle);
            }
            foreach (var valor in nuevosValores)
            {
                var detalle = new DetalleAuditoria
                {
                    Entidad      = nombreEntidad,
                    ClaveEntidad = claveEntidad,
                    Campo        = "",
                    ValorAntiguo = "",
                    ValorNuevo   = valor.ToString(),
                };
                auditoria.DetallesAuditorias.Add(detalle);
            }
            return(auditoria);
        }
Beispiel #4
0
        public Auditoria CrearRegistro <TEntidad>(TEntidad entidad,
                                                  string sucursal, string empresa, string unidadDeNegocio, Usuario Usuario, DbEntityEntry dbEntry)
            where TEntidad : EntidadBase
        {
            List <DetalleAuditoria> result = new List <DetalleAuditoria>();

            TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;
            string         tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name;
            //string keyName = dbEntry.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name;
            var auditoria = new Auditoria();

            auditoria.NombrePC        = Environment.MachineName;
            auditoria.UsuarioWindows  = Environment.UserName;
            auditoria.Windows         = Environment.OSVersion.ToString();
            auditoria.IP              = this.LocalIPAddress();
            auditoria.Sucursal        = sucursal;
            auditoria.Empresa         = empresa;
            auditoria.UnidadDeNegocio = unidadDeNegocio;
            auditoria.Entidad         = entidad.ToString();
            //faltara mas info sobre el usuario?
            auditoria.UsuarioSistema  = Usuario.Nombre;
            auditoria.UnidadDeNegocio = Usuario.UnidadDeNegocioActual.ToString();
            if (Usuario.EmpresaActual != null && Usuario.SucursalActual != null)
            {
                auditoria.Empresa  = Usuario.EmpresaActual.ToString();
                auditoria.Sucursal = Usuario.SucursalActual.ToString();
            }

            if (dbEntry.State == System.Data.Entity.EntityState.Added)
            {
                var detalle = new DetalleAuditoria();
                detalle.Accion        = Accion.Agrega;
                detalle.NombreTabla   = tableName;
                detalle.NombreColumna = "*ALL";
                var json = new JavaScriptSerializer().Serialize(dbEntry.CurrentValues.ToObject());
                detalle.ValorNuevo = json.ToString();
                // For Inserts, just add the whole record
                // If the entity implements IDescribableEntity, use the description from Describe(), otherwise use ToString()
                result.Add(detalle);
            }
            else if (dbEntry.State == System.Data.Entity.EntityState.Deleted)
            {
                // Same with deletes, do the whole record, and use either the description from Describe() or ToString()
                var detalle = new DetalleAuditoria();
                detalle.NombreColumna = "*ALL";
                detalle.NombreTabla   = tableName;
                detalle.Accion        = Accion.Elimina;
                var json = new JavaScriptSerializer().Serialize(dbEntry.OriginalValues.ToObject());
                detalle.ValorNuevo = json.ToString();
                result.Add(detalle);
            }
            else if (dbEntry.State == System.Data.Entity.EntityState.Modified)
            {
                foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
                {
                    // For updates, we only want to capture the columns that actually changed
                    if (!object.Equals(dbEntry.OriginalValues.GetValue <object>(propertyName), dbEntry.CurrentValues.GetValue <object>(propertyName)))
                    {
                        var detalle = new DetalleAuditoria();
                        detalle.Accion        = Accion.Modifica;
                        detalle.NombreTabla   = tableName;
                        detalle.NombreColumna = propertyName;
                        detalle.ValorAnterior = dbEntry.OriginalValues.GetValue <object>(propertyName) == null ? null : dbEntry.OriginalValues.GetValue <object>(propertyName).ToString();
                        detalle.ValorNuevo    = dbEntry.CurrentValues.GetValue <object>(propertyName) == null ? null : dbEntry.CurrentValues.GetValue <object>(propertyName).ToString();
                        result.Add(detalle);
                    }
                }
            }
            auditoria.Detalle = result;
            return(auditoria);
        }