Exemplo n.º 1
0
        public IHttpActionResult Update(int id, LastActiveRoute value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                service.Update(id, value);
                return(StatusCode(HttpStatusCode.NoContent));
            }
            catch (NotSupportedException)
            {
                return(BadRequest());
            }
            catch (UnauthorizedAccessException)
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }
            catch (KeyNotFoundException)
            {
                return(NotFound());
            }
            catch (DuplicateKeyException)
            {
                ModelState.AddModelError(nameof(LastActiveRoute.User), nameof(DuplicateKeyException));
                return(BadRequest(ModelState));
            }
        }
Exemplo n.º 2
0
        public IHttpActionResult Create(LastActiveRoute value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                service.Create(value);
                return(CreatedAtRoute("LastActiveRoutes_Create", new { id = value.Id }, value));
            }
            catch (NotSupportedException)
            {
                return(BadRequest());
            }
            catch (UnauthorizedAccessException)
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }
            catch (KeyNotFoundException)
            {
                return(NotFound());
            }
            catch (DuplicateKeyException)
            {
                ModelState.AddModelError(nameof(LastActiveRoute.User), nameof(DuplicateKeyException));
                return(BadRequest(ModelState));
            }
        }
Exemplo n.º 3
0
        public void Create(LastActiveRoute value)
        {
            using (log.Activity(m => m($"Creating {nameof(LastActiveRoute)} by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateCreate(value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var entity = null as LastActiveRoute;
                using (log.Activity(m => m("Create Entity")))
                {
                    try
                    {
                        entity = context.LastActiveRoutes.Add(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.OnCreated(newValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(m => m($"Created {nameof(LastActiveRoute)}[{entity.Id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Exemplo n.º 4
0
        public void OnPrincipalCreated(string name)
        {
            var value = new LastActiveRoute {
                User  = name,
                Value = "Configuration"
            };
            var entity = context.LastActiveRoutes.Add(value);

            context.SaveChanges();
        }
Exemplo n.º 5
0
        void IEntitySecurity <LastActiveRoute> .ValidateCreate(LastActiveRoute value)
        {
            var principal = GetAuthPrincipal();

            if (string.Equals(principal.Name, value.User, StringComparison.CurrentCultureIgnoreCase))
            {
                return;
            }
            throw new UnauthorizedAccessException();
        }
Exemplo n.º 6
0
        void IEntitySecurity <LastActiveRoute> .ValidateUpdate(LastActiveRoute entity, LastActiveRoute value)
        {
            var principal = GetAuthPrincipal();

            if (IsAdmin(principal) || IsSystem(principal) || IsSuper(principal))
            {
                return;
            }
            if (value.User == entity.User && entity.User == principal.Name)
            {
                return;
            }
            throw new UnauthorizedAccessException();
        }
Exemplo n.º 7
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}"));
            }
        }