Esempio n. 1
0
        // Determines whether to include the entity on the audit log or not
        private bool IncludeEntity(IAuditDbContext context, object entity, AuditOptionMode mode)
        {
            var type = entity.GetType();

#if EF_FULL
            type = ObjectContext.GetObjectType(type);
#else
            if (type.FullName.StartsWith("Castle.Proxies."))
            {
                type = type.GetTypeInfo().BaseType;
            }
#endif
            bool?result = EnsureEntitiesIncludeIgnoreAttrCache(type);   //true:excluded false=ignored null=unknown
            if (result == null)
            {
                // No static attributes, check the filters
                var localConfig  = EntityFramework.Configuration.GetConfigForType(context.GetType());
                var globalConfig = EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext));
                var included     = EvalIncludeFilter(type, localConfig, globalConfig);
                var ignored      = EvalIgnoreFilter(type, localConfig, globalConfig);
                result = included ? true : ignored ? false : (bool?)null;
            }
            if (mode == AuditOptionMode.OptIn)
            {
                // Include only explicitly included entities
                return(result.GetValueOrDefault());
            }
            // Include all, except the explicitly ignored entities
            return(result == null || result.Value);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the Audit scope asynchronously.
        /// </summary>
        public async Task <IAuditScope> CreateAuditScopeAsync(IAuditDbContext context, EntityFrameworkEvent efEvent)
        {
            var typeName     = context.GetType().Name;
            var eventType    = context.AuditEventType?.Replace("{context}", typeName).Replace("{database}", efEvent.Database) ?? typeName;
            var auditEfEvent = new AuditEventEntityFramework
            {
                EntityFrameworkEvent = efEvent
            };

            if (context.ExtraFields != null && context.ExtraFields.Count > 0)
            {
                auditEfEvent.CustomFields = new Dictionary <string, object>(context.ExtraFields);
            }
            var factory = context.AuditScopeFactory ?? Core.Configuration.AuditScopeFactory;
            var options = new AuditScopeOptions()
            {
                EventType       = eventType,
                CreationPolicy  = EventCreationPolicy.Manual,
                DataProvider    = context.AuditDataProvider,
                AuditEvent      = auditEfEvent,
                SkipExtraFrames = 3
            };
            var scope = await factory.CreateAsync(options);

            context.OnScopeCreated(scope);
            return(scope);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates the Audit scope asynchronously.
        /// </summary>
        public async Task <IAuditScope> CreateAuditScopeAsync(IAuditDbContext context, EntityFrameworkEvent efEvent)
        {
            var typeName     = context.GetType().Name;
            var eventType    = context.AuditEventType?.Replace("{context}", typeName).Replace("{database}", efEvent.Database) ?? typeName;
            var auditEfEvent = new AuditEventEntityFramework
            {
                EntityFrameworkEvent = efEvent
            };
            var factory = context.AuditScopeFactory ?? Core.Configuration.AuditScopeFactory;
            var options = new AuditScopeOptions()
            {
                EventType       = eventType,
                CreationPolicy  = EventCreationPolicy.Manual,
                DataProvider    = context.AuditDataProvider,
                AuditEvent      = auditEfEvent,
                SkipExtraFrames = 3
            };
            var scope = await factory.CreateAsync(options);

            if (context.ExtraFields != null)
            {
                foreach (var field in context.ExtraFields)
                {
                    scope.SetCustomField(field.Key, field.Value);
                }
            }
            context.OnScopeCreated(scope);
            return(scope);
        }
Esempio n. 4
0
        private bool IncludeEntity(IAuditDbContext context, DbEntityEntry entry, AuditOptionMode mode)
#endif
        {
            var type = entry.Entity.GetType();

#if NET45
            type = ObjectContext.GetObjectType(type);
#endif
            bool?result = EnsureEntitiesIncludeIgnoreAttrCache(type);  //true:excluded false=ignored null=unknown
            if (result == null)
            {
                // No static attributes, check the filters
                var localConfig  = EntityFramework.Configuration.GetConfigForType(context.GetType());
                var globalConfig = EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext));
                var included     = EvalIncludeFilter(type, localConfig, globalConfig);
                var ignored      = EvalIgnoreFilter(type, localConfig, globalConfig);
                result = included ? true : ignored ? false : (bool?)null;
            }
            if (mode == AuditOptionMode.OptIn)
            {
                // Include only explicitly included entities
                return(result.GetValueOrDefault());
            }
            // Include all, except the explicitly ignored entities
            return(result == null || result.Value);
        }
