Example #1
0
        public void Update(int id, UserPreference value)
        {
            using (log.Activity(m => m($"Update {nameof(UserPreference)}[{id}] by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m($"Validate {nameof(value)}")))
                {
                    if (id != value.Id)
                    {
                        //Cannot change primary key
                        log.Warn(l => l($"Cannot update primary key {nameof(UserPreference)}[{id}]"));
                        throw new NotSupportedException();
                    }
                }

                var entity = null as UserPreference;
                using (log.Activity(m => m($"Read {nameof(UserPreference)}[{id}]")))
                {
                    entity = context.UserPreferences.SingleOrDefault(item => item.Id == id);
                    if (entity == null)
                    {
                        log.Warn($"{nameof(UserPreference)}[{id}] is not found");
                        throw new KeyNotFoundException();
                    }
                }

                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateUpdate(entity, value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var oldValue = entity.Filter();
                using (log.Activity(m => m("Update Entity")))
                {
                    try
                    {
                        entity.User           = value.User;
                        entity.SessionTimeout = value.SessionTimeout;
                        entity.Configurations = value.Configurations;
                        entity.MaskData       = value.MaskData;
                        context.SaveChanges();
                    }
                    //TODO: KB: Do this on index validation
                    //throw new DuplicateKeyException(value.Name)
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnUpdated(newValue, oldValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(l => l($"Updated {nameof(UserPreference)}[{id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Example #2
0
        public void Update(int id, PrincipalClaim value)
        {
            using (log.Activity(m => m($"Update {nameof(PrincipalClaim)}[{id}] by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m($"Validate {nameof(value)}")))
                {
                    if (id != value.Id)
                    {
                        //Cannot change primary key
                        log.Warn(l => l($"Cannot update primary key {nameof(PrincipalClaim)}[{id}]"));
                        throw new NotSupportedException();
                    }
                }

                var entity = null as PrincipalClaim;
                using (log.Activity(m => m($"Read {nameof(PrincipalClaim)}[{id}]")))
                {
                    entity = context.PrincipalClaims.Include(item => item.Principal).SingleOrDefault(item => item.Id == id);
                    if (entity == null)
                    {
                        log.Warn($"{nameof(PrincipalClaim)}[{id}] is not found");
                        throw new KeyNotFoundException();
                    }
                }

                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateUpdate(entity, value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var oldValue = entity.Filter();
                using (log.Activity(m => m("Update Entity")))
                {
                    try
                    {
                        entity.Issuer         = value.Issuer;
                        entity.OriginalIssuer = value.OriginalIssuer;
                        entity.Subject        = value.Subject;
                        entity.Type           = value.Type;
                        entity.Value          = value.Value;
                        entity.ValueType      = value.ValueType;
                        context.SaveChanges();
                    }
                    //TODO: KB: Do this on index validation
                    //throw new DuplicateKeyException(value.Name)
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnUpdated(newValue, oldValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(l => l($"Updated {nameof(PrincipalClaim)}[{id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Example #3
0
        public void Update(string key, InfusionGlobalPreference value)
        {
            using (log.Activity(m => m($"Update {nameof(InfusionGlobalPreference)}[{key}] by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m($"Validate {nameof(value)}")))
                {
                    if (key != value.Name)
                    {
                        //Cannot change primary key
                        log.Warn(l => l($"Cannot update unique key {nameof(InfusionGlobalPreference)}[{key}]"));
                        throw new NotSupportedException();
                    }
                }

                var entity = null as InfusionGlobalPreference;
                using (log.Activity(m => m($"Read {nameof(InfusionGlobalPreference)}[{key}]")))
                {
                    entity = context.GlobalPreferences.OfType <InfusionGlobalPreference>().SingleOrDefault(item => item.Name == key);
                    if (entity == null)
                    {
                        log.Warn($"{nameof(GlobalPreference)}[{key}] is not found");
                        throw new KeyNotFoundException();
                    }
                }

                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateUpdate(entity, value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var oldValue = entity.Filter();
                using (log.Activity(m => m("Update Entity")))
                {
                    try
                    {
                        entity.Name = value.Name;
                        //TODO: KB: Do entity by Entity
                        entity.Configurations = value.Configurations;
                        context.SaveChanges();
                    }
                    catch (Exception e) when(e.HasDuplicateKeyNumber())
                    {
                        log.Warn($"Duplicate {nameof(InfusionGlobalPreference.Name)}:\"{value.Name}\"", e);
                        throw new DuplicateKeyException(value.Name);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnUpdated(newValue, oldValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(l => l($"Updated {nameof(InfusionGlobalPreference)}[{key}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Example #4
0
        public void Update(int id, LastActiveRoute value)
        {
            using (log.Activity(m => m($"Update {nameof(LastActiveRoute)}[{id}] by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m($"Validate {nameof(value)}")))
                {
                    if (id != value.Id)
                    {
                        //Cannot change primary key
                        log.Warn(l => l($"Cannot update primary key {nameof(LastActiveRoute)}[{id}]"));
                        throw new NotSupportedException();
                    }
                }

                var entity = null as LastActiveRoute;
                using (log.Activity(m => m($"Read {nameof(LastActiveRoute)}[{id}]")))
                {
                    entity = context.LastActiveRoutes.SingleOrDefault(item => item.Id == id);
                    if (entity == null)
                    {
                        log.Warn($"{nameof(LastActiveRoute)}[{id}] is not found");
                        throw new KeyNotFoundException();
                    }
                }

                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateUpdate(entity, value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var oldValue = entity.Filter();
                using (log.Activity(m => m("Update Entity")))
                {
                    try
                    {
                        entity.User  = value.User;
                        entity.Value = value.Value;
                        context.SaveChanges();
                    }
                    catch (Exception e) when(e.HasDuplicateKeyNumber())
                    {
                        log.Warn($"Duplicate {nameof(LastActiveRoute.User)}:\"{value.User}\"", e);
                        throw new DuplicateKeyException(value.User);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnUpdated(newValue, oldValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(l => l($"Updated {nameof(LastActiveRoute)}[{id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }