Esempio n. 1
0
        public ActionResult CreateAjaxAuditGrid([DataSourceRequest] DataSourceRequest request, AuditViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var dateTimeNow = DateTime.Now;

                    var maximumVersionNumer = _auditService.All().Where(x => x.CustomerId == _appUserContext.Current.CurrentCustomer.Id).OrderByDescending(y => y.Version).Select(y => y.Version).FirstOrDefault();

                    if (model.Version > maximumVersionNumer)
                    {
                        model.InsertedDate = dateTimeNow;
                        model.UpdatedDate  = dateTimeNow;
                        var audit = Mapper.Map <AuditViewModel, Audit>(model);
                        _auditService.Create(audit);
                    }
                    else
                    {
                        _contextManager.ResponseManager.StatusCode = 500;
                        _contextManager.ResponseManager.AppendHeader("HandledError", WebResources.CustomerAuditVersionNumber);
                    }
                }
                catch (Exception ex)
                {
                    _contextManager.ResponseManager.StatusCode = 500;
                    _contextManager.ResponseManager.AppendHeader(ModelStateErrorNames.ErrorMessage, ex.Message);
                }
            }

            return(Json(new[] { model }.ToDataSourceResult(request, ModelState)));
        }
Esempio n. 2
0
 public void AuditService_Create_EntityPassedIn_InsertCalledOnRepository()
 {
     _target.Create(new Audit());
     _mockAuditRepository.Verify(v => v.Insert(It.IsAny <Audit>()));
 }
Esempio n. 3
0
        /// <summary>
        /// Audits assessment CUD operations.
        /// </summary>
        /// <param name="service">The audit service instance.</param>
        /// <param name="oldEntity">The old entity.</param>
        /// <param name="newEntity">The new entity.</param>
        /// <param name="action">The action name.</param>
        /// <param name="username">The current username.</param>
        /// <param name="propertiesToSkipAuditing">The properties to skip.</param>
        /// <remarks>All exceptions will be propagated to caller method.</remarks>
        internal static void Audit <T>(IAuditService service, T oldEntity, T newEntity,
                                       string action, string username,
                                       IList <string> propertiesToSkipAuditing = null)
            where T : IdentifiableEntity
        {
            JObject           oldObject     = null;
            var               serializwer   = JsonSerializer.Create(CommonHelper.SerializerSettings);
            JObject           newObject     = JObject.FromObject(newEntity, serializwer);
            IList <JProperty> oldProperties = null;
            IList <JProperty> newProperties = newObject.Properties().ToList();

            if (oldEntity != null)
            {
                oldObject     = JObject.FromObject(oldEntity, serializwer);
                oldProperties = oldObject.Properties().ToList();
            }

            // keep time, so that all records have the same timestamp (also slightly improves performance)
            DateTime now = DateTime.Now;

            // iterate through all object properties
            IEnumerable <string> propertyNames = (oldProperties ?? newProperties).Select(p => p.Name);

            foreach (string propertyName in propertyNames)
            {
                if (propertiesToSkipAuditing != null && propertiesToSkipAuditing.Contains(propertyName))
                {
                    continue;
                }

                JToken oldPropertyValue = null;
                JToken newPropertyValue = newObject.Property(propertyName).Value;

                if (oldEntity != null)
                {
                    oldPropertyValue = oldObject.Property(propertyName).Value;
                }

                // skip when both are null
                if (newPropertyValue.Type == JTokenType.Null && oldPropertyValue == null)
                {
                    continue;
                }

                // when properties are the same - skip auditing
                if (JToken.DeepEquals(oldPropertyValue, newPropertyValue))
                {
                    continue;
                }

                var record = new AuditRecord
                {
                    Action        = action,
                    Field         = propertyName,
                    ItemId        = newEntity.Id,
                    ItemType      = typeof(T).FullName,
                    Timestamp     = now,
                    PreviousValue = CommonHelper.GetJtokenValue(oldPropertyValue),
                    NewValue      = CommonHelper.GetJtokenValue(newPropertyValue),
                    Username      = username
                };

                service.Create(record);
            }
        }