Example #1
0
        public async Task <IActionResult> PutSecurityRoleAction([FromRoute] int id, [FromBody] SecurityRoleAction securityRoleAction)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != securityRoleAction.Id)
            {
                return(BadRequest());
            }

            _context.Entry(securityRoleAction).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SecurityRoleActionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> PostSecurityRoleAction([FromBody] SecurityRoleAction securityRoleAction)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.SecurityRoleActions.Add(securityRoleAction);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetSecurityRoleAction", new { id = securityRoleAction.Id }, securityRoleAction));
        }
Example #3
0
        private void SaveBt_Click(object sender, EventArgs e)
        {
            ContextManager           manager = new ContextManager();
            SecurityRoleActionsLogic actions = new SecurityRoleActionsLogic(manager);
            int roleId     = Convert.ToInt32(RolesCB.SelectedValue);
            int categoryId = Convert.ToInt32(CategoriesCB.SelectedValue);

            foreach (DataGridViewRow r in ActionsGV.Rows)
            {
                DataGridViewCheckBoxCell chk = r.Cells["AllowAction"] as DataGridViewCheckBoxCell;
                int  actionId = Convert.ToInt32(r.Cells["ID"].Value.ToString());
                bool allow    = false;
                if (Convert.ToBoolean(chk.Value) == true)
                //MessageBox.Show("this cell checked" + r.Cells["ID"].Value.ToString());
                {
                    allow = true;
                }
                else
                {
                    allow = false;
                }

                SecurityRoleAction actionExists = actions.Get(roleId, actionId);
                if (actionExists == null)
                {
                    actions.Create(roleId, actionId, allow);
                }
                else
                {
                    actions.Update(actionExists.ID, roleId, actionId, allow);
                }
            }
            manager.Save();
            manager.CloseContext();
            MsgL.Text = "Дані збережено.";
        }
