Exemplo n.º 1
0
        public void Create(Role value)
        {
            using (log.Activity(m => m($"Creating {nameof(Role)} 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 Role;
                using (log.Activity(m => m("Create Entity")))
                {
                    try
                    {
                        entity = context.Roles.Add(value);
                        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.OnCreated(newValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(m => m($"Created {nameof(Role)}[{entity.Id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Exemplo n.º 2
0
        private void OnCreated(Facility.Facility value)
        {
            using (log.Activity(m => m($"Execute {nameof(OnCreated)} for {nameof(Facility.Facility)}[{value.Id}]")))
            {
                var realm = null as Authorization.Realm;
                using (log.Activity(m => m($"Define {nameof(Authorization.Realm)} for {nameof(Facility.Facility)}[{value.Id}]")))
                {
                    realm = new Authorization.Realm
                    {
                        Name     = value.Name,
                        ParentId = 1,
                        Claims   = new List <Authorization.RealmClaim>
                        {
                            new Authorization.RealmClaim
                            {
                                Issuer         = "BD.MedView.Facility",
                                OriginalIssuer = "BD.MedView.Facility",
                                Subject        = "Harness",
                                Type           = "Provider.Id",
                                Value          = value.Id.ToString(),
                                ValueType      = "Int32"
                            }
                        },
                        Roles = new List <Authorization.Role>
                        {
                            new Authorization.Role {
                                Name        = "BD.MedView.Web.Admin",
                                Permissions = context.Roles.Where(item => item.RealmId == 1 && item.Name == "BD.MedView.Web.Admin").SelectMany(item => item.Permissions).ToList()
                            },
                            new Authorization.Role {
                                Name        = "BD.MedView.Web.Clinician",
                                Permissions = context.Roles.Where(item => item.RealmId == 1 && item.Name == "BD.MedView.Web.Clinician").SelectMany(item => item.Permissions).ToList()
                            },
                            new Authorization.Role {
                                Name        = "BD.MedView.Web.Pharmacist",
                                Permissions = context.Roles.Where(item => item.RealmId == 1 && item.Name == "BD.MedView.Web.Pharmacist").SelectMany(item => item.Permissions).ToList()
                            },
                            new Authorization.Role {
                                Name        = "BD.MedView.Web.ClinicalPharmacist",
                                Permissions = context.Roles.Where(item => item.RealmId == 1 && item.Name == "BD.MedView.Web.ClinicalPharmacist").SelectMany(item => item.Permissions).ToList()
                            },
                            new Authorization.Role {
                                Name        = "BD.MedView.Web.Technician",
                                Permissions = context.Roles.Where(item => item.RealmId == 1 && item.Name == "BD.MedView.Web.Technician").SelectMany(item => item.Permissions).ToList()
                            }
                        }
                    };
                }

                var entity = null as Authorization.Realm;
                using (log.Activity(m => m($"Create {nameof(Authorization.Realm)} for {nameof(Facility.Facility)}[{value.Id}]")))
                {
                    try
                    {
                        entity = context.Realms.Add(realm);
                        context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        log.Error($"Create {nameof(Authorization.Realm)} for {nameof(Facility.Facility)}[{value.Id}] Error", e);
                        throw;
                    }
                }

                log.Info($"Created {nameof(Authorization.Realm)}[{entity.Id}] for {nameof(Facility.Facility)}[{value.Id}]");
            }
        }