Esempio n. 5
0
        /// <summary>
        /// Sets the configuration values from attribute, local and global
        /// </summary>
        public void SetConfig(IAuditDbContext context)
        {
            var type = context.GetType();

            if (!_auditAttributeCache.ContainsKey(type))
            {
                _auditAttributeCache[type] = type.GetTypeInfo().GetCustomAttribute(typeof(AuditDbContextAttribute)) as AuditDbContextAttribute;
            }
            var attrConfig   = _auditAttributeCache[type]?.InternalConfig;
            var localConfig  = Audit.EntityFramework.Configuration.GetConfigForType(type);
            var globalConfig = Audit.EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext));

            context.Mode = attrConfig?.Mode ?? localConfig?.Mode ?? globalConfig?.Mode ?? AuditOptionMode.OptOut;
            context.IncludeEntityObjects = attrConfig?.IncludeEntityObjects ?? localConfig?.IncludeEntityObjects ?? globalConfig?.IncludeEntityObjects ?? false;
            context.AuditEventType       = attrConfig?.AuditEventType ?? localConfig?.AuditEventType ?? globalConfig?.AuditEventType;
        }
Esempio n. 6
0
        /// <summary>
        /// Creates the Audit scope asynchronously.
        /// </summary>
        public async Task <AuditScope> CreateAuditScopeAsync(IAuditDbContext context, EntityFrameworkEvent efEvent)
        {
            var typeName     = context.GetType().Name;
            var eventType    = context.AuditEventType?.Replace("{context}", typeName).Replace("{database}", efEvent.Database) ?? typeName;
            var auditEfEvent = new AuditEventEntityFramework();

            auditEfEvent.EntityFrameworkEvent = efEvent;
            var scope = await AuditScope.CreateAsync(eventType, null, null, EventCreationPolicy.Manual, context.AuditDataProvider, auditEfEvent, 3);

            if (context.ExtraFields != null)
            {
                foreach (var field in context.ExtraFields)
                {
                    scope.SetCustomField(field.Key, field.Value);
                }
            }
            context.OnScopeCreated(scope);
            return(scope);
        }
Esempio n. 7
0
        /// <summary>
        /// Sets the configuration values from attribute, local and global
        /// </summary>
        public void SetConfig(IAuditDbContext context)
        {
            var type = context.GetType();

            if (!_auditAttributeCache.ContainsKey(type))
            {
                _auditAttributeCache[type] = type.GetTypeInfo().GetCustomAttribute(typeof(AuditDbContextAttribute)) as AuditDbContextAttribute;
            }
            var attrConfig   = _auditAttributeCache[type]?.InternalConfig;
            var localConfig  = Audit.EntityFramework.Configuration.GetConfigForType(type);
            var globalConfig = Audit.EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext));

            context.Mode = attrConfig?.Mode ?? localConfig?.Mode ?? globalConfig?.Mode ?? AuditOptionMode.OptOut;
            context.IncludeEntityObjects = attrConfig?.IncludeEntityObjects ?? localConfig?.IncludeEntityObjects ?? globalConfig?.IncludeEntityObjects ?? false;
            context.AuditEventType       = attrConfig?.AuditEventType ?? localConfig?.AuditEventType ?? globalConfig?.AuditEventType;
            context.EntitySettings       = MergeEntitySettings(attrConfig?.EntitySettings, localConfig?.EntitySettings, globalConfig?.EntitySettings);
            context.ExcludeTransactionId = attrConfig?.ExcludeTransactionId ?? localConfig?.ExcludeTransactionId ?? globalConfig?.ExcludeTransactionId ?? false;
#if NET45
            context.IncludeIndependantAssociations = attrConfig?.IncludeIndependantAssociations ?? localConfig?.IncludeIndependantAssociations ?? globalConfig?.IncludeIndependantAssociations ?? false;
#endif
        }