Example #1
0
 public void CreateUserTest1()
 {
     GuMembershipProvider mp = (GuMembershipProvider)Membership.Provider;
     MembershipCreateStatus status = default(MembershipCreateStatus);
     var rg = new Random();
     string ri = rg.Next(1000).ToString("0000");
     GuUser newUser = new GuUser("username" + ri, null, "email" + ri, "question", true)
     {
         NombrePropio = "nombre propio",
         Apellido1 = "Apellido 1"
     };
     var user = mp.CreateUser(newUser, "password", "respuesta", out status);
     Assert.AreEqual(MembershipCreateStatus.Success, status);
 }
Example #2
0
        public ActionResult Create(CreateUserVM u)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //Mapeo objView->ObjDominio
                    GuUser newUser = new GuUser(u.UserName, null, u.Email, u.PasswordQuestion, true)
                    {
                        NombrePropio = u.Nombre,
                        Apellido1 = u.Apellido1,
                        Apellido2 = u.Apellido2
                    };
                    string[] inroles = u.InRoles.Where(i => i.IsInRole).Select(r => r.RoleName).ToArray();
                    string[] outroles = u.InRoles.Where(i => !i.IsInRole).Select(r => r.RoleName).ToArray();

                    MembershipCreateStatus status = default(MembershipCreateStatus);
                    newUser = _mp.CreateUser(newUser, u.Password, u.PasswordAnswer, out status);
                    _rp.AddUsersToRoles(new string[] { u.UserName }, inroles);
                    _rp.RemoveUsersFromRoles(new string[] { u.UserName }, outroles);

                    if (status == MembershipCreateStatus.Success)
                    {
                        ViewBag.Status = status;
                        return RedirectToAction("Details", new { id =newUser.GuUserID });
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, status.ToString());
                        return View(u);
                    }
                }
                else
                {
                    return View(u);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex);
                return View(u);
            }
        }
Example #3
0
        public ActionResult Edit(UserVM u)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //Mapeo VM -> D
                    GuUser du = new GuUser(u.UserName, u.ID, u.Email, null, true)
                    {
                        NombrePropio = u.Nombre,
                        Apellido1 = u.Apellido1,
                        Apellido2 = u.Apellido2
                    };
                    _mp.UpdateUser(du);

                    string[] inroles = u.InRoles.Where(i => i.IsInRole).Select(r => r.RoleName).ToArray();
                    string[] outroles = u.InRoles.Where(i => !i.IsInRole).Select(r => r.RoleName).ToArray();

                    _rp.AddUsersToRoles(new string[] { u.UserName }, inroles);
                    _rp.RemoveUsersFromRoles(new string[] { u.UserName }, outroles);

                    return RedirectToAction("Details", new { id = u.ID });
                    //return View("Details", u);
                }
                else
                {
                    return View(u);
                }
            }

            catch (Exception e)
            {
                if (e is DbEntityValidationException)
                {
                    foreach (var error in ((DbEntityValidationException)e).EntityValidationErrors)
                    {
                        foreach (var item in error.ValidationErrors)
                        {
                            ModelState.AddModelError(string.Empty, item.ErrorMessage);
                        }
                    }
                }
                ModelState.AddModelError(string.Empty, e.Message);
                return View(u);
            }
        }