Ejemplo n.º 1
0
        public async Task <IActionResult> Create(Agente agente, int drpCargo, int drpSetor)
        {
            ViewBag.Cargos  = new SelectList(_cargoDAO.ListarTodos(), "CargoId", "NomeCargo");
            ViewBag.Setores = new SelectList(_setorDAO.ListarTodos(), "SetorId", "NomeSetor");
            if (ModelState.IsValid)
            {
                agente.Senha = SenhaPadrao.CriarSenhaPadrao(agente);
                // Preencher obrigatoriamente o UserName e o Email
                AgenteLogado aLogado = new AgenteLogado
                {
                    UserName = agente.Email,
                    Email    = agente.Email
                };

                IdentityResult result = await _userManager.CreateAsync(aLogado, agente.Senha);

                if (result.Succeeded)
                {
                    agente.Cargo = _cargoDAO.BuscarPorId(drpCargo);
                    agente.Setor = _setorDAO.BuscarPorId(drpSetor);

                    //-------------------atribuir role ao user------------------------------
                    var applicationRole = await _roleManager.FindByNameAsync(agente.Cargo.NomeCargo);

                    if (applicationRole != null)
                    {
                        IdentityResult roleResult = await _userManager.AddToRoleAsync(aLogado, agente.Cargo.NomeCargo);
                    }
                    //-------------------atribuir role ao user------------------------------

                    if (_agenteDAO.Cadastrar(agente))
                    {
                        return(RedirectToAction("Index"));
                    }
                }
                AdicionarErros(result);
            }
            ModelState.AddModelError("", "Selecione um Cargo e um Setor!");
            return(View());
        }
Ejemplo n.º 2
0
 // GET: Setor/Edit/5
 public IActionResult Edit(int id)
 {
     return(View(_setorDAO.BuscarPorId(id)));
 }