Ejemplo n.º 1
0
        public void OnPreUpdateCollection(PreCollectionUpdateEvent @event)
        {
            var affectedOwnerEntityName = @event.GetAffectedOwnerEntityName();

            //var role = @event.Collection.Role;
            if (_authorizationObjects.Any(a => a == affectedOwnerEntityName) &&
                @event.Collection.IsDirty)
            {
                OperationAuthorizationService.InvalidateCache();
            }
        }
Ejemplo n.º 2
0
        public void OnPreCollectionUpdateLogEvent(PreCollectionUpdateEvent @event)
        {
            if (IsTemporarilyDisabled)
            {
                return;
            }
            if (!_isEnabled)
            {
                return;
            }
            if (@event == null || @event.AffectedOwnerOrNull is AuditLogEntry)
            {
                return;
            }
            // Is the Parent Entity of the collection Auditable?
            var affectedOwnerEntityName = @event.GetAffectedOwnerEntityName();

            if (!_auditableEntities.ContainsKey(affectedOwnerEntityName))
            {
                return;
            }
            // Is the Collection Auditable?
            var auditableEntity = _auditableEntities[affectedOwnerEntityName];
            var role            = @event.Collection.Role;
            var key             = role.Substring(role.LastIndexOf(".", StringComparison.Ordinal) + 1);

            if (!auditableEntity.Properties.ContainsKey(key))
            {
                return;
            }
            var auditableProperty      = auditableEntity.Properties[key];
            var classMetadata          = @event.Session.SessionFactory.GetClassMetadata(auditableProperty.Datatype);
            var identifierPropertyName = classMetadata.IdentifierPropertyName;

            if (!(@event.Session.SessionFactory.GetCollectionMetadata(role) is ICollectionPersister collectionPersister))
            {
                throw new Exception("CollectionPersister is null. Not sure why.");
            }

            var type = GetTypeFromClassName(auditableProperty.Datatype);

            if (!(type != null))
            {
                return;
            }
            if (collectionPersister.IsInverse)
            {
                _log.Debug($"Collection [{key}] of entity [{affectedOwnerEntityName}] is inverse!");
                return;
            }
            var session     = @event.Session.GetSession(EntityMode.Poco);
            var username    = _getAuditContext().Username;
            var shouldFlush = false;

            foreach (var obj1 in @event.Collection.GetDeletes(collectionPersister, false))
            {
                var propertyInfo = type.GetProperty(identifierPropertyName);
                if (propertyInfo == null)
                {
                    continue;
                }
                var keyValue = propertyInfo.GetValue(obj1);
                var entry    = new AuditLogEntry
                {
                    EntityShortName = @event.AffectedOwnerOrNull.GetType().Name,
                    EntityFullName  = affectedOwnerEntityName,
                    UserName        = username,
                    EntityId        = @event.AffectedOwnerIdOrNull.ToString(),
                    EntryTypeId     = _auditLogEntryTypes["UPDATE"].Id,
                    Timestamp       = DateTime.UtcNow,
                    ActionTypeId    = _auditLogPropertyActionTypes["REMOVE"].Id,
                    PropertyName    = key,
                    OldValue        = keyValue.ToString()
                };
                session.Save(entry);
                shouldFlush = true;
            }
            var i = -1;

            if (@event.Collection.IsDirty)
            {
                foreach (var entryAdded in @event.Collection.Entries(collectionPersister))
                {
                    ++i;
                    var elemType = NHibernateUtil.GetSerializable(type);
                    if ([email protected](entryAdded, i, elemType))
                    {
                        continue;
                    }
                    var propertyInfo = type.GetProperty(identifierPropertyName);
                    if (propertyInfo == null)
                    {
                        continue;
                    }
                    var keyValue = propertyInfo.GetValue(entryAdded);
                    var entry    = new AuditLogEntry
                    {
                        EntityShortName = @event.AffectedOwnerOrNull.GetType().Name,
                        EntityFullName  = affectedOwnerEntityName,
                        UserName        = username,
                        EntityId        = @event.AffectedOwnerIdOrNull.ToString(),
                        EntryTypeId     = _auditLogEntryTypes["UPDATE"].Id,
                        Timestamp       = DateTime.UtcNow,
                        ActionTypeId    = _auditLogPropertyActionTypes["ADD"].Id,
                        PropertyName    = key,
                        NewValue        = keyValue.ToString()
                    };
                    session.Save(entry);
                    shouldFlush = true;
                }
                if (i > 10)
                {
                    _log.Warn($"Parsed {i} Entities for Collection [{key}] of entity [{affectedOwnerEntityName}]!");
                }
            }
            else
            {
                _log.Info($"Collection [{key}] of entity [{affectedOwnerEntityName}] is not Dirty. Skipping looking for inserted entries.");
            }
            if (shouldFlush)
            {
                // Flush only once
                session.Flush();
            }
        }