Example #1
0
        public ActionResult Create(NewUserViewModel newUserViewModel)
        {
            ViewBag.ROLES = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();

            if (ModelState.IsValid)
            {
                var repo = new UserRepository();
                if (repo.GetUserByPersonaId(newUserViewModel.USER_PERSONA_ID) != null)
                {
                    Session["error"] = "Ya existe un usuario con esa persona asignada";

                    return RedirectToAction("Index");
                }
                int personaId = repo.GetPersonaIdFromDni(newUserViewModel.UserName);
                if (personaId == 0)
                {
                    ModelState.AddModelError("UserName", "No existe ninguna persona con este documento.");
                    return View(newUserViewModel);
                }
                newUserViewModel.USER_PERSONA_ID = personaId;
                string userId = repo.CreateUser(newUserViewModel);
                if (userId == null)
                {
                    ModelState.AddModelError("UserName", "Ya existe un usuario con este documento.");
                    return View(newUserViewModel);
                }
                string code = UserManager.GeneratePasswordResetToken(userId);
                string encodedCode = code.Base64ForUrlEncode();
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId, code = encodedCode, area = "" }, protocol: Request.Url.Scheme);

                Mailer.SendMailWithOffice365(newUserViewModel.Email, newUserViewModel.UserName, callbackUrl);

                Flash.Instance.Success("Usuario Creado", "El usuario debera revisar su correo para activar su cuenta.");

                return RedirectToAction("Index");
            }
            return View(newUserViewModel);
        }
Example #2
0
 public UsuariosController()
 {
     context = GetContext();
     _repo = new UserRepository();
 }