Example #4
0
        public async Task <IActionResult> Update([FromBody] UpdateCompanyViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                string strRuta = _config["AvatarFiles"];

                Company company = await _context.Companies.Include(x => x.CompanySectors).Include(x => x.SecurityRoles).ThenInclude(x => x.SecurityRoleScreens).Include(x => x.SecurityRoles).ThenInclude(x => x.SecurityRoleActions).Include(x => x.ConfigScreen).ThenInclude(x => x.SecurityActions).Include(x => x.ConfigScreen).ThenInclude(x => x.ConfigScreenFields).Where(x => x.Id == model.Id).FirstOrDefaultAsync();

                var listRoles = company.SecurityRoles;
                if (company == null)
                {
                    return(BadRequest("Compania inexistente"));
                }

                //Actualizo la compania
                company.Name            = model.Name;
                company.ContactName     = model.ContactName;
                company.ContactLastName = model.ContactLastName;
                company.InitialDate     = model.InitialDate;
                company.Email           = model.Email;
                company.Phone           = model.Phone;
                company.Website         = model.Website;
                company.Address         = model.Address;
                company.Postal          = model.Postal;
                company.City            = model.City;
                company.State           = model.State;
                company.Country         = model.Country;
                company.Schedule        = model.Schedule;
                company.Comment         = model.Comment;

                ////Guardo el avatar
                if (!(string.IsNullOrEmpty(model.LogoName)) && (!string.IsNullOrEmpty(model.Logo)))
                {
                    strRuta = strRuta + "//" + company.Id.ToString() + "//" + model.LogoName;
                    System.IO.FileInfo file = new System.IO.FileInfo(strRuta);
                    file.Directory.Create();
                    System.IO.File.WriteAllBytes(strRuta, Convert.FromBase64String(model.Logo.Substring(model.Logo.LastIndexOf(',') + 1)));
                    company.Logo = strRuta;
                }


                //Sectores
                var sectorToBeAdded = model.CompanySectors.Where(a => company.CompanySectors.All(
                                                                     b => b.SectorId != a.SectorId));

                var sectorToBeDeleted = company.CompanySectors.Where(a => model.CompanySectors.All(
                                                                         b => b.SectorId != a.SectorId));

                foreach (var sector in sectorToBeDeleted)
                {
                    company.CompanySectors.Remove(sector);
                }

                foreach (var sector in sectorToBeAdded)
                {
                    company.CompanySectors.Add(sector);
                }

                //Screens
                //Solo vienen las activas
                var screenToBeAdded = model.ConfigScreens.Where(a => company.ConfigScreen.All(
                                                                    b => b.SystemScreenId != a.SystemScreenId));

                var screenToBeDeleted = company.ConfigScreen.Where(a => model.ConfigScreens.All(
                                                                       b => b.SystemScreenId != a.SystemScreenId));


                //Si no se dan de alta o se eliminan, los actualizo
                //son todos los que no se graban + los que no se eliminan
                List <ConfigScreen> screenNotbeUpdated = new List <ConfigScreen>();
                List <ConfigScreen> screenEnabled      = new List <ConfigScreen>();
                screenNotbeUpdated.AddRange(screenToBeAdded);
                screenNotbeUpdated.AddRange(screenToBeDeleted);
                var screenToBeUpdated = model.ConfigScreens.Where(a => screenNotbeUpdated.All(
                                                                      b => b.SystemScreenId != a.SystemScreenId));

                screenEnabled.AddRange(screenToBeAdded);
                screenEnabled.AddRange(screenToBeUpdated);

                //Elimino las pantallas del paso 2
                foreach (var screenDtl in screenToBeDeleted.ToList())
                {
                    //Borro los childs
                    ConfigScreen configScreen = company.ConfigScreen.Where(x => x.SystemScreenId == screenDtl.SystemScreenId).FirstOrDefault();
                    //Seteo las dependencias
                    var securityactionsToDelete   = configScreen.SecurityActions;
                    var configScreenFieldToDelete = configScreen.ConfigScreenFields;


                    //Borro las acciones
                    foreach (var action in securityactionsToDelete.ToList())
                    {
                        configScreen.SecurityActions.Remove(action);
                    }

                    //Borro los atributos
                    foreach (var attr in configScreenFieldToDelete.ToList())
                    {
                        configScreen.ConfigScreenFields.Remove(attr);
                    }

                    var listRolesToDelete = listRoles;

                    //Borro las pantallas de los roles
                    foreach (var item in listRolesToDelete.ToList())
                    {
                        var listRolesActionToDelete = item.SecurityRoleActions.Where(x => x.SecurityAction.ConfigScreen.SystemScreenId == screenDtl.SystemScreenId).ToList();
                        foreach (var action in listRolesActionToDelete)
                        {
                            item.SecurityRoleActions.Remove(action);
                        }

                        var listRolesScreenToDelete = item.SecurityRoleScreens.Where(x => x.ConfigScreen.SystemScreenId == screenDtl.SystemScreenId).ToList();
                        foreach (var screen in listRolesScreenToDelete)
                        {
                            item.SecurityRoleScreens.Remove(screen);
                        }
                    }

                    company.ConfigScreen.Remove(configScreen);
                }

                //Modifico
                foreach (var screen in company.ConfigScreen)
                {
                    ConfigScreen configScreen = screenToBeUpdated.Where(x => x.SystemScreenId == screen.SystemScreenId).FirstOrDefault();
                    if (configScreen != null)
                    {
                        screen.Description = configScreen.Description;
                        screen.Enabled     = configScreen.Enabled;
                        screen.Icon        = configScreen.Icon;
                        screen.Orden       = configScreen.Orden;
                    }

                    //Actualizo los fields
                    if (configScreen.ConfigScreenFields != null)
                    {
                        foreach (var item2 in configScreen.ConfigScreenFields)
                        {
                            bool existField = false;
                            foreach (var item1 in screen.ConfigScreenFields)
                            {
                                //Me fijo si esta creado, si esta lo actualizo
                                if (item1.SystemScreenFieldId == item2.SystemScreenFieldId)
                                {
                                    item1.Enabled      = item2.Enabled;
                                    item1.DefaultValue = item2.DefaultValue;
                                    item1.Orden        = item2.Orden;
                                    item1.FieldName    = item2.FieldName;
                                    item1.Name         = item2.Name;
                                    item1.Required     = item2.Required;
                                    item1.Visible      = item2.Visible;
                                    existField         = true;
                                    break;
                                }
                            }

                            //Sino existe lo creo
                            if (!existField)
                            {
                                ConfigScreenField configScreenField = new ConfigScreenField
                                {
                                    Enabled             = item2.Enabled,
                                    DefaultValue        = item2.DefaultValue,
                                    Orden               = item2.Orden,
                                    FieldName           = item2.FieldName,
                                    Name                = item2.Name,
                                    Required            = item2.Required,
                                    Visible             = item2.Visible,
                                    ConfigScreenId      = item2.ConfigScreenId,
                                    SystemScreenFieldId = item2.SystemScreenFieldId
                                };
                                screen.ConfigScreenFields.Add(configScreenField);
                            }
                        }
                    }

                    //Actualizo las acciones
                    if (configScreen.SecurityActions != null)
                    {
                        foreach (var item2 in configScreen.SecurityActions)
                        {
                            bool existAction = false;
                            foreach (var item1 in screen.SecurityActions)
                            {
                                if (item1.SystemActionId == item2.SystemActionId)
                                {
                                    item1.Enabled     = item2.Enabled;
                                    item1.Description = item2.Description;
                                    existAction       = true;
                                    break;
                                }
                            }

                            //Sino existe lo creo
                            if (!existAction)
                            {
                                SecurityAction securityAction = new SecurityAction
                                {
                                    Enabled        = item2.Enabled,
                                    Description    = item2.Description,
                                    ConfigScreenId = item2.ConfigScreenId,
                                    SystemActionId = item2.SystemActionId
                                };
                                screen.SecurityActions.Add(securityAction);
                            }
                        }
                    }
                }


                //Agrego
                foreach (var screenAdd in screenToBeAdded.ToList())
                {
                    ConfigScreen configScreenAdd = new ConfigScreen
                    {
                        CompanyId      = model.Id,
                        Description    = screenAdd.Description,
                        Enabled        = screenAdd.Enabled,
                        Orden          = screenAdd.Orden,
                        Icon           = screenAdd.Icon,
                        SystemScreenId = screenAdd.SystemScreenId
                    };

                    //tambien agrego los atributos

                    //Dentro de las pantallas agrego los Atributos
                    if (screenAdd.ConfigScreenFields != null)
                    {
                        configScreenAdd.ConfigScreenFields = new List <ConfigScreenField>();

                        foreach (var field in screenAdd.ConfigScreenFields)
                        {
                            ConfigScreenField configScreenField = new ConfigScreenField
                            {
                                ConfigScreenId      = screenAdd.Id,
                                Name                = field.Name,
                                Required            = field.Required,
                                Visible             = field.Visible,
                                DefaultValue        = field.DefaultValue,
                                Orden               = field.Orden,
                                Enabled             = field.Enabled,
                                FieldName           = field.FieldName,
                                SystemScreenFieldId = field.SystemScreenFieldId,
                            };
                            configScreenAdd.ConfigScreenFields.Add(configScreenField);
                        }
                    }

                    //Dentro de las pantallas agrego las Acciones
                    if (screenAdd.SecurityActions != null)
                    {
                        configScreenAdd.SecurityActions = new List <SecurityAction>();
                        foreach (var action in screenAdd.SecurityActions)
                        {
                            SecurityAction securityAction = new SecurityAction
                            {
                                ConfigScreenId = screenAdd.Id,
                                Description    = action.Description,
                                Enabled        = action.Enabled,
                                SystemActionId = action.SystemActionId,
                            };
                            configScreenAdd.SecurityActions.Add(securityAction);
                        }
                    }



                    company.ConfigScreen.Add(configScreenAdd);
                }


                _context.Entry(company).State = EntityState.Modified;
                await _context.SaveChangesAsync();


                //SecurityRoles
                //Vienen todos los roles
                foreach (var modelRol in model.SecurityRoles.ToList())
                {
                    //Paso 5
                    SecurityRole securityRole = company.SecurityRoles.Where(x => x.SystemRoleId == modelRol.SystemRoleId).FirstOrDefault();

                    if (securityRole != null)
                    {
                        securityRole.Description = modelRol.Description;
                        securityRole.Name        = modelRol.Name;
                        securityRole.Enabled     = modelRol.Enabled;

                        var screenRolToBeAdded = modelRol.SecurityRoleScreens.Where(a => securityRole.SecurityRoleScreens.All(
                                                                                        b => b.ConfigScreen.SystemScreenId != a.SystemScreenId));

                        var screenRolToBeDeleted = securityRole.SecurityRoleScreens.Where(a => modelRol.SecurityRoleScreens.All(
                                                                                              b => b.SystemScreenId != a.ConfigScreen.SystemScreenId));

                        var actionRolToBeAdded = modelRol.SecurityRoleActions.Where(a => securityRole.SecurityRoleActions.All(
                                                                                        b => b.SecurityAction.SystemActionId != a.SystemActionId));

                        var actionRolToBeDeleted = securityRole.SecurityRoleActions.Where(a => modelRol.SecurityRoleActions.All(
                                                                                              b => b.SystemActionId != a.SecurityAction.SystemActionId));

                        //SCREENS
                        foreach (var item in screenRolToBeDeleted.ToList())
                        {
                            securityRole.SecurityRoleScreens.Remove(item);
                        }

                        foreach (var item2 in screenRolToBeAdded.ToList())
                        {
                            SecurityRoleScreen securityRoleScreen = new SecurityRoleScreen
                            {
                                SecurityRoleId = item2.SystemRoleId,
                                ConfigScreenId = company.ConfigScreen.Where(x => x.SystemScreenId == item2.SystemScreenId).Select(x => x.Id).FirstOrDefault()
                            };

                            securityRole.SecurityRoleScreens.Add(securityRoleScreen);
                        }

                        //ACCIONES
                        foreach (var item in actionRolToBeDeleted.ToList())
                        {
                            securityRole.SecurityRoleActions.Remove(item);
                        }

                        foreach (var item2 in actionRolToBeAdded.ToList())
                        {
                            var configScreen = company.ConfigScreen.Where(x => x.SystemScreenId == item2.SystemScreenId).FirstOrDefault();
                            if (configScreen != null)
                            {
                                foreach (var action in configScreen.SecurityActions)
                                {
                                    if (item2.SystemActionId == action.SystemActionId)
                                    {
                                        SecurityRoleAction securityRoleAction = new SecurityRoleAction
                                        {
                                            SecurityRoleId = item2.SystemRoleId
                                        };
                                        securityRoleAction.SecurityActionId = action.Id;
                                        securityRole.SecurityRoleActions.Add(securityRoleAction);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                _context.Entry(company).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }

            return(Ok());
        }
Example #5
0
        public async Task <IActionResult> Create([FromBody] CreateCompanyViewModel model)
        {
            string userPassword = CreatePassword(6);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                string  strRuta = _config["AvatarFiles"];
                Company company = new Company
                {
                    Name            = model.Name,
                    ContactName     = model.ContactName,
                    ContactLastName = model.ContactLastName,
                    InitialDate     = model.InitialDate,
                    Email           = model.Email,
                    Phone           = model.Phone,
                    Website         = model.Website,
                    Address         = model.Address,
                    Postal          = model.Postal,
                    City            = model.City,
                    State           = model.State,
                    Country         = model.Country,
                    Schedule        = model.Schedule,
                    Comment         = model.Comment,
                    CreationDate    = DateTime.Now,
                    Enabled         = true
                };


                //Agrego los sectores
                if (model.CompanySectors != null)
                {
                    company.CompanySectors = new List <CompanySector>();
                    foreach (var sector in model.CompanySectors)
                    {
                        CompanySector companySector = new CompanySector
                        {
                            CompanyId = company.Id,
                            SectorId  = sector.SectorId
                        };
                        company.CompanySectors.Add(companySector);
                    }
                }


                //Agrego las pantallas
                if (model.ConfigScreens != null)
                {
                    company.ConfigScreen = new List <ConfigScreen>();
                    foreach (var item in model.ConfigScreens)
                    {
                        ConfigScreen configScreen = new ConfigScreen
                        {
                            CompanyId      = company.Id,
                            Description    = item.Description,
                            Enabled        = item.Enabled,
                            Orden          = item.Orden,
                            Icon           = item.Icon,
                            SystemScreenId = item.SystemScreenId
                        };

                        //Dentro de las pantallas agrego los Atributos
                        if (item.ConfigScreenFields != null)
                        {
                            configScreen.ConfigScreenFields = new List <ConfigScreenField>();
                            foreach (var field in item.ConfigScreenFields)
                            {
                                ConfigScreenField configScreenField = new ConfigScreenField
                                {
                                    ConfigScreenId      = item.Id,
                                    Name                = field.Name,
                                    Required            = field.Required,
                                    Visible             = field.Visible,
                                    DefaultValue        = field.DefaultValue,
                                    Orden               = field.Orden,
                                    Enabled             = field.Enabled,
                                    FieldName           = field.FieldName,
                                    SystemScreenFieldId = field.SystemScreenFieldId,
                                };
                                configScreen.ConfigScreenFields.Add(configScreenField);
                            }
                        }

                        //Dentro de las pantallas agrego las Acciones
                        if (item.SecurityActions != null)
                        {
                            configScreen.SecurityActions = new List <SecurityAction>();
                            foreach (var action in item.SecurityActions)
                            {
                                SecurityAction securityAction = new SecurityAction
                                {
                                    ConfigScreenId = item.Id,
                                    Description    = action.Description,
                                    Enabled        = action.Enabled,
                                    SystemActionId = action.SystemActionId,
                                };
                                configScreen.SecurityActions.Add(securityAction);
                            }
                        }

                        company.ConfigScreen.Add(configScreen);
                    }
                }



                _context.Companies.Add(company);
                await _context.SaveChangesAsync();



                //Agrego los roles
                ////Agrego las pantallas que aplican para ese rol
                company.SecurityRoles = new List <SecurityRole>();

                foreach (var rol in model.SecurityRoles)
                {
                    int          i            = 0;
                    SecurityRole securityRole = new SecurityRole
                    {
                        Name         = rol.Name,
                        CompanyId    = company.Id,
                        Description  = rol.Description,
                        Enabled      = true,
                        SystemRoleId = rol.SystemRoleId
                    };


                    if (i == 0)
                    {
                        //Genero el usuario
                        securityRole.Usuarios = new List <SecurityUser>();


                        CrearPasswordHash(userPassword, out byte[] passwordHash, out byte[] passwordSalt);

                        SecurityUser user = new SecurityUser
                        {
                            CompanyId      = company.Id,
                            Condicion      = true,
                            Direccion      = company.Address,
                            Email          = company.Email,
                            Nombre         = company.Name,
                            SecurityRoleId = securityRole.Id,
                            Password_hash  = passwordHash,
                            Password_salt  = passwordSalt
                        };

                        securityRole.Usuarios.Add(user);
                    }
                    i++;


                    securityRole.SecurityRoleScreens = new List <SecurityRoleScreen>();
                    securityRole.SecurityRoleActions = new List <SecurityRoleAction>();
                    foreach (var screen in company.ConfigScreen)
                    {
                        //Si la pantalla esta en el rol, la agrego
                        if (rol.SecurityRoleScreens.Where(x => x.SystemScreenId == screen.SystemScreenId).Any())
                        {
                            SecurityRoleScreen securityRoleScreen = new SecurityRoleScreen
                            {
                                ConfigScreenId = screen.Id,
                                SecurityRoleId = securityRole.Id
                            };
                            securityRole.SecurityRoleScreens.Add(securityRoleScreen);


                            foreach (var action in screen.SecurityActions)
                            {
                                if (rol.SecurityRoleActions.Where(x => x.SystemActionId == action.SystemActionId).Any())
                                {
                                    SecurityRoleAction securityRoleAction = new SecurityRoleAction
                                    {
                                        SecurityActionId = action.Id,
                                        SecurityRoleId   = securityRole.Id
                                    };
                                    securityRole.SecurityRoleActions.Add(securityRoleAction);
                                }
                            }
                        }
                    }


                    company.SecurityRoles.Add(securityRole);
                }

                //Guardo el avatar
                if (company.Id > 0)
                {
                    if (!(string.IsNullOrEmpty(model.LogoName)) && (!string.IsNullOrEmpty(model.Logo)))
                    {
                        strRuta = strRuta + "//" + company.Id.ToString() + "//" + model.LogoName;
                        System.IO.FileInfo file = new System.IO.FileInfo(strRuta);
                        file.Directory.Create();
                        System.IO.File.WriteAllBytes(strRuta, Convert.FromBase64String(model.Logo.Substring(model.Logo.LastIndexOf(',') + 1)));
                        company.Logo = strRuta;
                    }
                }

                _context.Entry(company).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }

            return(Ok(userPassword));
        }